Spring Tutorial - Spring JavaConfig








The following sections show how to use the annotation to mark dependencies for Spring

Spring JavaConfig Dependency

To use JavaConfig (@Configuration), you need to include CGLIB library.

<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.java2s.common</groupId>
  <artifactId>Java2sExamples</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Java2sExamples</name>
  <url>http://maven.apache.org</url>
  <properties>
    <spring.version>3.0.5.RELEASE</spring.version>
  </properties>  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
<!-- Spring 3 dependencies -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <!-- JavaConfig need this library -->
  <dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
  </dependency>
  </dependencies>
</project>




Java Bean

Create a a simple bean as follows.

package com.java2s.common;

public interface HelloWorld {

  void printHelloWorld(String msg);

}

Provide implementation for the interface.

package com.java2s.common;

public class HelloWorldImpl implements HelloWorld {

  public void printHelloWorld(String msg) {

    System.out.println("Hello : " + msg);
  }

}

The following code shows how to use use @Configuration to tell Spring that this is the core Spring configuration file, and define bean via @Bean.

package com.java2s.common;

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

@Configuration
public class AppConfig {

    @Bean(name="helloBean")
    public HelloWorld helloWorld() {
        return new HelloWorldImpl();
    }
}




Main method

In order to load the our JavaConfig class use AnnotationConfigApplicationContext.

package com.java2s.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
  public static void main(String[] args) {

            ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      HelloWorld obj = (HelloWorld) context.getBean("helloBean");

      obj.printHelloWorld("Spring3 Java Config");

  }
}

Run the code above with the following command.

mvn exec:java -Dexec.mainClass="com.java2s.common.App"  

Output



Download Java2s_Spring_JavaConfig.zip

Spring 3 JavaConfig @Import example

Two simple Spring beans.

File : Customer.java

package com.java2s.common;

public class Customer {

  public void printMsg(String msg) {

    System.out.println("Customer : " + msg);
  }

}

File : Employee.java

package com.java2s.common;
public class Employee {
  public void printMsg(String msg) {
    System.out.println("Employee : " + msg);
  }
}

Now, use JavaConfig @Configuration to declare above beans.

File : CustomerConfig.java

package com.java2s.common;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomerConfig {
  @Bean(name="customer")
  public Customer customer(){
       return new Customer();
  }
}

File : EmployeeConfig.java

package com.java2s.common;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EmployeeConfig {
  @Bean(name="employee")
  public Employee employee(){
       return new Employee();
  }
}

Use @Import to load multiple configuration files.

File : AppConfig.java

package com.java2s.common;
org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ CustomerConfig.class, EmployeeConfig.class })
public class AppConfig {
}

Load the main configuration file , and test it.

package com.java2s.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.java2s.config.AppConfig;
public class App {
  public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(
        AppConfig.class);
    Customer customer = (Customer) context.getBean("customer");
    customer.printMsg("Hello 1");
    Employee employee = (Employee) context.getBean("employee");
    employee.printMsg("Hello 2");
  }
}

Output