Spring再接触 注入类型
2021-07-04 11:07
标签:ret service return ice work private ack exception spring 共有三种注入类型 一种是set注入 一种是构造注入 一种是接口注入 最常用的还是set 现在看一下construct 构造注入 在userservice中加入 XML中 测试代码 这样也是可以成功注入impl的 Spring再接触 注入类型 标签:ret service return ice work private ack exception spring 原文地址:https://www.cnblogs.com/frankzone/p/9613077.htmlpackage com.bjsxt.service;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserService {
private UserDAO userDAO;
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public UserService(UserDAO userDAO) {
super();
this.userDAO = userDAO;
}
}
"1.0" encoding="UTF-8"?>
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
User u = new User();
u.setUsername("zhangsan");
u.setPassword("zhangsan");
service.add(u);
}
上一篇:04 Python数据类型
下一篇:Spring再接触 IOC DI