以下部分顯示如何使用注釋來(lái)標(biāo)記依賴關(guān)系為春天
要使用JavaConfig(@Configuration),您需要包括CGLIB庫(kù)。
<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.www.o2fo.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>
創(chuàng)建一個(gè)簡(jiǎn)單的bean如下。
package com.www.o2fo.common; public interface HelloWorld { void printHelloWorld(String msg); }
提供接口的實(shí)現(xiàn)。
package com.www.o2fo.common; public class HelloWorldImpl implements HelloWorld { public void printHelloWorld(String msg) { System.out.println("Hello : " + msg); } }
下面的代碼顯示了如何使用@Configuration告訴Spring這是核心Spring配置文件,并通過(guò)@Bean定義bean。
package com.www.o2fo.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(); } }
為了加載我們的JavaConfig類使用AnnotationConfigApplicationContext。
package com.www.o2fo.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"); } }
使用以下命令運(yùn)行上面的代碼。
mvn exec:java -Dexec.mainClass="com.www.o2fo.common.App"
輸出
兩個(gè)簡(jiǎn)單的Spring bean。
文件:Customer.java
package com.www.o2fo.common; public class Customer { public void printMsg(String msg) { System.out.println("Customer : " + msg); } }
文件:Employee.java
package com.www.o2fo.common; public class Employee { public void printMsg(String msg) { System.out.println("Employee : " + msg); } }
現(xiàn)在,使用JavaConfig @Configuration來(lái)聲明上面的bean。
文件:CustomerConfig.java
package com.www.o2fo.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(); } }
文件:EmployeeConfig.java
package com.www.o2fo.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(); } }
使用@Import加載多個(gè)配置文件。
文件:AppConfig.java
package com.www.o2fo.common; org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({ CustomerConfig.class, EmployeeConfig.class }) public class AppConfig { }
加載主配置文件,并進(jìn)行測(cè)試。
package com.www.o2fo.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"); } }
加載主配置文件,并進(jìn)行測(cè)試。...
更多建議: