商城项目加入spring框架
1.加入相应的包:
spring所需要的包还是挺多的.除了spring自己的包,还需要aopalliance....aspect...cglib...Commons-logging...Commons-pool...Commons-dhcp...
2.在web.xml中的struts2的filter前加入下面两句:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <listener> 2 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 3 </listener> 4 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:bean.xml</param-value> 8 </context-param>
3.引入bean.xml..另外为了把Hibernate也交给spring管理,所以把Hibernate.cfg.xml删掉:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 15 16 <context:annotation-config/> <!-- 允许使用annotation --> 17 <context:component-scan base-package="com.shopping" /> 18 19 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 20 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 21 <property name="url" value="jdbc:mysql://localhost:3306/shopping"/> 22 <property name="username" value="root"/> 23 <property name="password" value="1234"/> 24 <property name="defaultAutoCommit" value="false" /> 25 </bean> 26 27 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 28 <property name="dataSource" ref="myDataSource"/> 29 <!-- 30 <property name="annotatedClasses"> 31 <list> 32 <value>com.model.User</value> 33 <value>com.model.Log</value> 34 </list> 35 </property> 36 --> 37 <property name="packagesToScan"> 38 <list> 39 <value>com.shopping.model</value> 40 </list> 41 </property> 42 43 <property name="hibernateProperties"> 44 <props> 45 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 46 <prop key="hibernate.show_sql">true</prop> 47 <prop key="hibernate.format_sql">true</prop> 48 <prop key="hibernate.hbm2ddl.auto">update</prop> 49 <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> 50 </props> 51 </property> 52 </bean> 53 54 <bean id="txManager" 55 class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 56 <property name="sessionFactory" ref="sessionFactory"/> 57 </bean> 58 <tx:annotation-driven transaction-manager="txManager"/> 59 60 61 <!-- <aop:aspectj-autoproxy /> 允许使用aop 的annotation --> 62 <!-- 63 <bean id="u" class="com.dao.impl.userDAOImpl"> 64 </bean> 65 66 <bean id="userService" class="com.service.userService" init-method="initial" destroy-method="destroy"> 67 --> 68 69 <!-- <property name="ud" ref="u" /> --> 70 <!-- </bean> --> 71 72 73 </beans>
4.IOC..将所需的容器全部加上@Component..并在相应的set方法加上@Resource..
5.若需要用到日志输出..可以使用aop的切入..