在Spring的使用中,大量采用xml方式配置类之间的关系太过于繁琐(个人这么认为),而在学习了Spring4后发下使用JavaConfig方式来配置这些关系会更加的简单明了。
测试环境
1. Apache Maven
2. JDK 1.8
3. IDEA 15
先决条件
1. 使用IDEA创建一个Maven项目
2. 在pom.xml 中引用
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
3. 实际代码
很简单的一个Service吧!(这里为了方便省略了interface)
public class FunctionService {
public String sayHello(String msg) {
return "Hello " + msg + "!";
}
}
这里相当于组合上面的类
public class UseFunctionService {
private FunctionService functionService;
public void setFunctionService(FunctionService functionService) {
this.functionService = functionService;
}
public String SayHello(String msg) {
return functionService.sayHello(msg);
}
}
然后重头戏
1. 用@Configuration 表示这是一个spring java配置类
2. 定义了 ApplicationContext中的一个Bean等同于<bean id="functionService" class="com.malin.FunctionService"/>
3. 同2一样
package com.malin.javaconfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //1
public class JavaConfig {
@Bean //2
public FunctionService functionService() {
return new FunctionService();
}
@Bean //3
public UseFunctionService useFunctionService() {
UseFunctionService useFunctionService = new UseFunctionService();
useFunctionService.setFunctionService(functionService());
return useFunctionService;
}
}
在编写一个测试类吧
package com.malin.javaconfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
UseFunctionService service = context.getBean(UseFunctionService.class);
System.out.println(service.SayHello("小马"));
context.close();
}
}
然后输出了结果
D:\software\java\bin\java -Didea.launcher.port=7534 "-Didea.launcher.bin.path=D:\software\IntelliJ IDEA 2016.1.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\software\java\jre\lib\charsets.jar;D:\software\java\jre\lib\deploy.jar;D:\software\java\jre\lib\ext\access-bridge-64.jar;D:\software\java\jre\lib\ext\cldrdata.jar;D:\software\java\jre\lib\ext\dnsns.jar;D:\software\java\jre\lib\ext\jaccess.jar;D:\software\java\jre\lib\ext\jfxrt.jar;D:\software\java\jre\lib\ext\localedata.jar;D:\software\java\jre\lib\ext\nashorn.jar;D:\software\java\jre\lib\ext\sunec.jar;D:\software\java\jre\lib\ext\sunjce_provider.jar;D:\software\java\jre\lib\ext\sunmscapi.jar;D:\software\java\jre\lib\ext\sunpkcs11.jar;D:\software\java\jre\lib\ext\zipfs.jar;D:\software\java\jre\lib\javaws.jar;D:\software\java\jre\lib\jce.jar;D:\software\java\jre\lib\jfr.jar;D:\software\java\jre\lib\jfxswt.jar;D:\software\java\jre\lib\jsse.jar;D:\software\java\jre\lib\management-agent.jar;D:\software\java\jre\lib\plugin.jar;D:\software\java\jre\lib\resources.jar;D:\software\java\jre\lib\rt.jar;D:\workspace\spring-1\target\classes;C:\Users\Administrator\.m2\repository\org\springframework\spring-webmvc\4.1.5.RELEASE\spring-webmvc-4.1.5.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-beans\4.1.5.RELEASE\spring-beans-4.1.5.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-core\4.1.5.RELEASE\spring-core-4.1.5.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-context\4.1.5.RELEASE\spring-context-4.1.5.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-aop\4.1.5.RELEASE\spring-aop-4.1.5.RELEASE.jar;C:\Users\Administrator\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-expression\4.1.5.RELEASE\spring-expression-4.1.5.RELEASE.jar;C:\Users\Administrator\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-web\4.1.5.RELEASE\spring-web-4.1.5.RELEASE.jar;D:\software\IntelliJ IDEA 2016.1.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.malin.javaconfig.Main
六月 26, 2016 4:18:45 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1a6c5a9e: startup date [Sun Jun 26 04:18:45 CST 2016]; root of context hierarchy
Hello 小马!
六月 26, 2016 4:18:45 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1a6c5a9e: startup date [Sun Jun 26 04:18:45 CST 2016]; root of context hierarchy
Process finished with exit code 0