Spring 03 整合Junit+Web+SSH
2017-06-16 16:59
253 查看
3 整合Junit
导入jar包
基本 :4+1
测试:spring-test…jar
1.让Junit通知spring加载配置文件
2.让spring容器自动进行注入
修改测试类
4 整合web
0.导入jar包
spring-web.xml
![](https://img-blog.csdn.net/20170616131744911?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
1.tomcat启动加载配置文件
servlet –> init(ServletConfig) –>
filter –> init(FilterConfig) –> web.xml注册过滤器自动调用初始化
listener –> ServletContextListener –> servletContext对象监听【】
spring提供监听器 ContextLoaderListener –> web.xml
如果只配置监听器,默认加载xml位置:/WEB-INF/applicationContext.xml
![](https://img-blog.csdn.net/20170616131815818?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2.确定配置文件位置,通过系统初始化参数
ServletContext 初始化参数 web.xml
3.从servletContext作用域 获得spring容器 (了解)
5 SSH整合
5.1 jar整合
struts:2.3.15.3
hibernate : 3.6.10
spring: 3.2.0
5.1.1 struts
struts-2.3.15.3\apps\struts2-blank\WEB-INF\lib
![](https://img-blog.csdn.net/20170616165759805?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](https://img-blog.csdn.net/20170616165830836?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
模板技术 ,一般用于页面静态化
freemarker:扩展名:*.ftl
velocity :扩展名 *.vm
5.1.2 spring
基础:4+1 , beans、core、context、expression , commons-logging (struts已经导入)
AOP:aop联盟、spring aop 、aspect规范、spring aspect
db:jdbc、tx
测试:test
web开发:spring web
驱动:mysql
连接池:c3p0
整合hibernate:spring orm
![](https://img-blog.csdn.net/20170616165853603?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](https://img-blog.csdn.net/20170616165901774?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
5.1.3 hibernate
%h%\hibernate3.jar 核心
%h%\lib\required 必须
![](https://img-blog.csdn.net/20170616165935618?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
%h%\lib\jpa jpa规范 (java persistent api 持久api),hibernate注解开发 @Entity @Id 等
整合log4j
导入 log4j…jar (struts已经导入)
整合(过渡):slf4j-log4j12-1.7.5.jar
![](https://img-blog.csdn.net/20170616165954494?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
二级缓存
核心:ehcache-1.5.0.jar
依赖:
backport-util-concurrent-2.1.jar
commons-logging (存在)
5.1.4 整合包
spring整合hibernate: spring orm
struts 整合spring:struts2-spring-plugin-2.3.15.3.jar
删除重复jar包
![](https://img-blog.csdn.net/20170616170010556?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
5.2 spring整合hibernate:有hibernate.cfg.xml
5.2.1 创建表
create table t_user(
id int primary key auto_increment,
username varchar(50),
password varchar(32),
age int
);
5.2.2 PO 类
![](https://img-blog.csdn.net/20170616173357776?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGh5eHh4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
javabean
映射文件
5.2.3 dao层
spring提供 HibernateTemplate 用于操作PO对象,类似Hibernate Session对象。
5.2.4 service层
5.2.5 hibernate.cfg.xml
5.2.6 applicationContext.xml
5.2.6.1 添加命名空间
导入jar包
基本 :4+1
测试:spring-test…jar
1.让Junit通知spring加载配置文件
2.让spring容器自动进行注入
修改测试类
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml") public class TestApp { @Autowired //与junit整合,不需要在spring xml配置扫描 private AccountService accountService; @Test public void demo01(){ // String xmlPath = "applicationContext.xml"; // ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); // AccountService accountService = (AccountService) applicationContext.getBean("accountService"); accountService.transfer("jack", "rose", 1000); } }
4 整合web
0.导入jar包
spring-web.xml
1.tomcat启动加载配置文件
servlet –> init(ServletConfig) –>
<load-on-startup>2
filter –> init(FilterConfig) –> web.xml注册过滤器自动调用初始化
listener –> ServletContextListener –> servletContext对象监听【】
spring提供监听器 ContextLoaderListener –> web.xml
<listener><listener-class>....
如果只配置监听器,默认加载xml位置:/WEB-INF/applicationContext.xml
2.确定配置文件位置,通过系统初始化参数
ServletContext 初始化参数 web.xml
<context-param> <param-name>contextConfigLocation <param-value>classpath:applicationContext.xml <!-- 确定配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置spring 监听器,加载xml配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
3.从servletContext作用域 获得spring容器 (了解)
// 从application作用域(ServletContext)获得spring容器 //方式1: 手动从作用域获取 ApplicationContext applicationContext = (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); //方式2:通过工具获取 ApplicationContext apppApplicationContext2 = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
5 SSH整合
5.1 jar整合
struts:2.3.15.3
hibernate : 3.6.10
spring: 3.2.0
5.1.1 struts
struts-2.3.15.3\apps\struts2-blank\WEB-INF\lib
模板技术 ,一般用于页面静态化
freemarker:扩展名:*.ftl
velocity :扩展名 *.vm
5.1.2 spring
基础:4+1 , beans、core、context、expression , commons-logging (struts已经导入)
AOP:aop联盟、spring aop 、aspect规范、spring aspect
db:jdbc、tx
测试:test
web开发:spring web
驱动:mysql
连接池:c3p0
整合hibernate:spring orm
5.1.3 hibernate
%h%\hibernate3.jar 核心
%h%\lib\required 必须
%h%\lib\jpa jpa规范 (java persistent api 持久api),hibernate注解开发 @Entity @Id 等
整合log4j
导入 log4j…jar (struts已经导入)
整合(过渡):slf4j-log4j12-1.7.5.jar
二级缓存
核心:ehcache-1.5.0.jar
依赖:
backport-util-concurrent-2.1.jar
commons-logging (存在)
5.1.4 整合包
spring整合hibernate: spring orm
struts 整合spring:struts2-spring-plugin-2.3.15.3.jar
删除重复jar包
5.2 spring整合hibernate:有hibernate.cfg.xml
5.2.1 创建表
create table t_user(
id int primary key auto_increment,
username varchar(50),
password varchar(32),
age int
);
5.2.2 PO 类
javabean
public class User { private Integer id; private String username; private String password; private Integer age;
映射文件
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.itheima.domain.User" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="username"></property> <property name="password"></property> <property name="age"></property> </class> </hibernate-mapping>
5.2.3 dao层
spring提供 HibernateTemplate 用于操作PO对象,类似Hibernate Session对象。
public class UserDaoImpl implements UserDao { //需要spring注入模板 private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } @Override public void save(User user) { this.hibernateTemplate.save(user); } }
5.2.4 service层
public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void register(User user) { userDao.save(user); } }
5.2.5 hibernate.cfg.xml
<session-factory> <!-- 1基本4项 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///ee19_spring_day03</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">1234</property> <!-- 2 配置方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 3 sql语句 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 4 自动生成表(一般没用) --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 5本地线程绑定 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 导入映射文件 --> <mapping resource="com/itheima/domain/User.hbm.xml"/> </session-factory>
5.2.6 applicationContext.xml
5.2.6.1 添加命名空间
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">[/code]
5.2.6.2 加载hibernate配置文件<!-- 1 加载hibenrate.cfg.xml 获得SessionFactory * configLocation确定配置文件位置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!-- 2创建模板 * 底层使用session,session 有sessionFactory获得 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
5.2.6.3 dao和service<!-- 3 dao --> <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- 4 service --> <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean>
5.2.6.4 事务管理<!-- 5 事务管理 --> <!-- 5.1 事务管理器 :HibernateTransactionManager --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 5.2 事务详情 ,给ABC进行具体事务设置 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="register"/> </tx:attributes> </tx:advice> <!-- 5.3 AOP编程,ABCD 筛选 ABC --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"/> </aop:config>
5.2.7 测试@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml") public class TestApp { @Autowired private UserService userService; @Test public void demo01(){ User user = new User(); user.setUsername("jack"); user.setPassword("1234"); user.setAge(18); userService.register(user); } }
5.3 spring整合hibernate:没有hibernate.cfg.xml 【掌握】
删除hibernate.cfg.xml文件,但需要保存文件内容,将其配置spring中
修改dao层,继承HibernateDaoSupport
5.3.1 修改spring,配置SessionFactory<!-- 1.1加载properties文件 --> <!-- 1.2 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///ee19_spring_day03"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property> </bean> <!-- 1.3配置 LocalSessionFactoryBean,获得SessionFactory * configLocation确定配置文件位置 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 1)dataSource 数据源 2)hibernateProperties hibernate其他配置项 3) 导入映射文件 mappingLocations ,确定映射文件位置,需要“classpath:” ,支持通配符 <property name="mappingLocations" value="classpath:com/itheima/domain/User.hbm.xml"></property> <property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property> mappingResources ,加载执行映射文件,从src下开始 。不支持通配符* <property name="mappingResources" value="com/itheima/domain/User.hbm.xml"></property> mappingDirectoryLocations ,加载指定目录下的,所有配置文件 <property name="mappingDirectoryLocations" value="classpath:com/itheima/domain/"></property> mappingJarLocations , 从jar包中获得映射文件 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">thread</prop> </props> </property> <property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property> </bean>
5.3.2 修改dao,使用HibernateDaoSupport
继承HibernateDaoSupport// 底层需要SessionFactory,自动创建HibernateTemplate模板 public class UserDaoImpl extends HibernateDaoSupport implements UserDao { @Override public void save(User user) { this.getHibernateTemplate().save(user); } }
spring 删除模板,给dao注入SessionFactory<!-- 3 dao --> <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
5.4 struts整合spring:spring创建action
1.编写action类,并将其配置给spring ,spring可以注入service
2.编写struts.xml
3.表单jsp页面
4.web.xml 配置
1.确定配置文件contextConfigLocation
2.配置监听器 ContextLoaderListener
3.配置前端控制器 StrutsPrepareAndExecuteFitler
5.4.1 action类
通用public class UserAction extends ActionSupport implements ModelDriven<User> { //1 封装数据 private User user = new User(); @Override public User getModel() { return user; } //2 service private UserService userService; public void setUserService(UserService userService) { this.userService = userService; }
功能/** * 注册 * @return */ public String register(){ userService.register(user); return "success"; }
5.4.2 spring配置<!-- 6 配置action --> <bean id="userAction" class="com.itheima.web.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean>
5.4.3 struts配置<struts> <!-- 开发模式 --> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- 底层自动从spring容器中通过名称获得内容, getBean("userAction") --> <action name="userAction_*" class="userAction" method="{1}"> <result name="success">/messag.jsp</result> </action> </package> </struts>
5.4.4 jsp表单<form action="${pageContext.request.contextPath}/userAction_register" method="post"> 用户名:<input type="text" name="username"/> <br/> 密码:<input type="password" name="password"/> <br/> 年龄:<input type="text" name="age"/> <br/> <input type="submit" /> </form>
5.4.5 配置web.xml<!-- 1 确定spring xml位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 2 spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 3 struts 前端控制器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5.5 struts整合spring:struts创建action 【】
删除spring action配置
struts<action class="全限定类名"><package name="default" namespace="/" extends="struts-default"> <!-- 底层自动从spring容器中通过名称获得内容, getBean("userAction") --> <action name="userAction_*" class="com.itheima.web.action.UserAction" method="{1}"> <result name="success">/messag.jsp</result> </action> </package>
要求:Action类中,必须提供service名称与 spring配置文件一致。(如果名称一样,将自动注入)
分析:
1. struts 配置文件
default.properties ,常量配置文件
struts-default.xml ,默认核心配置文件
struts-plugins.xml ,插件配置文件
struts.xml,自定义核心配置文件
常量的使用,后面配置项,将覆盖前面的。
2.default.properties ,此配置文件中确定 按照【名称】自动注入
/org/apache/struts2/default.properties
struts-plugins.xml ,struts整合spring<constant name="struts.objectFactory" value="spring" />
struts的action将由spring创建
总结,之后action由spring创建,并按照名称自动注入
6 要求![]()
相关文章推荐
- 关于搭建restful web service + springMVC + ibatis/hibernate的整合和XML配置意思和ssh+springMVC框架搭建
- Web项目开发中 Spring整合Junit单元测试
- [原]Java web学习系列之 Java web开发中的SSH整合(Struts、Spring、Hibernate)(上)
- SSH与SSM学习之Spring10——Spring与junit整合测试
- spring-在web应用中使用spring-test与junit整合
- [原]Java web学习系列之 Java web开发中的SSH整合(Struts、Spring、Hibernate)(下)
- spring_day4_03-ssh整合(struts2整合spring)
- [原]Java web学习系列之 Java web开发中的SSH整合(Struts、Spring、Hibernate)(上)
- SSH整合--联合spring测试hibenate--Junit测试
- SSH整合之org.springframework.web.context.ContextLoaderListener not found问题
- SSH整合过程中出现找不到org.springframework.web.context.ContextLoaderServlet的错误
- SSH整合Spring、Struts、Hibernate、web配置文件
- ssh整合源码一:配置Spring之applicationContext.xml和web.xml
- [原]Java web学习系列之 Java web开发中的SSH整合(Struts、Spring、Hibernate)(下)
- SSH+ maven整合:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- spring(四) 手动整合web项目(SSH)
- 【JavaWeb-25】事务管理相关知识、手动/半自动/自动管理事务案例、整合Junit、整合Web、spring和hibernate整合、struts2和spring整合
- spring---整合Junit,整合web
- ssh整合!Struts.xml、hibernate与spring整合的applicationContext.xml、web.xml文件的配置!