Spring Application context using annotation

Spring Application context using annotation

If you want to load your application context using annotation spring 3 and up provide this functionality. Using org.springframework.context.annotation.Configuration you could annotate class and make it your context file in place of doing configuration through XML file

  • For example below XML file is equivalent to ApplicationConfiguration.java class:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="personBean" class="com.javahonk.bean.PersonImpl" />

</beans>

  • ApplicationConfiguration.java class
package com.javahonk;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.javahonk.bean.Person;
import com.javahonk.bean.PersonImpl;

@Configuration
public class ApplicationConfiguration {
    
    @Bean(name = "personBean")
    public Person getName(){
        return new PersonImpl();
    }

}

 

  • Now let see with full example how to load it. Below is maven project structure:

Spring Application context using annotation

  • Create ApplicationConfiguration.java file inside com.javahonk package:
package com.javahonk;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.javahonk.bean.Person;
import com.javahonk.bean.PersonImpl;

@Configuration
public class ApplicationConfiguration {
    
    @Bean(name = "personBean")
    public Person getName(){
        return new PersonImpl();
    }

}

  • Create folder name resources as source folder inside project
  • Create application-config xml inside resources folder
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="personBean" class="com.javahonk.bean.PersonImpl" />

</beans>

  • Create interface Person.java inside com.javahonk.bean package
package com.javahonk.bean;

public interface Person {

    public String getName();
}

  • Implements Person interface as PersonImpl.java
package com.javahonk.bean;

public class PersonImpl implements Person {

    public String getName() {
        return "Java Honk";
    }

}

  • Pom.xml file dependency:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javahonk</groupId>
    <artifactId>SpringConfigurationThroughAnnotation</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringConfigurationThroughAnnotation Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>4.0.3.RELEASE</spring.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>SpringConfigurationThroughAnnotation</finalName>
    </build>
</project>

  • Now crate BeanAnnotationTest to test both configuration:
package com.javahonk;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javahonk.bean.Person;

public class BeanAnnotationTest {

    public static void main(String[] args) {

        
        System.out.println("Load application context through annotation");
        ApplicationContext context = new 
                AnnotationConfigApplicationContext(
                        ApplicationConfiguration.class);
        Person person = (Person) context.getBean("personBean");
        System.out.println("Name: "+person.getName());
        ((ConfigurableApplicationContext)context).close();
        
                
        System.out.println("Load application context through XML file");        
        ClassPathXmlApplicationContext applilcationContext = 
                new ClassPathXmlApplicationContext(
                    "application-config.xml");
        Person person2 = (Person) applilcationContext.getBean("personBean");

        System.out.println("Name: "+person2.getName());
        ((ConfigurableApplicationContext)applilcationContext).close();

    }

}

  • To run test java class right click –. Run As –> Java Application you will see below output as both configuration loaded successfully:

Spring Application context using annotation

 

download Download project source: SpringConfigurationThroughAnnotation

That’s it Spring Application context using annotation

Leave a Reply

Your email address will not be published. Required fields are marked *