Mindmap
Spring Beans and dependency injection
Inhalt der Mindmap
Spring Beans and dependency injection
@ComponentScan is used to find your beans,
- @Component, @Service, @Repository, @Controller etc
- And if a bean has one constructor, you can omit the @Autowired.
- @SpringBootApplication
- Is the composite of @Configuration, @ComponentScan, and @EnableAutoConfiguration
- <context:component-scan base-package="com.example.foo, com.example.bar"/>
- @Component, @Service, @Repository, @Controller etc
One type of DI is constructor injection (On a constructor method)
- <bean id="knight" class="com.springinaction.knights.BraveKnight"> <constructor-arg ref="quest" /> </bean>
- <bean id="quest" class="com.springinaction.knights.SlayDragonQuest"> <constructor-arg value="#{T(System).out}" /> </bean>
- To use a shortchut p:nameAttribute. You need to set in your XML header xmlns:p="http://www.springframework.org/schema/p"
- @Autowired Public Knight(Quest quest) { ... }
- @Configuration public class KnightConfig { @Bean public Knight knight() { return new BraveKnight(quest()); } @Bean public Quest quest() { return new SlayDragonQuest(System.out); } }
- A bean can have multiples names
- @Bean(name = { "value1", "value2" } )
- A bean can have multiples names
- @Configuration public class KnightConfig { @Bean public Knight knight() { return new BraveKnight(quest()); } @Bean public Quest quest() { return new SlayDragonQuest(System.out); } }
- <bean id="knight" class="com.springinaction.knights.BraveKnight"> <constructor-arg ref="quest" /> </bean>
public interface CompactDisc { void play(); }
- @Component public class SgtPeppers implements CompactDisc { private String title = "Sgt. Pepper's Lonely Hearts Club Band"; private String artist = "The Beatles"; public void play() { System.out.println("Playing " + title + " by " + artist); } }
If there are two implementations of a bean. Spring will throw a NoUniqueBeanDefinitionException
- Autowirin with @Qualifier("NomeDoBean")
- Or you can use @Primary anottation
- you can create a custom Qualifier, creating a Annotation with the @Qualifier annotation within
- @Qualifier public @interface Genre { String value(); }
- @Autowired @Genre("Action") private MovieCatalog actionCatalog;
- <bean class="br.com.Exemplo"><qualifier type="Genre" value="Action"/></bean>
- @Autowired @Genre("Action") private MovieCatalog actionCatalog;
- @Autowired @MovieQualifier(format=Format.DVD, genre="Action") private MovieCatalog actionDvdCatalog;
- <bean class="br.com.Exemplo"> <qualifier type="MovieQualifier"> <attribute key="format" value="VHS"/> <attribute key="genre" value="Action"/> </qualifier> </bean>
- Type is the type of annotation. Relative or qualified
- Attribute tag with key and value for distinction the qualifier (inner qualifier)
- XML can validate a unique id for bean
- <bean class="example.SimpleMovieCatalog"><meta key="format" value="DVD"/><metakey="genre" value="Action"/></bean>
- Meta tag with key and value for distinction the qualifier
- <bean class="br.com.Exemplo"> <qualifier type="MovieQualifier"> <attribute key="format" value="VHS"/> <attribute key="genre" value="Action"/> </qualifier> </bean>
- @Qualifier public @interface Genre { String value(); }
- <bean class="br.com.Exemplo"> <qualifier value="NomeDoBean"/> </bean>
- can be used on properties, setters, and constructors.
- JSR 330's @Qualifier or Spring @Qualifier
- Or spring use the field name to determine what bean should use
- Autowirin with @Qualifier("NomeDoBean")
ApplicationContext (Interface)
- enhances BeanFactory
- Bean instantiation/wiring
- Automatic BeanPostProcessor registration
- Automatic BeanFactoryPostProcessor registration
- MessageSource (i18n)
- ApplicationEvent publication
- AnnotationConfigApplicationContext
- AnnotationConfigWebApplicationContext
- new AnnotationConfigApplicationContext( com.springinaction.knights.config.KnightConfig.class);
- ClassPathXmlApplicationContext
- new ClassPathXmlApplicationContext("knight.xml");
- <beans> <import resources="myServices.xml"/> <import resources="myResources/messageSourceA.xml"/> <import resources="/myResources/messageSourceB.xml"/> </beans>
- new ClassPathXmlApplicationContext("knight.xml");
- FileSystemXmlApplicationContext
- new FileSystemXmlApplicationContext("c:/knight.xml");
- XmlWebApplicationContext
- WebApplicationContext
- ContextLoaderListener
- Bootstrap listener to start up and shut down Spring's root WebApplicationContext.
- ContextLoaderListener
BeanFactory (Interface)
- Bean instantiation/wiring
Bean Life Cycle
- Instantiate
- Populate properties
- BeanNameAware’s setBeanName()
- BeanFactoryAware’s setBeanFactory()
- ApplicationContextAware’s setApplicationContext()
- Pre-initialization BeanPostProcessors
- InitializingBean ’s afterPropertiesSet
- Call custom init-method
- Post-initialization BeanPostProcessors
- BEAN IS READY TO USE
- @PostConstruct and @PreDestroy
- Methods can have any access modifier, but no arguments and return void
- Defined by JSR-250
- Also used by EJB3
- Alternative in @Configuration is @Bean(initMethod="init”, destroyMethod="destroy") - can be used for third party beans
- Method is invoked by Spring after DI (PostConstruct)
- equivalent to init-method attribute
- @PreDestroy
- Method is invoked before bean is destroyed
- Is not called for prototype scoped beans!
- After application context is closed
- Only if JVM exits normally
- equivalent to destroy-method attribute
- @PostConstruct and @PreDestroy
- BEAN IS READY TO USE
- If the bean implements BeanPostProcessor,
- postProcessAfterInitialization()
- Post-initialization BeanPostProcessors
- If the bean implements the InitializingBean interface
- afterPropertiesSet()
- Call custom init-method
- If the bean implements the BeanPostProcessor interface,
- postProcessBeforeInitialization()
- InitializingBean ’s afterPropertiesSet
- If the bean implements ApplicationContextAware
- Pre-initialization BeanPostProcessors
- If the bean implements BeanFactoryAware
- ApplicationContextAware’s setApplicationContext()
- If the bean implements BeanNameAware
- setBeanName()
- BeanFactoryAware’s setBeanFactory()
- BeanNameAware’s setBeanName()
- Populate properties
- Constructor method is called before DI
- Instantiate
Container shutdown
- DisposableBean’s destroy()
- Call custom destroy-method
- End of bean
- If the bean implements the DisposableBean interface
- destroy()
- Call custom destroy-method
- registerShutdownHook()
- shutdown and call the relevant destroy methods
- Method of AbstractApplicationContext
- DisposableBean’s destroy()
@Autowired
- Field, Constructor or methods
- @Resource
- Identifies dependencies for injection by name, not by type like @Autowired
- Name is spring bean name
- Can be used for field and setter DI, not for constructors
- With name @Resource("beanName") or without
- Identifies dependencies for injection by name, not by type like @Autowired
- @Required ou required= true
- when using in constructor
- only one can have required=true
Informationen
- Beschreibung
- Mindmap am Spring Beans and dependency injection, erstellt von Felipe Malfatti am 20/02/2018.
- Erstellt von
- Felipe Malfatti
- Aufrufe
- 18
- Tags
- Bewertung