web.xml配置请参考
http://xiaofancn.iteye.com/admin/blogs/986469
applicationContext.xml(我放在了WEB-INF目录下了)
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- annotation on -->
<context:annotation-config />
<context:component-scan base-package="org.snailteam"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/db?useUnicode=true&amp;amp;characterEncoding=UTF-8">
</property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>model.Product</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="*" read-only="false" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* org.snailteam.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
</aop:config>
</beans>
struts.xml配置可就清爽多了
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.multipart.saveDir" value="d://" />
<constant name="struts.action.extension" value="do,action" />
<!-- 全局变量 -->
<package name="global" namespace="/" extends="struts-default">
<global-results>
<result name="error">/WEB-INF/pages/error.jsp</result>
<result name="index">/index.jsp</result>
<result name="adminLogin">/admin/login.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="Exception"></exception-mapping>
</global-exception-mappings>
</package>
</struts>
持久层类ProductDaoImpl.java
package org.snailteam.dao;
import javax.annotation.Resource;
import model.Product;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
@Repository("productDao")
public class ProductDaoImpl implements ProductDao {
private HibernateTemplate hibernateTemplate;
@Resource(name = "hibernateTemplate")
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void save(Product p) {
this.hibernateTemplate.save(p);
}
}
业务层ProductServiceImpl.java
package org.snailteam.service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import model.Product;
import org.snailteam.dao.ProductDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service("productService")
public class ProductServiceImpl implements ProductService{
private ProductDao dao;
@Resource(name="productDao")
public void setDao(ProductDao dao) {
this.dao = dao;
}
//@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public void add(Product p) {
dao.save(p);
}
@PostConstruct//对象构造之后
public void init(){
}
@PreDestroy//对象销毁之前
public void destroy(){
}
}
表现层ProductAction.java
package org.snailteam.action;
import javax.annotation.Resource;
import model.Product;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.snailteam.service.ProductService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller("productBean")
@Scope("prototype")
@Namespace("/prod")
@ParentPackage("global")
@Results( { @Result(name = "success", location = "/index.jsp") })
public class ProductAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private ProductService service;
private Product prod;
public String add() {
service.add(prod);
return SUCCESS;
}
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}
@Resource(name = "productService")
public void setService(ProductService service) {
this.service = service;
}
public Product getProd() {
return prod;
}
public void setProd(Product prod) {
this.prod = prod;
}
}
测试代码
public class ProductActionTest {
public static ProductAction action;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
action = (ProductAction) ac.getBean("productBean");
}
@Test
public void testSave() throws Exception {
action.setProd(new Product("产品介绍信息"));
System.out.println(action.add());
}
}