GoConqr has a new look! Let us know what you think.

Mind map

Spring Beans and dependency injection

Felipe Malfatti Portuguese

Map content

Spring Beans and dependency injection

  1. @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"/>
  2. 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" } )
  3. 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); } }
  4. 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 @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 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
  5. 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>
    • FileSystemXmlApplicationContext
      • new FileSystemXmlApplicationContext("c:/knight.xml");
    • XmlWebApplicationContext
    • WebApplicationContext
      • ContextLoaderListener
        • Bootstrap listener to start up and shut down Spring's root WebApplicationContext.
  6. BeanFactory (Interface)

    • Bean instantiation/wiring
  7. 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
                    • If the bean implements BeanPostProcessor,
                      • postProcessAfterInitialization()
                  • If the bean implements the InitializingBean interface
                    • afterPropertiesSet()
                • If the bean implements the BeanPostProcessor interface,
                  • postProcessBeforeInitialization()
              • If the bean implements ApplicationContextAware
            • If the bean implements BeanFactoryAware
          • If the bean implements BeanNameAware
            • setBeanName()
    • Constructor method is called before DI
  8. Container shutdown

    • DisposableBean’s destroy()
      • Call custom destroy-method
        • End of bean
      • If the bean implements the DisposableBean interface
        • destroy()
    • registerShutdownHook()
      • shutdown and call the relevant destroy methods
      • Method of AbstractApplicationContext
  9. @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
    • @Required ou required= true
    • when using in constructor
      • only one can have required=true

Information

Description
Mind Map on Spring Beans and dependency injection, created by Felipe Malfatti on 20/02/2018.
Created by
Felipe Malfatti
Views
18
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?