Sunday, 20 December 2020

Spring annotations


@Component is added at a class, meaning that this class will be scanned by the Spring container and loaded in the Spring context.  @Component is the most generic decoration. A @Service is a component; A @Controller is a component too. 

@Service means actually a specific @Component, which stands for a business logic layer.  So when a class is added with @Service, it will be scanned by Spring container and loaded in the context. 

@RestController  is also a specific @Component when a Spring context is initiated. These controllers will be instantiated and start waiting for the requests and making responses. 

@Autowired is specific to the Spring framework; it is equivalent to the Java @Injected 

@Configuration and its Relevant Annotations


@Configuration working with @Bean, @Value, @PropertySource, @ComponentScan and @Profile

@Configuration together with @Bean defines a bean blueprint, which tells a bean factory how to create a bean and its dependencies(DI).  In a simple word, it depicts a bean graph. 

@Configuration indicates a class, in which one or more @Beans methods are declared. 

What is a @Bean? @Bean is used to decorate a method. It tells Spring that this method is used to return an instantiated  @component or @Service. @Bean is used together with @Configuration class to tell Spring framework how to do DI. 



@Cofiguration working with externalized values

@Configuration class may need external env properties or other values defined in the other property files. 

Using  Environment API

Via Spring Environment injection, we may access properties. 
@Configuration
 public class AppConfig {

     @Autowired Environment env;

     @Bean
     public MyBean myBean() {
         MyBean myBean = new MyBean();
         myBean.setName(env.getProperty("bean.name"));
         return myBean;
     }
 }

Using Property Sources

Properties resolved via Environment reside in one or more "property source" objects; @Configuration may work with specific property files. 
@Configuration
@PropertySource("classpath:/com/acme/app.properties")
 public class AppConfig {

     @Inject Environment env;

     @Bean
     public MyBean myBean() {
         return new MyBean(env.getProperty("bean.name"));
     }
 }

Using Value Injection

Without bypass the Environment, externalized values may be injected into @Configuration via @Value.
@Configuration
 @PropertySource("classpath:/com/acme/app.properties")
 public class AppConfig {

     @Value("${bean.name}") String beanName;

     @Bean
     public MyBean myBean() {
         return new MyBean(beanName);
     }
 }

Specifying @Component Location

@ComponentScan configures component scanning directives for use with @Configuration classes. 

@Configuration
 @ComponentScan("com.acme.app.services")
 public class AppConfig {
     // various @Bean definitions ...
 }


?About proxyBeanMethods

By default  @Configuration(proxyBeanMethods=true); it specifies whether @Bean methods should get proxied in order to enforce bean lifecycle behaviour, e.g to return shared singleton bean instances even in direct @Bean method calls in user code. This feature requires method interception, implemented through a runtime-generated CGLIB subclass which comes with limitations such as configuration class and its methods not being allowed to declare final.  
public abstract boolean proxyBeanMethods


Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...