spring 学习总结
前几天,一直在学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()); }
spring 学习总结的更多相关文章
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- Spring学习之第一个AOP程序
IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...
- MyEclipse Spring 学习总结三 SpringMVC
MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...
- Spring学习 Ioc篇(一 )
一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...
- Spring学习(三)——Spring中的依赖注入的方式
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring学习(二)——Spring中的AOP的初步理解[转]
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- Spring学习8-Spring事务管理
http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
随机推荐
- component to string
component to string string to component ObjectTextToBinary ObjectBinaryToText ReadComponent #include ...
- HDU1007最近点对(分治)
http://acm.hdu.edu.cn/showproblem.php?pid=1007 直接见代码吧.不过这个是N*logN*logN的 尽管如此,我怎么感觉我的比他们的还快??? #inclu ...
- 用完成例程(Completion Routine)实现的重叠I/O模型
/// 用完成例程(Completion Routine)实现的重叠I/O模型 /// 异步IO模型 /// 用完成例程来实现重叠I/O比用事件通知简单得多.在这个模型中,主线程只用不停的接受连接 / ...
- CreateWaitableTimer和SetWaitableTimer函数(定时器)
用户感觉到软件的好用,就是可以定时地做一些工作,而不需要人参与进去.比如每天定时地升级病毒库,定时地下载电影,定时地更新游戏里的人物.要想 实现这些功能,就可以使用定时器的API函数CreateWai ...
- iOS国际化多语言设置
一.创建工程.添加语言
- 解决sqlserver使用IP无法连接的问题,用localhost或者‘“.”可以连接
今天装了一个mssql发现用ip无法连接但是用localhost和“.”却可以连接,纠结了一天终于找到了问题的解决办法: 打开mssql配置管理器(我点电脑---->右键选择管理--->服 ...
- position:sticky用法
用户的屏幕越来越大,而页面太宽的话会不宜阅读,所以绝大部分网站的主体宽度和之前相比没有太大的变化,于是浏览器中就有越来越多的空白区域,所以你可能注意到很多网站开始在滚动的时候让一部分内容保持可见,比如 ...
- cocos2dx shader实现灰度图android后台切换回来导致图像偏移的问题
转自:http://www.tuicool.com/articles/U3URRrI 项目中经常会遇到将一张图像处理成灰色的需求,为了节省资源,一般不会让美术再做一套同样的灰度图,通常会通过代码处理让 ...
- Flex圆角矩形
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...
- HRESULT 0x80131515 解决方法
http://254698001.blog.51cto.com/2521548/1339696 很多朋友在加载DLL是发生这样的错误:HRESULT: 0x80131515. 解决方法是,在DLL文件 ...