在Spring中引用属性文件:
    优点:
        1.防止随意更改jdbc的连接
        2.给不懂代码的人使用
    步骤:
        1.数据库连接信息写在属性文件中
        范例:#jdbc.properties

  jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost/db
jdbc.username = root
jdbc.password = root

2.采用PropertyPlaceholderConfigurer可以引入属性文件,在Spring配置文件中采用诸如${jdbc.url}的方式引用属性值

3.PropertyPlaceholderConfigurer的配置用到了List类型属性
        
Spring自动装配:
    1.定义:Spring可以根据属性类型、名称等自动进行注入
    2.使用方法:设置<bean>元素的autowire属性
        a.autowire属性取值:
            取值                            说明
             no                默认值。Spring 默认不进行自动装配,必须显式指定依赖对象
             byName            根据属性名自动装配。Spring 自动查找与属性名相同的id,如果找到,则自动注入,否则什么都不做
             byType            根据属性的类型自动装配。Spring 自动查找与属性类型相同的Bean,如果刚好找到★唯一★的那个,则自动注入;如果找到多个与属性类型相同的Bean,则抛出异常;如果没找              到,就什么也不做
             constructor    和byType 类似,不过它针对构造方法。如果 Spring 找到一个Bean和构造方法的参数类型相匹配,则通过构造注入该依赖对象;如果找不到,将抛出异常
        b.可以为<beans>元素设置default-autowire属性,影响全局
        c.<bean>节点上autowire的设置可以覆盖全局设置
    3.一般情况下,使用byName的方式更合适。Spring3推荐使用带参构造或者使用在setXxx方法上使用@Required注解

4.注意:自动装配使得配置文件可以非常简洁,但同时也造成组件之间的依赖关系不明确,容易引发一些潜在的错误,在实际项目中要谨慎使用

拆分配置文件 -- 拆分策略
    1.公用配置+每个系统模块一个单独配置文件(包含dao、service、action)
    2.公用配置+DAO Bean配置+业务逻辑Bean配置+Action Bean配置
    3.两种策略各有特色,适用于不同场合

方法:
        a.配制Spring集成时:
            1)配制ContextLoadListener的contextConfigLocation属性
            2)配置多个配置文件用逗号隔开
            3)使用通配符
        b.使用<import  resource="xxx.xml"/>方式

使用注解实现IoC:
    1.注解方式将Bean的定义信息和Bean实现类结合在一起
    2.Spring提供的注解:
        @Component
        @Repository    :用于标注DAO类
        @Service    :用于标注业务类
        @Controller    :用于标注控制器类
    范例:

 @Repository("userDao")
public class UserDaoImpl implements UserDao {

}

等效:与在XML配置文件中编写

 <bean id="userDao" class="com.xuetang9.dao.impl.UserDaoImpl" /> 

3.使用@Autowired注解实现Bean的自动装配
        a.默认按类型匹配
        b.可以使用@Qualifier指定Bean的名称
    范例:
    a.可以对类的成员变量进行标注

 @Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDao")
private UserDao userDao;
……
}

b.也可以对方法的入参进行标注

  @Service("userService")
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}

4.使用@Scope注解指定Bean的作用域
    范例:

  @Scope("prototype")
@Service("userService")
public class UserService implements UserService {
……
}

5.使用注解信息启动Spring容器
    范例:

  <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="……
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 扫描包中注解标注的类 -->
<!-- 指定需要扫描的基类包,多个包可用逗号隔开 -->
<context:component-scan base-package="com.xuetang9.demo.service, com.xuetang9.demo.dao" />
</beans>

使用注解实现事务处理:
    1.在Spring配置文件中配置事务管理类,并添加对注解配置的事务的支持
    范例:

 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />

2.使用@Transactional为方法添加事务支持
    范例:

  public class UserServiceImpl implements UserService {
@Transactional(readOnly=true)
public User login(User user){
......
}
}

