Spring在4.x版本后支持用java的方式配置,今天要写的是有关Spring的Java配置的Demo.
测试环境:
1.Windows 7 旗舰版 Service Pack 1
2.Eclipse Oxygen.1a Release (4.7.1a)
1.新建一个meavn项目,war包工程,如果pom.xml里的<packaging>war</packaging>报错,工程中缺少了web.xml文件,项目名称-->右键-->Java EE Tools-->generate deployment descriptor stub.即可解决.
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.warpfuture</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.新建SpringConfig.java配置类,以及User.java, UserDao.java, UserService.java以及测试类UserTest.java
SpringConfig.java
package com.warpfuture.springboot.javaconfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**Spring的java配置方式
* @author JMX
*
*/
@Configuration //通过该注解来表明该类是一个Spring的配置,相当于一个xml文件
@ComponentScan(basePackages = "com.warpfuture.springboot.javaconfig") //配置扫描包
public class SpringConfig {
@Bean // 通过该注解来表明是一个Bean对象,相当于xml中的<bean>
public UserDAO getUserDAO(){
return new UserDAO(); // 直接new对象做演示
}
}
User.java
package com.warpfuture.springboot.javaconfig;
/** User对象
* @author JMX
*
*/
public class User {
private String username;
private String password;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
UserDao.java
package com.warpfuture.springboot.javaconfig;
import java.util.ArrayList;
import java.util.List;
/**模拟数据库查询
* @author JMX
*
*/
public class UserDAO {
public List<User> queryUserList(){
List<User> result = new ArrayList<User>();
// 模拟数据库的查询
for (int i = 0; i < 5; i++) {
User user = new User();
user.setUsername("username=" + i);
user.setPassword("password=" + i);
user.setAge(i + 1);
result.add(user);
}
return result;
}
}
UserService.java
package com.warpfuture.springboot.javaconfig;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**模拟Service层
* @author JMX
*
*/
@Service
public class UserService {
@Autowired // 注入Spring容器中的bean对象
private UserDAO userDAO;
public List<User> queryUserList() {
// 调用userDAO中的方法进行查询
return this.userDAO.queryUserList();
}
}
UserTest.java
package com.warpfuture.springboot.test;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.warpfuture.springboot.javaconfig.SpringConfig;
import com.warpfuture.springboot.javaconfig.User;
import com.warpfuture.springboot.javaconfig.UserService;
public class UserTest {
private AnnotationConfigApplicationContext context=null;
private UserService userService=null;
@Before
public void before() {
// 通过Java配置来实例化Spring容器
context= new AnnotationConfigApplicationContext(SpringConfig.class);
// 在Spring容器中获取Bean对象
userService= context.getBean(UserService.class);
}
@Test
public void test() {
List<User> list = userService.queryUserList();
for (User user : list) {
System.out.println(user.getUsername() + ", " + user.getPassword() + ", " + " age="+user.getAge());
}
}
@After
public void after() {
context.destroy();
}
}
3.运行测试类UserTest.java里的test()方法,控制台打印
完美测试通过
项目Demo:点击打开链接