Wednesday, January 25, 2012

Annotation-Based Spring Application context




Our goal is to get rid of manual wiring in the configuration files (Java or XML)

Annotation based application context

Define configuration bean as application context 

Define configuration java class having @Configuration, this java class is called Annotation based application context
1.                     @Configuration
     public class ApplicationConfig{
   
    }

    Register you bean


  • Register your java bean using@ComponentScan, This will auto-detect your classes as spring bean in package com.sudhir 
  @Configuration
     @ComponentScan(basePackages="com.sudhir", excludeFilters {@ComponentScan.Filter(Configuration.class)})
  public class ApplicationConfig{
   
}

There are 4 steriotype annotation
@Component Indicates that an annotated class is a "component".
@Repository Indicates that an annotated class is a "Repository" (or "DAO").
@Service Indicates that an annotated class is a "Service" (e.g. a business service facade).
@Controller Indicates that an annotated class is a "Controller" (e.g. a web controller).

Here is an example of DAO classes 


@Repository
public class BeerDaoImpl implements BeerDao{
}

Inject the bean as dependency

Inject the required bean using @autowired , example bean dataSource is injected using autowiring

@Repository
public class BeerDaoImpl implements BeerDao{
            JdbcTemplate jdbcTemplate;

            @Autowired
            public void setDataSource(DataSource dataSource) {
                        this.jdbcTemplate = new JdbcTemplate(dataSource);
            }        
}

Register third party bean

   DataSource is not your bean how do you inject the data source bean?, what if the bean are third party classes, we need to define such bean in configuration file, configuration for datasource.
a.       We don’t want to hard code the properties in datasource and want to use the properties file
b.      Add  @PropertySource annotation to get the properties files values in Environment class
c.       Inject Environment class using autowired
Java based configuration file for data source


@Configuration
@ComponentScan(basePackages = "cybage", excludeFilters = {@ComponentScan.Filter(Configuration.class)})
@PropertySource("classpath:jdbc.properties")
public class ApplicationConfig{
   
            @Autowired
    Environment env;
           
            @Bean
            public DataSource dataSource() {
                        com.mchange.v2.c3p0.ComboPooledDataSource dataSource = new com.mchange.v2.c3p0.ComboPooledDataSource();
                        try {
                                    dataSource.setDriverClass(env.getProperty("driverClass"));
                        } catch (PropertyVetoException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                        }
                        dataSource.setJdbcUrl(env.getProperty("jdbcUrl"));
                        dataSource.setUser(env.getProperty("user"));
                        dataSource.setPassword(env.getProperty("password"));
                        return dataSource;
            }

}


Write the test class

Because your context is AnnotationConfigAplicationContext Loader should be AnnotationConfigContextLoader.class And Configuratin class should be the class which has @Configuation. If you have more than one configuration files then use

classes = {ApplicationConfig.class,ApplicationConfig1.class }

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = ApplicationConfig.class)
public class BeerDaoImplTest extends AbstractJUnit4SpringContextTests {

            private BeerDao beerDao;

            @Autowired
            protected void setBeerDao(BeerDao beerDao) {
                        this.beerDao = beerDao;
            }

           
}

No comments: