spring 学习

来源:互联网 发布:海报自动设计软件 编辑:程序博客网 时间:2024/06/05 02:54
第一部分Java SSH框架搭建<    1 springHibernate 的集成    2 spring 声明式事务    3 springstruts2 的集成>Web 的框架基本思路:Request->action ->service ->dao ->session->connection->数据库->connection->session->dao->service->action->jsp其中session 是交由sessionFactory 管理A如果在项目中存在Hibernate的配置文件hibernate.cfg.xml可以在spring 配置文件中实现SessionFactory Bean的定义。<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation">    <value>classpath:hibernate.cfg.xml</value></property></bean>B如果在spring 中集中管理。需要在spring中配置数据源对象(dbcp,c3p0,Proxool)其中用dbcp需要commons-dbcp-1.4.jar 和 commons-poll-1.6.jar<!--定义dbcp数据源--><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><!--指定JDBC驱动类--><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property><!--提供连接数据库的URL地址--><property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property><!--提供连接数据库的用户名和密码--><property name="username" value="bdqn"></property><property name="password" value="bdqn"></property></bean>配置好数据源后 在此基础上配置SessionFactory Bean<!--定义SessionFactory Bean--><bean id="sessionFactory"     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">         <!--为LocalSessionFactoryBean注入定义好的数据源--><property name="dataSource">    <ref bean="dataSource"></property><!--添加Hibernate配置参数--><property name="hibernateProperties">    <props><prop key="hibernate.dialet">org.hibernate.dialect.OracleDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props><property><!--添加对象关系映射文件--><property name="mappingResources"><list><value>cn/bdqn/jboa/entity/Employee.hbm.xml</value><!--省略部分文件--></list></property></bean>因为可能有很多的映射文件,挨个指定很麻烦。可以直接指定文件所在的目录用mappingDirectoryLocations<!--添加对象关系映射文件所在路径--><property name="mappingDirectoryLocations">    <list>        <value>classpath:cn/bdqn/jboa/entity/</value>    </list></property>第二部分 使用HibernaTempLate简化Hibernate DAO在配置好之前的文件之后。就能进行数据访问提取开发。而spring中提供了HibernateTemplate例如:旧版 未用模板public void addEmployee(Employee employee){    Transcation tx = null;    try{        tx=HibernateUtil.getSession().beginTransaction();        HibernateUtil.getSession().save(employee);        tx.commit();    }catch(RuntimeException re){        tx.rollback();        throw re;    }finally{        HibernateUtil.closseSession();    }}新用了模板  注意本类要继承 HibernateDaoSupport 其中2个方法很重要1 public final void setSessionFactory(SessionFactory sessionFactory).2 public final HibernateTemplate getHibernateTemplate().public void addEmployee(Employee employee){    this.getHibernateTemplate.save(employee);}列举了HibernateTemplate的常用方法find(String queryString)           Listsave(Object entity)                    Serializabbleupdate(Object entity)           voiddelete(Object entity)                   voidfindByNamedQuery(String queryName)        Listget(Class entityClass,Serinlizable id)   ObjectsaveOrUpdate(Object entity)     voidsetMaxResult(int maxResult)    voidSpring 配置文件中:       <bean id=“employeeDao”  class="cn.bdqn.jdoa.dao.hibimpl.EmployeeDaoHibImpl">        <property name="sessionFactory" ref="sessionFactory"/></bean>第三部分业务层 并且添加声明式事务public interface EmployeeService{   //接口类/***登陆*@param Employee 包含用户名和密码*@return 登陆成功返回登陆成功的用户信息,登录失败返回null*@throws Exception 抛出异常*/public Employee login(Employee employee) thorws Exception;}public class EmployeeServiceImpl implements EmoloyeeService{ //实现类     private EmployeeDao employeeDao;    public Employee login(Employee employee) throws Exception{        try{             List<Employee> list = employeeDao.find(employee);               if(list ==null || list.size()!=1){                return null;            }            return list.get(0);        }catch(Exception e){            throw new Exception("登陆过成错误");        }    }        //注入EmployeeDao    public void setEmployeeDao(EmployeeDao employeeDao){        this.employeeDao = employeeDao;    }}把登陆业务类加入spring    <bean id="employeeService" class="文件的目录包地址">        <property name = "employeeDao"  ref="employeeDao"></property>    </bean>第四部分为业务层添加声明式事务处理在spring 配置文件中采用Schema的方式进行,这需要用到tx和aop两个命名空间下的标签例如<?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:context="http://www.springframework.org/schema/context"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:aop="http://www.springframework.org/schema/aop"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context.xsd       http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx.xsd       http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">  </beans>  接下来需要配置一个事务管理器对象。统一管理事务的处理Spring 的Hibernate 事务管理类 HibernateTransactionManager例如<!-- 配置事务管理器 --><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">   <property name="sessionFactory" ref="mySessionFactory">   </property></bean><!--  配置事务传播特性 --><!--通过<tx:advice> 定义事务增强,并指定事务管理器--><tx:advice id="TestAdvice" transaction-manager="txManager">    <tx:attributes>      <tx:method name="find*" propagation="REQUIRED" read-only="true"/>      <tx:method name="search*" propagation="REQUIRED" read-only="true"/>          <tx:method name="query*" propagation="REQUIRED" read-only="true"/>      <tx:method name="save*" propagation="REQUIRED"/>      <tx:method name="del*" propagation="REQUIRED"/>      <tx:method name="update*" propagation="REQUIRED"/>      <tx:method name="add*" propagation="REQUIRED"/>      <tx:method name="get*" propagation="REQUIRED"/>      <tx:method name="apply*" propagation="REQUIRED"/>      <tx:method name="do*" propagation="REQUIRED"/>      <tx:method name="*"   propagation="REQUIRED" read-only="true"/>    </tx:attributes></tx:advice><!--  配置参与事务的类 --><aop:config><aop:pointcut id="allTestServiceMethod" expression="execution(* com.test.testAda.test.model.service.*.*(..))"/><aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="TestAdvice" /></aop:config>其中 propagation 事务传播机制-- PROPAGATION_REQUIRED       -- 加入当前已有事务;只有当前没有事务才起一个新的事务       比如说,ServiceB.methodB的事务级别定义为PROPAGATION_REQUIRED, 那么由于ServiceA.methodA的时候,ServiceA.methodA已经起了事务,这时调用ServiceB.methodB,ServiceB.methodB看到自己已经运行在ServiceA.methodA的事务内部,就不再起新的事务。而假如ServiceA.methodA运行的时候发现自己没有在事务中,它就会为自己分配一个事务。这样,在ServiceA.methodA或者在ServiceB.methodB内的任何地方出现异常,事务都会被回滚。    -- PROPAGATION_SUPPORTS       -- 如果当前在事务中,即以事务的形式运行,如果当前不在一个事务中,那么就以非事务的形式运行    -- PROPAGATION_MANDATORY       -- 必须在一个事务中运行。也就是说,只能被一个父事务调用。否则,就要抛出异常。    -- PROPAGATION_REQUIRES_NEW       -- 比如我们设计ServiceA.methodA的事务级别为PROPAGATION_REQUIRED,ServiceB.methodB的事务级别为PROPAGATION_REQUIRES_NEW,那么当执行到ServiceB.methodB的时候,ServiceA.methodA所在的事务就会挂起,ServiceB.methodB会起一个新的事务,等待ServiceB.methodB的事务完成以后,它才继续执行。它与PROPAGATION_REQUIRED 的事务区别在于事务的回滚程度了。因为ServiceB.methodB是新起一个事务,那么就是存在两个不同的事务。如果ServiceB.methodB已经提交,那么ServiceA.methodA失败回滚,ServiceB.methodB是不会回滚的。如果ServiceB.methodB失败回滚,它抛出的异常被ServiceA.methodA捕获,ServiceA.methodA事务仍然可以提交。    -- PROPAGATION_NOT_SUPPORTED       -- 当前不支持事务。比如ServiceA.methodA的事务级别是PROPAGATION_REQUIRED ,而ServiceB.methodB的事务级别是PROPAGATION_NOT_SUPPORTED,       那么当执行到ServiceB.methodB时,ServiceA.methodA的事务挂起,而它以非事务的状态运行完,再继续ServiceA.methodA的事务。    -- PROPAGATION_NEVER       -- 不能在事务中运行。假设ServiceA.methodA的事务级别是PROPAGATION_REQUIRED, 而ServiceB.methodB的事务级别是PROPAGATION_NEVER,那么ServiceB.methodB就要抛出异常了。    -- PROPAGATION_NESTED       -- 理解Nested的关键是savepoint。它与PROPAGATION_REQUIRES_NEW的区别是,PROPAGATION_REQUIRES_NEW另起一个事务,将会与它的父事务相互独立,而Nested的事务和它的父事务是相依的,它的提交是要等和它的父事务一块提交的。也就是说,如果父事务最后回滚,它也要回滚的。而Nested事务的好处是它有一个savepoint。也就是说ServiceB.methodB失败回滚,那么ServiceA.methodA也会回滚到savepoint点上,ServiceA.methodA可以选择另外一个分支,比如ServiceC.methodC,继续执行,来尝试完成自己的事务。但是这个事务并没有在EJB标准中定义。其中 isoloation 事务隔离机制 既的当前事务和其他事务的隔离程度,在并发事务处理的情况需要处理并设置。Spring事务的隔离级别 1. ISOLATION_DEFAULT: 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别.      另外四个与JDBC的隔离级别相对应 2. ISOLATION_READ_UNCOMMITTED: 这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据。      这种隔离级别会产生脏读,不可重复读和幻像读。 3. ISOLATION_READ_COMMITTED: 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据 4. ISOLATION_REPEATABLE_READ: 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。      它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。 5. ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。      除了防止脏读,不可重复读外,还避免了幻像读。什么是脏数据,脏读,不可重复读,幻觉读? 脏读: 指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,     另外一个事务也访问这个数据,然后使用了这个数据。因为这个数据是还没有提交的数据, 那么另外一     个事务读到的这个数据是脏数据,依据脏数据所做的操作可能是不正确的。 不可重复读: 指在一个事务内,多次读同一数据。在这个事务还没有结束时,另外一个事务也访问该同一数据。             那么,在第一个事务中的两次读数据之间,由于第二个事务的修改,那么第一个事务两次读到的数据             可能是不一样的。这样就发生了在一个事务内两次读到的数据是不一样的,因此称为是不可重复读。 幻觉读: 指当事务不是独立执行时发生的一种现象,例如第一个事务对一个表中的数据进行了修改,这种修改涉及         到表中的全部数据行。同时,第二个事务也修改这个表中的数据,这种修改是向表中插入一行新数据。那么,         以后就会发生操作第一个事务的用户发现表中还有没有修改的数据行,就好象发生了幻觉一样。来源: <http://www.cnblogs.com/yangy608/archive/2011/06/29/2093478.html>第五部分Spring 和 Struts2 的整合需要struts2-spring-plugin-2.3.15.1.jar 此包中有一个struts-plugin.xml 文件<!--定义Spring 的ObjectFactory--><bean type="com.opensymphony.xwork2.0.ObjectFactory"  name='spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory"/><!--   将Spring的ObjectFactory-设置为Struts2默认的ObjectFactory--><constant name="struts.objectFactory" value=”spring“/>来源: <http://blog.csdn.net/flfna/article/details/4901202>//定义业务接口的属性private EmployeeService employeeService = null;//定义setter 方法,方法名应和spring 配置文件中业务Bean 的名称相匹配public void setEmployeeService(EmployeeService employeeService){    this.employeeService = employeeService;}//省略getter方法在Strut.xml 中配置 struts.objectFactory.spring.autoWire常量,可以改变差劲的自动装配策略<constant name="struts.objectFactory.spring.autoWire"  value="type" />按类型匹配即不再按setter方法名和Bean的名称匹配。根据产品类型查询合适的Bean实现注入。最后   配置ContextLoaderListener 使spring容器 和web容器同时启动  <context-param>  <!--配置文件位置,默认是在WEB-INF下-->        <param-name>contextConfigLocation</param-name>        <param-value>classpath*:applicationContext.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener>      <!-- OpenSessionInViewFilter  作用是把一次完整的请求过程相绑定,在请求开始是开启session,结束关闭session。统一Hibernate Session 是唯一统一 -->    <filter>        <filter-name>OpenSessionInViewFilter</filter-name>        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>    </filter>    <init-param><!-- 指定自己的自定义的SessionFactory -->        <param-name>sessionFactoryBeaName</param-name>        <param-value>mySessionFactory</param-value>    </init-param>  完整的web.xml配置 如下<!-- spring 容器初始化 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath*:applicationContext.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener><!--     OpenSessionInViewFilter  作用是把一次完整的请求过程相绑定,在请求开始是开启session,结束关闭session。统一Hibernate Session 是唯一统一    <filter>        <filter-name>OpenSessionInViewFilter</filter-name>        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>    </filter>    <init-param>指定自己的自定义的SessionFactory        <param-name>sessionFactoryBeaName</param-name>        <param-value>mySessionFactory</param-value>    </init-param> -->    <!-- Spring 刷新Introspector防止内存泄露 -->    <listener>        <listener-class>org.springframework.web.util.IntrospectorCleanupListener        </listener-class>    </listener>    <listener>        <listener-class>            org.springframework.web.context.request.RequestContextListener        </listener-class>    </listener>    <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>    <welcome-file-list>        <welcome-file>index.html</welcome-file>    </welcome-file-list>  综上~~~~ApplicactionContext   Spring容器 接口    ClassPathXmlApplicationContext   读取本地XML文件    WebApplicationContext 通过Web中Listener技术实现容器实例////////////////////////////////////////////////////////////1. 控制台或Junit测试    ClassPathXmlApplicationContext  + applicationContext.xml2. MVC Web模式        ContextLoaderListener + WebApplicationContext + applicationContext.xml////////////////////////////////////////////////////////////   接口注入(不再使用)   构造注入(通过构造方法参数)   属性注入(setter方法属性赋值)////////////////////////////////////////////////////////////   <!-- web应用程序中初始化参数变量 -->  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <!-- spring web模式下加载器 -->  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>////////////////////////////////////////////////////////////   spring 对 hibernate 支持   1. 自定义Dao继承自spring.orm提供HiberanteDaoSupport      【HiberanteDaoSupport提供了一个hibernateTemplate模板类】   2. 自定义Dao中方法,替换成hibernateTemplate方法调用   3. spring中声明自定义DAO时,注入sessionFactory属性。. 基于注解的Spring应用配置实现     spring的context.jar包中定义了支持注解的四个内容:    @Repository(为DAO持久层设计)、    @Controller(为Action控制器设计)、    @Service(为service业务类设计)    @Component(任何类都可以使用)     ※ Spring容器可以自动实例化添加了注解的类,        并且在容器中以类名首字母小写的方式(Camel Case)为这些对象指定id     ------------------------------------------------------------------------------------------------------------     @Autowire —— 在bean实例化后指定需要spring注入的属性(spring按属性的类型在容器中查找)     @Resource —— 和@Autowire作用基本一致。但在指定了name属性后,可以按指定的"名称"容器中查找匹配项并注入。     ------------------------------------------------------------------------------------------------------------         spring的orm.jar包中定义了一个写好的OpenSessionInViewFilter,可以直接配置在web.xml中。不再需要手工编写。    <filter>        <filter-name>openSessionInView</filter-name>        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>    </filter>    <!-- 必须配置在struts的filter之前!!! -->    <filter-mapping>        <filter-name>openSessionInView</filter-name>        <url-pattern>*.action</url-pattern>    </filter-mapping>     ------------------------------------------------------------------------------------------------------------     解决继承自HibernateDaoSupport父类的自定义DAO注入问题:    在spring配置文件的<beans> 标签中添加default-autowire="byName"属性。    default-autowire所用:让spring容器中所有Bean属性的注入都通过指定方式注入。    可以使用的属性值: byName :按属性名称注入               byType :按属性类型注入               no     :默认不注入               constructor : 和byType方式一样,但使用构造函数注入。. Spring AOP 特性实现. Spring 配置式事务=====================================================================================================================================Spring 配置的优化第一部分 使用属性文件 spring 中提供的PropertyPlaceholderConfiguer引入属性文件 在applicationContext.xml中用${......}的方式引用属性文件中键值对。A <!--引入properties文件--><bean id="propertyConfigurer" class=="org.springframework.beans.factory.config.PropertyPlaceholderConfiguer"><property name="location">    <value>classpath:jdbc.properties</value></property></bean><!--配置DataSource--><bean id="datasource" class=“org.apache.common.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc. url}"></property><property name="username" value="${jdbc. username}"></property><property name="password" value="${jdbc. password}"></property></bean><!--省略 SessionFactory Bean 配置--><!--省略事务管理配置--><!--省略DAO Bean及Action Bean配置-->jdbc.properties属性文件内容jdbc.driver = oracle.jdbc.driver.PracleDriverjdbc. url = jdbc:oracle:thin:@localhost:1521:jboajdbc. username = scottjdbc. password =  tiger第二部分 创建基于注解的SessionFactory如果Hibernate使用注解方式进行映射配置,则必须使用AnnotationSessionFactoryBean【是LocalSessionFactoryBean的子类】<bean id="sessionFactiory" class="org.springfrmaework.orm.hibernate3.annotation.AnnotationSessionFactroyBean"><!--引用数据源--><property name="hibernateProperties"><props>    <!--Hibernate方言-->    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>    <prop key="hibernate.show_sql">true</prop>    <prop key="hibernate.format_sql">true</prop><props></property><!--加载由注解定义的持久化类--><property name="annotatedClasses"><list>    <value>cn.bdqn.jboa.entity.Employee</value >    <!--省略部分配置--><list></property>或者 用 packagesToScan添加包下所有带有注解的持久化类,多个包用英文逗号隔开既可<property name="packagesToScan" value=”cn.bdqn.jboa.entity.Employee“/></bean>第三部分 Spring 的自动装配和依赖检查A自动装配JBOA 系统采用传统方式配置Bean组建的代码如下所示.<!--dao--><bean id="employeeDao"  class="cn.bdqn.jboa.dao.hibimpl.EmployeeDaoHibImpl"><property name="sessionFactory" ref ="sessionFactory"/></bean><!--biz--><bean id="employeeService"  class="cn.bdqn.jboa.dao.hibimpl.EmployeeServiceImpl"><property name="employeeDao" ref ="employeeDao"/></bean><!--action--><bean id="employeeAction"  class="cn.bdqn.jboa.dao.hibimpl.EmployeeActionImpl"><property name="employeeService" ref ="employeeService"/></bean>如上用<property>标签为Bean的属性注入所需要的值。当要注入的属性增多时候。就会太繁琐了。所以:需要自动装配代码如下:<!--配置employeeDao,根据属性名称自动装配--><bean id="employeeDao"  class="cn.bdqn.jboa.dao.hibimpl.EmployeeDaoHibImpl"autowire="byName"/><!--配置employeeService,根据属性名称自动装配--><bean id="employeeService"  class="cn.bdqn.jboa.dao.hibimpl.EmployeeServiceImpl">autowire="byName"/><!--配置employee action,根据属性名称自动装配--><bean id="employeeAction"  class="cn.bdqn.jboa.dao.hibimpl.EmployeeActionImpl">scope="prototype"autowire="byName"/>autowire 的属性值和说明模式                        说明   no                     不使用自动装配,必须通过ref元素指定依赖,默认设置。   byName            根据属性名自动装配。此选项将检查容器并根据名字查找与                                             属性完全一致的bean,并将其与属性自动装配。   byType              如果容器中存在一个与指定属性类型相同的bean,那么将与                             该属性自动装配;如果存在多个该类型bean,那么抛出异                             常,并指出不能使用byType方式进行自动装配;如果没有找                             到相匹配的bean,则什么事都不发生,也可以通过设置                             dependency-check="objects"让Spring抛出异常。   constructor        与byType方式类似,不同之处在于它应用于构造器参数。如                             果容器中没有找到与构造器参数类型一致的bean,那么抛出                             异常。   autodetect        通过bean类的自省机制(introspection)来决定是使用                             constructor还是byType方式进行自动装配。如果发现默认的                             构造器,那么将使用byType方式。 可以设置bean使自动装配失效: 采用xml格式配置bean时,将<bean/>元素的autowire-candidate属性设置为false,这样容器在查找自动装配对象时,将不考虑该bean,即它不会被考虑作为其它bean自动装配的候选者,但是该bean本身还是可以使用自动装配来注入其它bean的。还可以在全局设置自动装配策略<beans。。。。。省略。。。。。。default-autowire="byName">。。。。。省略。。。。。。</beans>B依赖检查通过<bean>元素的dependency-check属性来设置依赖检查 或者通过<beans>元素的default-dependency-check属性来设置全局依赖检查、none 不进行依赖检查objects 检查和BeanFactory中其他Bean的依赖simple 检查原始类型和String类型是否正常装配all       同事检查simple和objecs第三部分 拆分Spring配置文件拆分方法在web.xml中 需要配置spring监听器来实现、A策略如下<!--省略其他代码--><!--Spring配置-->    <context-param>        <param-name>contextConfigLocation</param-name>            <!--配置多个Spring配置文件-->        <param-value>            classpath:applicationContext.xml,            classpath:applicationContext-dao.xml,            classpath:applicationContext-service.xml,            classpath:applicationContext-action.xml,             </param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener>  或者采用通配符(*)类配置多个有一定命名规则的配置文件。<!--省略其他代码--><!--Spring配置-->    <context-param>        <param-name>contextConfigLocation</param-name>            <!--采用通配符配置多个Spring配置文件-->        <param-value>                        classpath:applicationContext.xml,      classpath:applicationContext-*.xml,             </param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener>  B策略Spring 配置文件可以通过import子元素导入其他配置文件,将多个配置文件整合到一起。如下:<!--省略其他配置--><!--导入多个Spring配置文件--><import resource="applicationContext-dao.xml"/><import resource="applicationContext-service.xml"/><import resource="applicationContext-action.xml"/><!--省略其他配置-->第四部分 使用注解实现IoC的配置(@Repository,@Service,@Controller)使用注解实现自动装配(@Autowired)@Autowired是默认的按类型如需要按名称匹配则可以用@Qualifier("userDao")指定名称,如下@Autowired也可以对方法进行标注。使用注解指定Bean的作用域[spring 提供了@Scope来指定]加载注解定义的Bean[在spring配置文件中加载之前用注解定义的Bean组件]使用注解实现声明式事务处理
0 0