Spring配置文件总结
































http://blog.csdn.net/axu20/article/details/4668188

目录(?)[+]

1.基本配置:

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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	                   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	                   http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
	                   ">


<context:component-scan base-package="com.persia">

context:component-scan>

<context:annotation-config>

context:annotation-config>


<bean id="personServiceAnno" class="com.persia.PersonServiceAnnotation">bean>
<bean id="personDaoBeanAnno" class="com.persia.PersonDaoBean">bean>
<bean id="personDaoBeanAnno2" class="com.persia.PersonDaoBean">bean>


<bean id="personServiceAutoInject" class="com.persia.PersonServiceAutoInject" autowire="byName">bean>


<bean id="personService" class="com.persia.PersonServiceBean">

bean>

<bean id="personService2" class="com.persia.PersonServiceBeanFactory" factory-method="createInstance" lazy-init="true" 
      init-method="init"  destroy-method="destory">

bean>

<bean id="fac" class="com.persia.PersonServiceBeanInsFactory">bean>
<bean id="personService3" factory-bean="fac" factory-method="createInstance" scope="prototype">

bean>



<bean id="personDao" class="com.persia.PersonDaoBean">bean>
<bean id="personService4" class="com.persia.PersonServiceBean">
  <property name="personDao" ref="personDao">property>
bean>


<bean id="personService5" class="com.persia.PersonServiceBean">
  <property name="personDao">
     <bean class="com.persia.PersonDaoBean">bean>
  property>
  <property name="name" value="persia">property>
  <property name="age" value="21">property>
  
  <property name="sets">
    
     <set>
       <value>第一个value>
       <value>第二个value>
       <value>第三个value>
     set>
  property>
  
  <property name="lists">
    
    <list>
        <value>第一个lvalue>
       <value>第二个lvalue>
       <value>第三个lvalue>
    list>
    
  property>
  
  <property name="properties">
    <props>
      <prop key="key1">value1prop>
      <prop key="key2">value2prop>
      <prop key="key3">value3prop>
    props>
  property>
  
  <property name="map">
   <map>
      <entry key="key1" value="value-1">entry>
      <entry key="key2" value="value-2">entry>
      <entry key="key3" value="value-3">entry>
   map>
  property>
bean>

<bean id="personService6" class="com.persia.PersonServiceBean">
   <constructor-arg index="0" value="构造注入的name" >constructor-arg>
   
   <constructor-arg index="1" type="com.persia.IDaoBean" ref="personDao">
   constructor-arg> 
bean>

beans>

2.开启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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	                   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	                    http://www.springframework.org/schema/aop
	                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	                   http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
	                  ">

<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
<bean id="myInterceptor" class="com.persia.service.MyInterceptor">bean>
<bean id="personServiceImpl" class="com.persia.service.impl.PersonServiceImpl">bean>
beans>
AOP的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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	                   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	                    http://www.springframework.org/schema/aop
	                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	                   http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
	                  ">

<aop:aspectj-autoproxy>aop:aspectj-autoproxy>

<bean id="personService" class="com.persia.service.impl.PersonServiceImpl">bean>
<bean id="aspectBean" class="com.persia.service.MyInterceptor">bean>

<aop:config>
 <aop:aspect id="myaop" ref="aspectBean">
 <aop:pointcut id="mycut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..))"/>
 <aop:pointcut id="argcut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..)) and args(name)"/>  
 <aop:before pointcut-ref="mycut" method="doAccessCheck"  />
 <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
   <aop:after-throwing pointcut-ref="mycut" method="doThrowing"/>
   <aop:after pointcut-ref="argcut" method="doAfter" arg-names="name"/>
 <aop:around pointcut-ref="mycut" method="arround"/>
 aop:aspect>
  
aop:config>

beans>

3.开启事务和注解:

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: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-2.5.xsd
	                   http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
	                   http://www.springframework.org/schema/aop
	                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	                  ">

<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
	                  
   
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>   
    <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>   
    <property name="username" value="root"/>   
    <property name="password" value=""/>   
        
     <property name="initialSize" value="1"/>   
        
     <property name="maxActive" value="500"/>   
        
     <property name="maxIdle" value="2"/>   
        
     <property name="minIdle" value="1"/>   
  bean>  
   
     
 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
    <property name="dataSource" ref="dataSource"/>   
  bean>  
  
    <bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
    <property name="ds" ref="dataSource">property>
  bean>
   
     
  <tx:annotation-driven transaction-manager="txManager"/>  


