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里的
pom.xml
4.0.0
com.warpfuture
springboot
0.0.1-SNAPSHOT
war
org.springframework
spring-webmvc
4.3.7.RELEASE
junit
junit
4.9
test
org.apache.maven.plugins
maven-resources-plugin
UTF-8
org.apache.maven.plugins
maven-compiler-plugin
1.8
UTF-8
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中的
public UserDAO getUserDAO(){
return new UserDAO(); // 直接new对象做演示
}
}
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;
}
}
package com.warpfuture.springboot.javaconfig;
import java.util.ArrayList;
import java.util.List;
/**模拟数据库查询
* @author JMX
*
*/
public class UserDAO {
public List queryUserList(){
List result = new ArrayList();
// 模拟数据库的查询
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 queryUserList() {
// 调用userDAO中的方法进行查询
return this.userDAO.queryUserList();
}
}
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 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:点击打开链接