3.@Transactional属性
    属性                        类型                                                                说明
    propagation                枚举型:Propagation                                        可选的传播性设置。使用举例:@Transactional(propagation=Propagation.REQUIRES_NEW)
    isolation                枚举型:Isolation                                        可选的隔离性级别。使用举例:@Transactional(isolation=Isolation.READ_COMMITTED)
    readOnly                布尔型                                                    是否为只读型事务。使用举例:@Transactional(readOnly=true)
    timeout                    sint型(以秒为单位)                                    事务超时。使用举例:Transactional(timeout=10)
    rollbackFor                一组 Class 类的实例,必须是Throwable的子类            一组异常类,遇到时 必须回滚。使用举例:@Transactional(rollbackFor={SQLException.class}),多个异常用逗号隔开
    rollbackForClassName    一组 Class 类的名字,必须是Throwable的子类            一组异常类名,遇到时 必须回滚。使用举例:@Transactional(rollbackForClassName={"SQLException"}),多个异常用逗号隔开
    noRollbackFor            一组 Class 类的实例,必须是Throwable的子类            一组异常类,遇到时 必须不回滚
    noRollbackForClassName    一组 Class 类的名字,必须是Throwable的子类            一组异常类名,遇到时 必须不回滚
    
范例:
1.hibernate的配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>

2.Struts的配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.Elastic.SpringDemo4.ivy.action.UserAction" method="login">
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>

3.Spring的配置文件
a1.spring与hibernate的配置文件分离 -- applicationContext.xml

 <?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: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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置事务 -->
<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="search*" propagation="REQUIRED" read-only="true" timeout="5"/>
<tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txpointcut" expression="execution(* com.Elastic.SpringDemo4.ivy.service..*.*(..))" />
<aop:advisor advice-ref="tx" pointcut-ref="txpointcut"/>
</aop:config>
</beans>

