1、把用到的Spring包拷贝到lib目录下
2、新建配置文件 applicationContext.xml
加入<context: compontent-scan></context>标签
<!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->
<context:component-scan base-package="pan.dao,pan.service" />
3、编写测试类
3-1新建 UserServiceI.java接口
public interface UserServiceI {
public void test();
}
3-2新建 UserServiceImpl 实现类
//使用 annotation 自动注册bean,并检查@Service已被注入
@Service(value="userService")
public class UserServiceImpl implements UserServiceI {
@Override
public void test() {
System.out.println("Success!");
}
}
3-3新建测试类SpringTest.java
public class SpringTest {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"resources/spring/applicationContext.xml"});
UserServiceI user=(UserServiceI) ac.getBean("userService");
user.test();
}
}
4、Spring在Web.xml中的配置
<!-- Spring的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoad erListener
</listener-class>
</listener>
到此Spring配置完毕
1、添加struts依赖的jar包
2、配置struts.xml
<struts>
<!-- 开启使用开发模式,详细错误提示 -->
<constant name="struts.devMode" value="true"/>
<!-- 指定资源编码 -->
<contant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 默认后缀名 -->
<constant name="struts.action.extension" value="action"/>
<!-- 将对象交给spring管理 -->
<contant name="struts.objectFactory" value="spring"/>
<!-- 设置Web应用的默认Locale为zh_CN -->
<contant name="struts.locale" value="zh_CN"/>
<!-- struts配置文件更改后是否重新加载 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="true"/>
<package name=”pan-default” extends=”struts-default”
namespace=”/”></package>
</struts>
3、配置web.xml
<!-- 配置struts -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,resources/struts/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</filter-mapping>
4、编写测试方法(测试web)
public class UserAction {
public String test(){
return "success";
}
}
在struts.xml里添加
<struts>
<package name="admin" namespace="/a" extends="pan-default">
<!-- action测试 -->
<action name="user" class="pan.action.UserAction" method="test">
<result name="success">/WEB-INF/admin/test.jsp</result>
</action>
</package>
</struts>
测试结果:
5.1、编写测试方法(测试Spring 和 Struts的结合是否成功 方法1)
public String test2(){
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext、 (ServletActionContext.getServletContext());
UserServiceI userService = (UserServiceI)ac.getBean("userService");
userService.test();
return "success";
}
5.2、(测试Spring 和 Struts的结合是否成功 方法2)
增加属性 UserServiceI
private UserServiceI userService;
public UserServiceI getUserService() {
return userService;
}
@Autowired
public void setUserService(UserServiceI userService) {
this.userService = userService;
}
测试结果 :
1、导入Hibernate4的核心jar包
2、配置applicationContext.xml(因为我的Hibernate和Spring的配置都写在了该文件中)
<!-- Hibernate数据源的配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- 指定连接数据库的URL -->
<property name="url"
value="jdbc:mysql://localhost:3306/examonline" />
<!-- 指定连接数据库的用户名 -->
<property name="username" value="root" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="root" />
<!-- 指定连接数据库的连接池的初始化大小 -->
<property name="initialSize" value="5" />
<!-- 指定连接数据库的连接池最大连接数 -->
<property name="maxActive" value="100" />
<!-- 指定连接数据库的连接池最大空闲时间 -->
<property name="maxIdle" value="30" />
<!-- 指定连接数据库的连接池最大等待时间 -->
<property name="maxWait" value="1000" />
</bean>
<!-- 配置hibernate session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="cache.region.factory_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="cache.use_query_cache">true</prop>
<prop key="cache.use_second_level_cache">true</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 实体扫描路径 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan" value="pan.model"/><!-路径一定要对,我在这儿栽了->
</bean>
<!-- 配置事务管理 -->
<!-- 声明一个事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 拦截器方式配置事务 切面方式 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<!-- read-only method-->
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="count*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="mainServiceMethods" expression="execution(* pan.main.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="mainServiceMethods" />
</aop:config>
红色字体为须修改的地方,其它部分可不变。
3、编写测试类和方法
model实体类
@Entity
@Table(name="t_user")
@DynamicInsert(true)
@DynamicUpdate(true)
@SuppressWarnings("serial")
public class Tuser implements Serializable{
private String id;
/*
* 用户名
*/
private String name;
/*
* 密码
*/
private String password;
/*
* 创建时间
*/
private Date createdatetime;
/*
* 修改时间
*/
private Date modifydatetime;
@Id
@Column(name="id", length=36, nullable=false)
public String getId() {
return id;
}
@Column(name="name", unique=true, length=64, nullable=false)
public String getName() {
return name;
}
@Column(name="password", length=64, nullable=false)
public String getPassword() {
return password;
}
@Column(name="createdatetime")
public Date getCreatedatetime() {
if(this.createdatetime != null){
return createdatetime;
}
return new Date();
}
@Column(name="modifydatetime")
public Date getModifydatetime() {
if(this.modifydatetime != null){
return modifydatetime;
}
return new Date();
}
(......省略set方法)
UserDaoI.java
@Repository("userDao")
public interface UserDaoI {
public Serializable save(Tuser tuser);
}
-------------------------------------------------------------
UserDaoImpl.java
@Repository("userDao")
public class UserDaoImpl implements UserDaoI {
private SessionFactory sessionFactory;
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public Serializable save(Tuser tuser) {
return this.sessionFactory.getCurrentSession().save(tuser);
}
}
UserServiceI.java
@Repository("userDao")
public interface UserDaoI {
public Serializable save(Tuser tuser);
}
------------------------------------------------------------
UserServiceImpl.java
@Service(value="userService")//使用 annotation 自动注册bean,并检查@Service已被注入
public class UserServiceImpl implements UserServiceI {
private UserDaoI userDao;
@Resource
public void setUserDao(UserDaoI userDao) {
this.userDao = userDao;
}
@Override
public Serializable save(Tuser tuser) {
return userDao.save(tuser);
}
}
HibernateTest测试方法:
public class HibernateTest {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"classpath:resources/spring/applicationContext.xml"});
UserServiceI userService = (UserServiceI) ac.getBean("userService");
Tuser tuser = new Tuser();
tuser.setId(UUID.randomUUID().toString());
tuser.setName("admin");
tuser.setPassword("23341");
tuser.setCreatedatetime(new Date());
tuser.setModifydatetime(new Date());
userService.save(tuser);
}
}
测试结果 :
到此Struts2、Hibernate4和Spring3整合完毕。
(配置过程中,可能需要导入依赖的jar包,根据控制台提示信息导入即可。)