Spring Annotation Tutorial

@Component :       

It is generic  stereotype for any spring managed component. While other stereotypes like @Repository, @Controller, @Service are specialized form of @Component for specific use. Specilized form of stereotypes(e.g. @Controller) should take precedence over @Component as it will have extra benifit of pointcuts, better suited for processing by spring tools. Since Spring 2.5.

@Repository :

Specialized form of @Component. This annotation work as marker for any class which fulfills the role of repository or Data Access  Object(Persistent Layer). Since Spring 2.0

@Controller :

Specialized form of @Component and applied to Presentation Layer.

@Service :

Specialized form of @Component and applied to Service Layer.

@Configuration :

@Configuration annotated class are same like old XML file where we used to define beans. @Configuration is configuring the beans using Java classes. Each @Configuration annotated class is a configuration in its own. @Configuration can be used in conjuction with @Bean to create beans. Java class annotated with @Configuration will have methods to instantiated and configure the dependencies. Such methods will be annotated with @Bean.  @Configuration annotated classes consist principally of @Bean-annotated methods that define instantiation, configuration, and initialization logic for objects that will be managed by the Spring IoC container.

@Configuration
public class SessionFactoryConfiguration {

   @Bean
   public DataSource dataSource() {
     DataSource dataSource = new OracleDataSource();
     dataSource.setURL();
     dataSource.setUser();
     return dataSource;
   }

   @Bean
   public SessionFactory sessionFactory() {
     SessionFactory sessionFactory
                = new SessionFactory();
     sessionFactory.setDataSource(dataSource());
     sessionFactory.setShowSql(true);
     return sessionFactory;
   }

}

@Bean

It is a method level annotation which work with @Configuration as mentioned above. It is same like <bean> element in XML configuration. It can also be used in Component Class(Annotated with @Configuration).

@Configuration
public class AppConfig {

    @Bean
    public ContactService contactService() {
        return new ContactServiceImpl();
    }

Above configuration is same as

<beans>
    <bean name="contactService" class="com.ContactServiceImpl"/>
</beans>

@Scope

Every dean defined in IoC container needs to have a scope, default scope is Singleton. @Scope is used in order to specify the scope of Beans defined with @Bean annotation. StandardScopes class is there to define all 4 type of scope.

@Configuration
public class MyConfiguration {
    @Bean
    @Scope(StandardScopes.PROTOTYPE)
    public ContactService contactService() {

    }
}

@DependsOn

@Primary

@Lazy

@Import

@ImportResource

@Value

private @Value("#{jdbcProperties.url}") String jdbcUrl;

To get this work you need to add property file in application context XML.

<util:properties id="jdbcProperties" location="classpath:jdbc.properties"/>
By | 2017-07-21T19:35:23+00:00 June 16th, 2012|Spring|2 Comments

About the Author:

2 Comments

  1. werwef July 5, 2014 at 11:17 am - Reply

    we

  2. Jesse October 7, 2016 at 2:25 pm - Reply

    How do you know where to put these annotations??

Leave A Comment