Spring教程 - Spring JavaConfig
2018-01-09 19:06 更新
Spring教程 - Spring JavaConfig
以下部分显示如何使用注释来标记依赖关系为春天
Spring教程 - Spring JavaConfig...
要使用JavaConfig(@Configuration),您需要包括CGLIB库。
<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.w3cschool.cnmon</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
创建一个简单的bean如下。
package com.www.w3cschool.cnmon; public interface HelloWorld { void printHelloWorld(String msg); }
提供接口的实现。
package com.www.w3cschool.cnmon; public class HelloWorldImpl implements HelloWorld { public void printHelloWorld(String msg) { System.out.println("Hello : " + msg); } }
下面的代码显示了如何使用@Configuration告诉Spring这是核心Spring配置文件,并通过@Bean定义bean。
package com.www.w3cschool.cnmon; 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.w3cschool.cnmon; 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"); } }
使用以下命令运行上面的代码。
mvn exec:java -Dexec.mainClass="com.www.w3cschool.cnmon.App"
输出
Download Java2s_Spring_JavaConfig.zip
Spring 3 JavaConfig @Import示例
两个简单的Spring bean。
文件:Customer.java
package com.www.w3cschool.cnmon; public class Customer { public void printMsg(String msg) { System.out.println("Customer : " + msg); } }
文件:Employee.java
package com.www.w3cschool.cnmon; public class Employee { public void printMsg(String msg) { System.out.println("Employee : " + msg); } }
现在,使用JavaConfig @Configuration来声明上面的bean。
文件:CustomerConfig.java
package com.www.w3cschool.cnmon; 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.w3cschool.cnmon; 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加载多个配置文件。
文件:AppConfig.java
package com.www.w3cschool.cnmon; org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({ CustomerConfig.class, EmployeeConfig.class }) public class AppConfig { }
加载主配置文件,并进行测试。
package com.www.w3cschool.cnmon; 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"); } }
加载主配置文件,并进行测试。...
以上内容是否对您有帮助:
更多建议: