ApplicationContext.xml配置

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:context="http://www.springframework.org/schema/context"
  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 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
  11. <!-- 读取db.properties文件 -->
  12. <context:property-placeholder location="classpath:db.properties" />
  13. <!-- 开启扫描类中的注解 -->
  14. <context:component-scan base-package="cn.itcast"></context:component-scan>
  15. <!-- 配置c3p0连接池 -->
  16. <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
  17. <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
  18. <property name="driverClass" value="${jdbc.driverClass}" ></property>
  19. <property name="user" value="${jdbc.user}" ></property>
  20. <property name="password" value="${jdbc.password}" ></property>
  21. </bean>
  22. <!-- 核心事务管理器 -->
  23. <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
  24. <property name="sessionFactory" ref="sessionFactory" ></property>
  25. </bean>
  26. <tx:annotation-driven transaction-manager="transactionManager" />
  27. <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
  28. <property name="dataSource" ref="dataSource" ></property>
  29. <property name="hibernateProperties">
  30. <props>
  31. <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
  32. <!-- 可选配置 -->
  33. <prop key="hibernate.show_sql" >true</prop>
  34. <prop key="hibernate.format_sql" >true</prop>
  35. <prop key="hibernate.hbm2ddl.auto" >update</prop>
  36. </props>
  37. </property>
  38. <property name="mappingDirectoryLocations" value="classpath:cn/itcast/domain" ></property>
  39. </bean>
  40. </beans>

开启注解

  1. <!-- 开启扫描类中的注解 -->
  2. <context:component-scan base-package="(项目的包的位置))"></context:component-scan>

Dao层

有技术含量一点的是在Dao层注入SessionFactory,就是下面这几句话

  1. @Resource(name="sessionFactory")
  2. public void setSF(SessionFactory sf){
  3. super.setSessionFactory(sf);
  4. }

注册加注入
image.png

Service层

注册@Service(“serviceName”) ,放到类上面。
注入:

  1. @Resource(name="customerDao")
  2. private CustomerDao cd;

事务:在service层的类上面直接加。

  1. @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)

Action层

image.png
注册:@Controller(“actionName”)
注入:

  1. @Resource(name="customerService")
  2. private CustomerService cs;

多例:@Scope(“prototype”)