前几天,一直在学spring,然后又学习spring mvc ,再回过头去看spring,本来不是特别熟悉,竟然几乎全部忘记了。于是,立刻写这篇博客来总结一下。这是我写的其中一个程序,大概的逻辑流程。
注意:导入前两个jar包
1.这个是项目的总体结构。
2.web.xml 配置文件
是自动生成的,没有改,这里主要使用junit4 进行测试了,没有涉及到页面的交互。
3.springmvc-servlet.xml配置文件
<!-- xsl -->
<!--xml能引入多个 dtd (之前) xsd 一般称为scheme 一般称为 定义的xml的语法 元数据文件 -->
<!-- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> -->
<!-- <bean id="u" class="com.dao.impl.UserDAOImpl"> -->
<bean id="userDAO" class="com.dao.impl.UserDAOImpl">
<!-- <property name="daoId"></property> -->
<!-- <property name="daoStatus" value="good"></property> -->
<property name="daoId" value="1"></property> //userdaoImple实现了userdao的接口,在里面有个daoId属性,spring 框架做的事情,就是把在这里赋的值
传到类里。用下面的方法赋给了类。
public void setDaoId(int daoId) {
this.daoId = daoId;
}
此为userdaoimple 里的set,get方法
</bean>
<bean id="userDAO1" class="com.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="com.service.UserService">
</bean>
3.此为userdaoimple方法(省略set,get方法)
public class UserDAOImpl implements UserDAO{
private int daoId;
private String daoStatus;
private Set<String> sets;
private List<String> lists;
private Map<String,String> maps;
4.此为user类方法
public class User {
private String username;
private String password;
public
5.此为userdao接口方法
public interface UserDAO {
public void save(User u);
}
6.此为service类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.dao.UserDAO;
import com.model.User;
//功能 存入数据库之前 业务逻辑 认证
public class UserService {
private UserDAO userDAO;
/*public UserService(UserDAO userDAO){
super();
this.userDAO=userDAO;
}*/
//private UserDAO userDAO =new UserDAOImpl();
//在实现中可以写多个 oracle、mysql, 用哪个new 谁 面向抽象编程
public void init(){
System.out.println("init");
}
public UserDAO getUserDAO() {
return userDAO;
}
@Autowired //必须有参数为空的构造方法
//autowire="by type"
public void setUserDAO(@Qualifier("userDAO")UserDAO userDAO) {
this.userDAO = userDAO;
}
//@Qualifier 可以指定到底哪个bean
public void add(User user){
userDAO.save(user);
}
public void destroy(){
System.out.println("destroy");
}
7.junit test包
新建一个包test,然后在service包下选择userService类,右击新建junit test cast 文件
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.model.User;
public class UserServiceTest {
@Test
public void test() {
fail("Not yet implemented");
}
public void testAdd() {
ApplicationContext ctx =new ClassPathXmlApplicationContext("springmvc-servlet.xml"); //此处规定了,映射了springmvc-servlet.xml 文件
//UserDAO u1=(UserDAO)ctx.getBean("userDAO");
// System.out.println(u1);
/* UserService service =(UserService)ctx .getBean("userService");
UserService service2 =(UserService)ctx .getBean("userService");
*/
//System.out.println(service==service2);
//UserService service =new UserService();
/* User u=new User();
u.setUsername("zhang");
u.setPassword("zhang");
service.add(u);*/
UserService service =(UserService)ctx.getBean("userService");
service.add(new User()); //服务里new 一个user() 对象。 印象中spring 框架,它把new 对象这个事情 做了,不知道理解的对不对
System.out.println(service.getUserDAO());
}