a2.spring与hibernate集成 -- applicationContext.xml

 <?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: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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 配置DataSource,c3p0的方式配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost/hibernatedb"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="initialPoolSize" value="10"></property>
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="100"></property>
<property name="acquireIncrement" value="3"></property>
<property name="acquireRetryAttempts" value="3"></property>
<property name="acquireRetryDelay" value="5"></property>
<property name="idleConnectionTestPeriod" value="60"></property>
</bean> <!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/Elastic/SpringDemo4/ivy/entity/*.hbm.xml</value>
</list>
</property>
<!-- 加载持久化类也可以使用<property name="packagesToScan" value="com.Elastic.SpringDemo4.ivy.entity" /> -->
<!-- <property name="annotatedClasses">
<list>
<value>com.Elastic.SpringDemo4.ivy.entity.User</value>
</list>
</property> -->
</bean>
<!-- 配置事务 -->
<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="search*" propagation="REQUIRED" read-only="true" timeout="5"/>
<tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txpointcut" expression="execution(* com.Elastic.SpringDemo4.ivy.service..*.*(..))" />
<aop:advisor advice-ref="tx" pointcut-ref="txpointcut"/>
</aop:config>
</beans>

a3.在Spring中引用属性文件 -- applicationContext.xml

 <?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: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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置属性文件的读取 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> <!-- 配置DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"></property>
<property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}"></property>
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"></property>
</bean> <!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/Elastic/SpringDemo4/ivy/entity/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事务 -->
<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="search*" propagation="REQUIRED" read-only="true" timeout="5"/>
<tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txpointcut" expression="execution(* com.Elastic.SpringDemo4.ivy.service..*.*(..))" />
<aop:advisor advice-ref="tx" pointcut-ref="txpointcut"/>
</aop:config>
</beans>

a4.当有多个Spring的配置文件时 -- applicationContext.xml
方法二:applicationContext配置中用,<import resource="">

 <?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: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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置属性文件的读取 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> <!-- 配置DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"></property>
<property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}"></property>
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"></property>
</bean> <!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/Elastic/SpringDemo4/ivy/entity/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事务 -->
<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="search*" propagation="REQUIRED" read-only="true" timeout="5"/>
<tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txpointcut" expression="execution(* com.Elastic.SpringDemo4.ivy.service..*.*(..))" />
<aop:advisor advice-ref="tx" pointcut-ref="txpointcut"/>
</aop:config> <!-- DAO -->
<import resource="Spring-DAO.xml"/> <!-- Service -->
<import resource="Spring-Service.xml"/>
</beans>

a5.使用注解 -- applicationContext.xml

 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 配置属性文件的读取 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> <!-- 配置DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"></property>
<property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}"></property>
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"></property>
</bean> <!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/Elastic/SpringDemo4/ivy/entity/*.hbm.xml</value>
</list>
</property>
</bean> <!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- 使用注解配置DAO\Service -->
<context:component-scan base-package="com.Elastic.SpringDemo4.ivy"></context:component-scan>
</beans>

b.Spring-DAO.xml

 <?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="userDao" class="com.Elastic.SpringDemo4.ivy.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>

b2.Spring自动配置 -- Spring-DAO.xml

 <?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="userDao" class="com.Elastic.SpringDemo4.ivy.dao.impl.UserDaoImpl" autowire="byName">
</bean>
</beans>

c.Spring-Service.xml

 <?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="userService" class="com.Elastic.SpringDemo4.ivy.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>

c2.Spring自动配置 -- Spring-Service.xml

 <?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="userService" class="com.Elastic.SpringDemo4.ivy.service.impl.UserServiceImpl" autowire="byName">
</bean>
</beans>

4.动态Java项目的配置
a.web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringDemo4_ivy</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 配置Spring文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 配置Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- OpenSessionInView -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 配置Struts2核心过滤器 -->
<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> <!-- 设置session过期时间(单位:分钟) -->
<session-config>
<session-timeout>20</session-timeout>
</session-config> </web-app>

b.当有多个Spring的配置文件时
方法一:context-param配置中用逗号隔开

 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringDemo4_ivy</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 配置Spring文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:Spring-*.xml</param-value>
</context-param> <!-- 配置Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- OpenSessionInView -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 配置Struts2核心过滤器 -->
<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> <!-- 设置session过期时间(单位:分钟) -->
<session-config>
<session-timeout>20</session-timeout>
</session-config> </web-app>

5.属性文件
a.jdbc.properties

 jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost/hibernatedb
jdbc.driver=com.mysql.jdbc.Driver # c3p0
jdbc.initialPoolSize=10
jdbc.minPoolSize=5
jdbc.maxPoolSize=100
jdbc.acquireIncrement=3
jdbc.acquireRetryAttempts=3
jdbc.acquireRetryDelay=5
jdbc.idleConnectionTestPeriod=60

6.实体类及其配置文件
a.User类

 package com.Elastic.SpringDemo4.ivy.entity;

 import java.io.Serializable;

 public class User implements Serializable{
private String loginName;
private String loginPass; public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getLoginPass() {
return loginPass;
}
public void setLoginPass(String loginPass) {
this.loginPass = loginPass;
} }

b.User.hbm.xml

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-7-8 16:52:24 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.Elastic.SpringDemo4.ivy.entity.User" table="user">
<id name="loginName" type="java.lang.String">
<column name="userName" />
<generator class="assigned" />
</id>
<property name="loginPass" type="java.lang.String">
<column name="passWord" />
</property>
</class>
</hibernate-mapping>

7.dao包
a.UserDao接口

 package com.Elastic.SpringDemo4.ivy.dao;

 import java.io.Serializable;

 import com.Elastic.SpringDemo4.ivy.entity.User;

 public interface UserDao {
User findById(Serializable id);
}

8.dao.impl包
a1.UserDaoImpl

 package com.Elastic.SpringDemo4.ivy.dao.impl;

 import java.io.Serializable;

 import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository; import com.Elastic.SpringDemo4.ivy.dao.UserDao;
import com.Elastic.SpringDemo4.ivy.entity.User; public class UserDaoImpl extends HibernateDaoSupport implements UserDao { @Override
public User findById(Serializable id) {
return this.getHibernateTemplate().get(User.class, id);
}
}

a2.使用注解 -- UserDaoImpl

 package com.Elastic.SpringDemo4.ivy.dao.impl;

 import java.io.Serializable;

 import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository; import com.Elastic.SpringDemo4.ivy.dao.UserDao;
import com.Elastic.SpringDemo4.ivy.entity.User; @Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { @Autowired
public UserDaoImpl(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
} @Override
public User findById(Serializable id) {
return this.getHibernateTemplate().get(User.class, id);
}
}

b.使用注解 -- TestUserDaoImpl

 package com.Elastic.SpringDemo4.ivy.dao.impl;

 import java.io.Serializable;

 import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository; import com.Elastic.SpringDemo4.ivy.dao.UserDao;
import com.Elastic.SpringDemo4.ivy.entity.User; @Repository
public class TestUserDaoImpl extends HibernateDaoSupport implements UserDao { @Autowired
public TestUserDaoImpl(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
} @Override
public User findById(Serializable id) {
return null;
}
}

9.service包
a.UserService接口

 package com.Elastic.SpringDemo4.ivy.service;

 import com.Elastic.SpringDemo4.ivy.entity.User;

 public interface UserService {
public User search(String name, String pass);
}

10.service.impl包
a.UserServiceImpl

 package com.Elastic.SpringDemo4.ivy.service.impl;

 import com.Elastic.SpringDemo4.ivy.dao.UserDao;
import com.Elastic.SpringDemo4.ivy.entity.User;
import com.Elastic.SpringDemo4.ivy.service.UserService; public class UserServiceImpl implements UserService {
private UserDao userDao; public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} @Override
public User search(String name, String pass) {
User user = userDao.findById(name);
if (null != user && user.getLoginPass().equals(pass)) {
return user;
}
return null;
}
}

a2.使用注解 -- UserServiceImpl

 package com.Elastic.SpringDemo4.ivy.service.impl;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import com.Elastic.SpringDemo4.ivy.dao.UserDao;
import com.Elastic.SpringDemo4.ivy.entity.User;
import com.Elastic.SpringDemo4.ivy.service.UserService; @Service("userService")
@Transactional(propagation=Propagation.REQUIRED,rollbackFor={Exception.class,RuntimeException.class})
public class UserServiceImpl implements UserService {
//方法一
@Autowired
@Qualifier("userDaoImpl")
private UserDao userDao; //方法二
/*public UserDao getUserDao() {
return userDao;
} @Autowired
@Qualifier("testUserDaoImpl")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}*/ // @Transactional(propagation=Propagation.REQUIRED,readOnly=true,timeout=3,rollbackFor={Exception.class,RuntimeException.class})
@Transactional(timeout=3)
@Override
public User search(String name, String pass) {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
} User user = userDao.findById(name);
if (null != user && user.getLoginPass().equals(pass)) {
return user;
}
return null;
} @Transactional
public void test() {
userDao.findById("");
}
}

