SSH框架中applicationContext.xml文件的配置

来源:互联网 发布:孤岛危机3优化补丁 编辑:程序博客网 时间:2024/06/13 04:26

今天我给大家详细解释一下Spring的applicationContext.xml文件。Ok,还是通过代码加注释的方式为大家演示;

以下是详解Spring的applicationContext.xml文件代码:

1.<!-- 头文件,主要注意一下编码 -->

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd       http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.1.xsd       http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd       http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-4.1.xsd       http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring            http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd       http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">  </beans>
2.<!--加载jdbc配置文件,建立数据源--!>

 <!-- 加载jdbc.properties配置文件 -->      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="locations">             <list>                   <value>classpath:jdbc.properties</value>             </list>          </property>       </bean>            <!-- c3p0数据源配置方式-->     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"p:driverClass="${jdbc.driverClassName}"p:jdbcUrl="${jdbc.url}"p:user="${jdbc.username}"p:password="${jdbc.password}"p:maxPoolSize="${jdbc.maxPoolSize}"p:minPoolSize="${jdbc.minPoolSize}"p:initialPoolSize="${jdbc.initialPoolSize}"p:maxIdleTime="${jdbc.maxIdleTime}"p:acquireIncrement="${jdbc.acquireIncrement}"p:maxStatements="${jdbc.maxStatements}"p:idleConnectionTestPeriod="${jdbc.idleConnectionTestPeriod}" /> 

3.<!-- 定义Hibernate会话工厂 ,并注入数据源实例dataSource--!>

 <bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" ><property name="dataSource" ref="dataSource"></property><property name="hibernateProperties">    <props>       <!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接那种类型的数据库服务器。-->             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                           <!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true。项目部署后可以设置为false,提高运行效率-->  <prop key="hibernate.show_sql">true</prop> <!--jdbc.batch_size是指Hibernate批量插入,删除和更新时每次操作的记录数。BatchSize越大,批量操作的向数据库发送Sql的次数越少, 速度就越快,同样耗用内存就越大--> <prop key="hibernate.jdbc.batch_size">20</prop>            <!--jdbc.fetch_size是指Hibernate每次从数据库中取出并放到JDBC的Statement中的记录条数。            FetchSize设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢--><prop key="hibernate.jdbc.fetch_size">50</prop><!-- 如果改为thread将不能开启事务 --><prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>   <!-- 是否自动创建数据库表  他主要有一下几个值:      validate:当sessionFactory创建时,自动验证或者schema定义导入数据库。      create:每次启动都drop掉原来的schema,创建新的。      create-drop:当sessionFactory明确关闭时,drop掉schema。      update(常用):如果没有schema就创建,有就更新。    -->  <prop key="hibernate.hbm2ddl.auto">update</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.temp.use_jdbc_metadata_defaults ">false</prop></props><!-- 包级扫描 hibernate实体类 --> </property><property name="packagesToScan"><list>    <value>com.b505.bean</value></list>    </property> </bean>

4.<!--start spring集中式声明(事务的配置)--!>

 <tx:advice id="txAdvice" transaction-manager="transactionManager">         <tx:attributes>              <tx:method name="saveUser*" />              <tx:method name="get*"/>            <tx:method name="find*"/>            <tx:method name="update*"/>            <tx:method name="findUsers*" />             <tx:method name="delete*"/>              <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到session   -->               <tx:method name="*"  propagation="REQUIRED" />        </tx:attributes>      </tx:advice>            <aop:config>          <aop:pointcut id="productServiceMethods" expression="execution(* com.b505.service..*.*(..))"/>         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />      </aop:config>


上面这段代码的意思是:
意思是这个事物advice的管理者为transactionManager,你从配置文件中应该能够找到一个ID为transactionManager的bean,而这个建议中规定了save方法的传输方式为required,也就是说没有sessionfactory的时候会自动建立,有的时候就不会建立了。当然了这只是规定了一个advice,你还需要指定一个aop:pointcut去引用他,例如<aop:config>        <aop:pointcut id="productServiceMethods"            expression="execution(public * x.y..*.*(..))" />        <aop:advisor pointcut-ref="productServiceMethods"            advice-ref="txAdvice" />    </aop:config>这样这个advice就被联系到了productServiceMethods这个pointcut上了


5.<!-- 事务管理,与hibernate相关联 -->

 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/>     </bean> 

6.<!-- 配置扫描注解,不扫描@Controller注解 -->

  <context:component-scan base-package="com.b505">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>      </context:component-scan>


详解:<context:component-scan base-package="com.b505"> </context:component-scan>

            Spring 容器初始化的时候,会扫描com.b505下 标有 (@Component,@Service,@Repository) 注解的 类 纳入spring容器管理

然而  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>的作用却是不扫描标有@Controller注解 ,标有@Controller注解的类将在spring mvc的配置文件中进行扫描;


在类上 ,使用以下注解,实现bean 的声明

@Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@Service 用于标注业务层组件

@Controller 用于标注控制层组件(如srping mvc的controller,struts中的action)

@Repository 用于标注数据访问组件,即DAO组件

示例:

@Controller
@RequestMapping(value = "/test")
public class TestController {

}


=====================================================================================================


在类的成员变量上,使用以下注解,实现属性的自动装配

@Autowired : 按类 的 类型进行装配

@Resource (推荐) : 1 如果同时指定了name和type,则从spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

    2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常 

    3.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常 

    4.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

@Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于J2EE的,减少了与spring的耦合。 

示例:

@Resource
private TestServiceImpl testServiceImpl;



2 0
原创粉丝点击