在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:softwarejavainjava -Didea.launcher.port=7534 "-Didea.launcher.bin.path=D:softwareIntelliJ IDEA 2016.1.3in" -Dfile.encoding=UTF-8 -classpath "D:softwarejavajrelibcharsets.jar;D:softwarejavajrelibdeploy.jar;D:softwarejavajrelibextaccess-bridge-64.jar;D:softwarejavajrelibextcldrdata.jar;D:softwarejavajrelibextdnsns.jar;D:softwarejavajrelibextjaccess.jar;D:softwarejavajrelibextjfxrt.jar;D:softwarejavajrelibextlocaledata.jar;D:softwarejavajrelibext ashorn.jar;D:softwarejavajrelibextsunec.jar;D:softwarejavajrelibextsunjce_provider.jar;D:softwarejavajrelibextsunmscapi.jar;D:softwarejavajrelibextsunpkcs11.jar;D:softwarejavajrelibextzipfs.jar;D:softwarejavajrelibjavaws.jar;D:softwarejavajrelibjce.jar;D:softwarejavajrelibjfr.jar;D:softwarejavajrelibjfxswt.jar;D:softwarejavajrelibjsse.jar;D:softwarejavajrelibmanagement-agent.jar;D:softwarejavajrelibplugin.jar;D:softwarejavajrelib esources.jar;D:softwarejavajrelib t.jar;D:workspacespring-1 argetclasses;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-webmvc4.1.5.RELEASEspring-webmvc-4.1.5.RELEASE.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-beans4.1.5.RELEASEspring-beans-4.1.5.RELEASE.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-core4.1.5.RELEASEspring-core-4.1.5.RELEASE.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-context4.1.5.RELEASEspring-context-4.1.5.RELEASE.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-aop4.1.5.RELEASEspring-aop-4.1.5.RELEASE.jar;C:UsersAdministrator.m2 epositoryaopallianceaopalliance1.0aopalliance-1.0.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-expression4.1.5.RELEASEspring-expression-4.1.5.RELEASE.jar;C:UsersAdministrator.m2 epositorycommons-loggingcommons-logging1.2commons-logging-1.2.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-web4.1.5.RELEASEspring-web-4.1.5.RELEASE.jar;D:softwareIntelliJ IDEA 2016.1.3libidea_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