beans>

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:context="http://www.springframework.org/schema/context"
	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-2.5.xsd
	                   http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
	                   http://www.springframework.org/schema/aop
	                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	                  ">

<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
	                  
   
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>   
    <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>   
    <property name="username" value="root"/>   
    <property name="password" value=""/>   
        
     <property name="initialSize" value="1"/>   
        
     <property name="maxActive" value="500"/>   
        
     <property name="maxIdle" value="2"/>   
        
     <property name="minIdle" value="1"/>   
  bean>  
   

 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
    <property name="dataSource" ref="dataSource"/>   
  bean>  
  
   <bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
    <property name="ds" ref="dataSource">property>
  bean>
  
  
      
<aop:config>  
       
    <aop:pointcut id="transactionPointcut" expression="execution(* com.persia.service..*.*(..))"/>  
    <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>  
aop:config> 
    
<tx:advice id="txAdvice" transaction-manager="txManager">  
      <tx:attributes> 
       
        <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/> 
       
        <tx:method name="*"/>  
      tx:attributes>  
tx:advice>  
   
  
beans>

4.SSH:

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: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-2.5.xsd
	                   http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
	                   http://www.springframework.org/schema/aop
	                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	                  ">


	   
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>   
    <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>   
    <property name="username" value="root"/>   
    <property name="password" value=""/>   
        
     <property name="initialSize" value="1"/>   
        
     <property name="maxActive" value="500"/>   
        
     <property name="maxIdle" value="2"/>   
        
     <property name="minIdle" value="1"/>   
  bean>  
  
  
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource"><ref bean="dataSource" />property>
	 <property name="mappingResources">
		    <list>
		      <value>com/persia/model/Person.hbm.xmlvalue>
		    list>
		 property>
		 
	    
        
        
              
              
               
	 <property name="hibernateProperties">
		    <value>
		        hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
		        hibernate.hbm2ddl.auto=update
		        hibernate.show_sql=false
		        hibernate.format_sql=false
		        hibernate.cache.use_second_level_cache=true
       	        hibernate.cache.use_query_cache=false
        	    hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
		    value>
	     property>
bean>


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

	
<tx:annotation-driven transaction-manager="txManager"/>


<context:annotation-config>context:annotation-config>


<bean id="personService" class="com.persia.service.impl.PersonServiceImpl">bean>


<bean name="/person/list" class="com.persia.struts.PersonListAction">




bean>

<bean name="/person/manage" class="com.persia.struts.PersonManageAction">bean>
beans>

5.SSH2:

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: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-2.5.xsd
	                   http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
	                   http://www.springframework.org/schema/aop
	                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	                  ">


	   
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>   
    <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>   
    <property name="username" value="root"/>   
    <property name="password" value=""/>   
        
     <property name="initialSize" value="1"/>   
        
     <property name="maxActive" value="500"/>   
        
     <property name="maxIdle" value="2"/>   
        
     <property name="minIdle" value="1"/>   
  bean>  
  
  
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource"><ref bean="dataSource" />property>
	 <property name="mappingResources">
		    <list>
		      <value>com/persia/model/Person.hbm.xmlvalue>
		    list>
		 property>
		 
	    
        
        
              
              
               
	 <property name="hibernateProperties">
		    <value>
		        hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
		        hibernate.hbm2ddl.auto=update
		        hibernate.show_sql=false
		        hibernate.format_sql=false
		        hibernate.cache.use_second_level_cache=true
       	        hibernate.cache.use_query_cache=false
        	    hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
		    value>
	     property>
bean>


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

	
<tx:annotation-driven transaction-manager="txManager"/>


<context:annotation-config>context:annotation-config>


<bean id="personService" class="com.persia.service.impl.PersonServiceImpl">bean>


<bean id="personList" class="com.persia.struts2.action.PersonListAction">bean>
beans>

6.SSJ:

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: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-2.5.xsd
	                   http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
	                   http://www.springframework.org/schema/aop
	                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	                  ">



<context:annotation-config>context:annotation-config>


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
      <property name="persistenceUnitName" value="SpringJPAPU"/>
bean>


    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  	  <property name="entityManagerFactory" ref="entityManagerFactory"/>
bean>


<tx:annotation-driven transaction-manager="txManager"/>
  



<bean id="personService" class="com.persia.service.impl.PersonServiceImpl">bean>


<bean name="/person/list" class="com.persia.struts.PersonListAction"/>
<bean name="/person/manage" class="com.persia.struts.PersonManageAction"/>
beans>

你可能感兴趣的:(框架)