Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Example of Annotation Based Spring Application

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 495
    Comment on it

    With Spring 2.5 it is possible to do all configuration using the annotations. We can configure all dependency injection using the annotations, so no need to define or register your bean to the xml. You can do all wiring configuration on component bean itself using annotation.

    Spring container inject dependency using the annotation first and if not found then it will search for the dependency in the xml configuration.

    By default spring annotation configuration is disabled, you need to enable it explicitly. To enable the annotation driven configuration we just need to write some lines on spring configuration file.Following is the sample of xml configuration :

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <!-- To enable annotation driven scanning -->
       <context:annotation-config/> 
       <!-- bean definitions go here -->
    </beans>
    

    Once context:annotation-config is configured in xml configuration, now you can use the annotation in your application.

    Example :

    Company.java

    package com.evon.example.annotaionDriven;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Company {
        private String companyName="Evon Technology";
    
        public String getCompanyName() {
            return companyName;
        }
        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        } 
    }
    

    Employee.java

    package com.evon.example.annotaionDriven;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Employee {
        private String empName;
        private String department; 
        private Company company;
    
        public Company getCompany() {
            return company;
        }
        @Autowired
        public void setCompany(Company company) {
            this.company = company;
        }
        public String getEmpName() {
            return empName;
        }
        public void setEmpName(String empName) {
            this.empName = empName;
        }
        public String getDepartment() {
            return department;
        }
    
        public void setDepartment(String department) {
            this.department = department;
        }
    }
    

    bean.xml

        <?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:c="http://www.springframework.org/schema/c"
          xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:annotation-config/> 
        <context:component-scan base-package="com.evon.example.annotaionDriven"/>
     </beans>
    

    Main.java

    package com.evon.example.annotaionDriven;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    public class Main {
        public static void main(String[] args) {
            ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/evon/example/annotaionDriven/bean.xml");
            Employee employee = (Employee)context.getBean("employee");
            employee.setEmpName("Sumit");
            employee.setDepartment("Software Development");
            System.out.println("Employee Name : "+employee.getEmpName());
            System.out.println("Department : "+employee.getDepartment());
            System.out.println("Compnay Name : "+employee.getCompany().getCompanyName());
        }
    }
    

    Output :

    Employee Name : Sumit
    Department : Software Development
    Compnay Name : Evon Technology
    

    If you look at the above spring xml configuration file, you find that we have used two configurations, following is the description of those configurations:

    <context:annotation-config/> : This tag will give you the freedom to use annotations in your application. <context:component-scan base-package="" /> : This tag will tell the container that look the configured beans into this directory. So the container scan the base-package, instantiate the bean and inject the dependencies.

    We have lots of annotations to use in Spring application, some of those are @Required, @Autowired, @Qualifier, @Component, @Repository, @Bean etc., You can also use Java standard annotation api(JSR-330) like @Inject, @Resource, @PostConstruct, @PreDestroy etc.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: