Spring再接触 模拟Spring
2021-07-04 14:04
标签:对象 .com for obj hibernate zha dep loader port 项目分层: 1.最土的方法是直接写到main中去 2。分出model层 2.如下 4.在抽象一个对数据库的访问层(跨数据库实现) 面向抽象编程 User.java UserService.java UserDAOimpl BeanFactory ClasspathxmlApplicationContext.java beans.xml testadd Spring再接触 模拟Spring 标签:对象 .com for obj hibernate zha dep loader port 原文地址:https://www.cnblogs.com/frankzone/p/9612720.htmlpackage com.bjsxt.model;
public class User {
private String username;
private String password;
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;
}
}
package 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;
}
}
package com.bjsxt.dao.impl;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserDAOImpl implements UserDAO {
public void save(User user) {
//Hibernate
//JDBC
//XML
//NetWork
System.out.println("user saved!");
}
}
package com.bjsxt.spring;
public interface BeanFactory {
public Object getBean(String id);
}
package com.bjsxt.spring;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class ClassPathXmlApplicationContext implements BeanFactory {
private Map
@Test
public void testAdd() throws Exception {
BeanFactory applicationContext = new ClassPathXmlApplicationContext();
UserService service = (UserService)applicationContext.getBean("userService"); //模拟Spring自动帮装配了
//UserService service = new Userservice();
//Userdao userdao = new userdaoimpl(); 编程起来方便,低耦合
User u = new User();
u.setUsername("zhangsan");
u.setPassword("zhangsan");
service.add(u);
}
上一篇:python 可视化界面
文章标题:Spring再接触 模拟Spring
文章链接:http://soscw.com/index.php/essay/101749.html