11.action包
a.UserAction

 package com.Elastic.SpringDemo4.ivy.action;

 import com.Elastic.SpringDemo4.ivy.entity.User;
import com.Elastic.SpringDemo4.ivy.service.UserService;
import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport {
private UserService userService;
private User user; public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public String login() {
User loginUser = userService.search(user.getLoginName(), user.getLoginPass());
if (null == loginUser) {
return ERROR;
}
return SUCCESS;
}
}

SSH(三)的更多相关文章

  1. 【SSH三框架】Struts2第一章的基础:第一次写Struts2规划

    今年八月,当已经SSH三架完成学业.然后,他感动Android开展.三个框架已经很长的时间做无用的东西.所以,如果你想花三四天的时间来复习一下,写在博客. 附带SSH整个jar包网盘下载:http:/ ...

  2. SSH三种框架及表示层、业务层和持久层的理解

    Struts(表示层)+Spring(业务层)+Hibernate(持久层) SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts:Struts是一个表示 ...

  3. Jmeter(三十八)while控制器实现ssh三次重连

    在jmeter中,可以使用SSH协议连接主机进行相关操作, 步骤如下 首先添加一个ssh command  我们的测试交流群:317765580 在command中填写远程连接的必要信息 结果树中可以 ...

  4. SSH三种框架及表示层、业务层和持久层的理解(转)

    Struts(表示层)+Spring(业务层)+Hibernate(持久层) SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts:Struts是一个表示 ...

  5. 用最简单话概括SSH三框架

    Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL语句. Struts是用来做应用层的,他它负责调用业务逻辑serivce层,所以S ...

  6. 【SSH三框架】Struts2第六章的基础:他们拦截函数的定义

    干web当然,需要做的事情时,项目管理登录身份验证及其他权利.假设我们必须使用相应的登陆,未经允许是不可能的. 因此,我们需要使用拦截器,拦截功能struts2它集成.当然,有可能在Spring正在使 ...

  7. SSH三作品的框架和流程

    Hibernate工作的,为什么? 原理: 1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件 2.由hibernate.cfg.xm ...

  8. 【SSH三框架】Hibernate基金会七:许多附属业务

    相对于上述一关系,在这里,下一个一对多关系说明. 另外,在上述.我们描述了许多人描述的一一对应关系.在关系数据库是多对一的关系.但也有许多关系. 但,只知道它是不够的,Hibernate它是一种面向对 ...

  9. 【SSH三个框架】Hibernate第七基金会:不少下属业务

    相对于上述一关系,在这里,下一个一对多关系说明. 另外,在上述.我们描述了许多人描述的一一对应关系.在关系数据库是多对一的关系,但也有许多关系.但,只知道它是不够的,Hibernate它是一种面向对象 ...

  10. 【SSH三个框架】Hibernate第十篇基础:inverse属性具体解释

    inverse后经常用于双向1-N在相关性.它也可以在使用N-N该协会,这里,例如用双1-N联想 或两个与各部门及工作人员,两javabean没有写. 首先,我们的员工看映射文件: <?xml ...

