<!-- web.xml核心代码 -->
<!-- 初始化webApplicationContext(相当于beanfactory),
listener将其放入了servletcontext中 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置struct的拦截器 -->
<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>
//hibernate提供的 API
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.bigjava.bean.TTestPaper;
public class HibernateTest {
public static void main(String[] args) {
Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
Session s = factory.openSession();
Transaction tx = s.beginTransaction();
s.save(new TTestPaper("1","1",1,"1"));
tx.commit();
s.close();
factory.close();
}
}
<!-- spring的核心配置 -->
<!-- 配置transactionManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义事务处理规则 -->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置AOP -->
<aop:config proxy-target-class="true">
<!--定义切面 -->
<aop:pointcut expression="execution(* com.bigjava.dao.*.*(..))" id="point" />
<!--定义织入点规则 -->
<aop:advisor advice-ref="tx" pointcut-ref="point" />
</aop:config>
</beans>
/*springTest*/
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import com.bigjava.dao.TTestPaperDao;
public class SpringTest {
private TTestPaperDao tTestPaperDao;
public void settTestPaperDao(TTestPaperDao tTestPaperDao) {
this.tTestPaperDao = tTestPaperDao;
}
public void test(){
System.out.println("list--size-->"+tTestPaperDao.findall().size());
}
public static void main(String[] args) {
//类似于sessionfactory
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
SpringTest test=(SpringTest)context.getBean("springTest");
test.test();
}
}
<!-- structs核心配置 -->
<struts>
<!-- 让spring来管理struct2中的action -->
<constant name="struts.objectFactory" value="spring" />
<package name="com.test" namespace="/test" extends="struts-default" >
<action name="StructTest" class="structTest">
<result name="success">/welcome.jsp</result>
<result name="hello">/welcome1.jsp</result>
</action>
<action name="ExcelContrlloer" class="com.test.ExcelContrlloer">
<result name="excletest" type="xslt">/welcome.jsp</result>
</action>
</package>
</struts>
/*structTest*/
package com.test;
import org.springframework.stereotype.Controller;
import com.bigjava.bean.TTestPaper;
import com.bigjava.dao.TTestPaperDao;
import com.opensymphony.xwork2.ActionSupport;
@Controller("structTest")
public class StructTest extends ActionSupport{
private Login p=new Login();
@Override
public String execute() throws Exception {
//System.out.println("actiontest success...");
return "";
}
public String StructTest() throws Exception {
/*
* 如同普通的servlet一样,得到beanfactory
HttpServletRequest request = ServletActionContext.getRequest();
ServletContext sc=request.getSession().getServletContext();
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
tTestPaperDao = (TTestPaperDao) ac.getBean("tTestPaperDao");
*/
//System.out.println("size--->"+tTestPaperDao.findall().size());
System.out.println(p.getPaperId()+"fdfd"+p.getPaperName());
return SUCCESS;
}
public String StructTest1() throws Exception {
System.out.println("size--->"+tTestPaperDao.findall().size());
return "hello";
}
private TTestPaperDao tTestPaperDao;
public void settTestPaperDao(TTestPaperDao tTestPaperDao) {
this.tTestPaperDao = tTestPaperDao;
}
}