Wednesday, September 24, 2014

Spring commanly used annotation and their usage.

@Configuration

Refer :stackoverflow.com and theserverside.com

@Configuration is the heart of the Java-based configuration mechanism that was introduced in Spring 3. It provides an alternative to XML-based configuration.
So the 2 following snippets are identical:
<beans ...>
    <context:component-scan base-package="my.base.package"/>
    ... other configuration ...
</beans>
and:
@Configuration
@ComponentScan(basePackages = "my.base.package")
public class RootConfig {
    ... other configuration ...
}
In both cases Spring will scan in my.base.package and below for classes annotated with @Component or one of the other annotations that are meta-annotated with @Component such as @Service.


@Import 

Refer : Spring @Import
Indicates one or more @Configuration classes to import.
Provides functionality equivalent to the <import/> element in Spring XML. Only supported for classes annotated with @Configuration or declaring at least one @Bean method, as well as ImportSelector and ImportBeanDefinitionRegistrarimplementations.
@Bean definitions declared in imported @Configuration classes should be accessed by using @Autowired injection. Either the bean itself can be autowired, or the configuration class instance declaring the bean can be autowired. The latter approach allows for explicit, IDE-friendly navigation between @Configuration class methods.


@Bean

Refer : Spring @Bean
Indicates that a method produces a bean to be managed by the Spring container.
 


@Value

Refer :  Spring @Value but you can fine better explanation on other place..

This annotation is basically used to get the value defined  properties file on the basis of key value pair.

In order for @Value annotations to work PropertySourcesPlaceholderConfigurer should be registered. It is done automatically when using <context:property-placeholder> in XML, but should be registered as a static @Bean when using @Configuration.


@Value can be used in three different ways:  

1)
@Value ("${test.at.value}")
String atValue;
2)
@Value ("${test.at.value}") 
public void testATValue(String atValue){
                  println("value of atValue : " atValue);
 }
 3)
public  void testAtValue(@Value ("${test.at.value}")  String atValue) {
                  println("value of atValue : " atValue);
 }

@Profile

Refer : Spring @Profile 

This annotation is used basically used to activate or run a piece of code when certain profile is active.
Can act as filter for conditional loading or importing of stuff.


Condition loading of bean example : depending upon what is the current active profile corresponding bean will be loded

@Bean
@Profile("TestUser")
public User loadTestUser()  {
               return new TestUser();
}

@Bean
@Profile("NormalUser")
public User loadNormalUser()  {
               return new NormalUser();
}

A profile is a named logical grouping that may be activated programmatically via ConfigurableEnvironment.setActiveProfiles(java.lang.String...) or declaratively through setting the spring.profiles.active property, usually through JVM system properties, as an environment variable, or for web applications as a Servlet context parameter in web.xml.


Hope this helps.





No comments:

Post a Comment