随机推荐

  1. Spark学习笔记(一)——基础概述

    本篇笔记主要说一下Spark到底是个什么东西,了解一下它的基本组成部分,了解一下基本的概念,为之后的学习做铺垫.过于细节的东西并不深究.在实际的操作过程中,才能够更加深刻的理解其内涵. 1.什么是Sp ...

  2. 个人博客-vue-blog

    http://47.100.126.169/zmengBlog/

  3. CF749D Leaving Auction set排序查找

    CodeForces 749D. Leaving Auction 传送门 There are n people taking part in auction today. The rules of a ...

  4. C# 阿里云查询、删除文件

    class Program { static string regionId = "cn-shanghai"; static string accessKeyId = " ...

  5. MySQL定时备份(全量备份+增量备份)

    MySQL 定时备份 参考 zone7_ 的 实战-MySQL定时备份系列文章 参考 zmcyu 的 mysql数据库的完整备份.差异备份.增量备份 更多binlog的学习参考马丁传奇的 MySQL的 ...

  6. 自学 JAVA 的几点建议

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 许久不见,最近公众号多了很多在校的师弟师妹们.有很多同学都加了我微信问了一些诸如 [如何自学 Java ]的问题, ...

  7. EasyExcel 自定义单元格式的问题。

    最近在做一个关于性能测试管理系统,一个新的需求,需要导出测试报告,直接使用了ali的封装的EasyExcel,但是在复杂头与一些样式,就缺少了自定义的灵活性,在官方demo中没有找到很好的解决方法. ...

  8. DataFrame 索引和复合索引

    前面按照多个条件进行分组产生的索引是复合索引 一.索引 # a.获取index df.index # b.指定index df.index = [] # c.重新设置index df.reindex( ...

  9. ORM批量添加

    # ########### Book是模型类 ############ 建一个空列表,盛放obj对象lst_obj=[]# 用for循环控制添加信息条数for i in range(100):# 创建 ...

  10. 域渗透之票据传递攻击(pass the ticket,ptt)

    票据传递攻击(PtT)是一种使用Kerberos票据代替明文密码或NTLM哈希的方法.PtT最常见的用途可能是使用黄金票据和白银票据,通过PtT访问主机相当简单. 1.ptt攻击的部分 就不是简单的N ...