spring配置文件详解



转自:http://book.51cto.com/art/201004/193743.htm

Spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"。Java EE程序员必须学会并灵活应用这份"图纸"准确地表达自己的"生产意图"。Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。

在学会动手"绘制图纸"之前,先要学会"阅读图纸",熟能生巧讲的就是这个道理,"熟读唐诗三百首,不会作诗也会吟"。

下面列举的是一份比较完整的配置文件模板,文档中各XML标签节点的基本用途也给出了详细的解释,这些XML标签节点在后续的知识点中均会用到,熟练掌握了这些XML节点及属性的用途后,为我们动手编写配置文件打下坚实的基础。

 
 
 
 


 

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">    
  6.         
  7.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">    
  8.             
  9.         <property name="driverClass">    
  10.             <value>com.mysql.jdbc.Drivervalue>    
  11.         property>    
  12.             
  13.         <property name="jdbcUrl">    
  14.             <value>jdbc:mysql://localhost:3306/eportal?useUnicode=    
  15.                 true&characterEncoding=gbkvalue>    
  16.         property>    
  17.             
  18.         <property name="user">    
  19.             <value>rootvalue>    
  20.         property>    
  21.             
  22.         <property name="password">    
  23.             <value>rootvalue>    
  24.         property>    
  25.             
  26.         <property name="maxPoolSize">    
  27.             <value>20value>    
  28.         property>    
  29.             
  30.         <property name="minPoolSize">    
  31.             <value>2value>    
  32.         property>    
  33.             
  34.         <property name="initialPoolSize">    
  35.             <value>2value>    
  36.         property>    
  37.             
  38.         <property name="maxIdleTime">    
  39.             <value>20value>    
  40.         property>    
  41.     bean>    
  42.         
  43.     <bean id="sessionFactory"    
  44.         class="org.springframework.orm.    
  45. hibernate3.LocalSessionFactoryBean">    
  46.             
  47.         <property name="dataSource" ref="dataSource" />    
  48.             
  49.         <property name="mappingResources">    
  50.             <list>    
  51.                 <value>com/eportal/ORM/News.hbm.xmlvalue>    
  52.                 <value>com/eportal/ORM/Category.hbm.xmlvalue>    
  53.                 <value>com/eportal/ORM/Memberlevel.hbm.xmlvalue>    
  54.                 <value>com/eportal/ORM/Cart.hbm.xmlvalue>    
  55.                 <value>com/eportal/ORM/Traffic.hbm.xmlvalue>    
  56.                 <value>com/eportal/ORM/Newsrule.hbm.xmlvalue>    
  57.                 <value>com/eportal/ORM/Merchandise.hbm.xmlvalue>    
  58.                 <value>com/eportal/ORM/Admin.hbm.xmlvalue>    
  59.                 <value>com/eportal/ORM/Orders.hbm.xmlvalue>    
  60.                 <value>com/eportal/ORM/Cartselectedmer.hbm.xmlvalue>    
  61.                 <value>com/eportal/ORM/Newscolumns.hbm.xmlvalue>    
  62.                 <value>com/eportal/ORM/Member.hbm.xmlvalue>    
  63.             list>    
  64.         property>    
  65.             
  66.         <property name="hibernateProperties">    
  67.             <props>    
  68.                     
  69.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>    
  70.                     
  71.                 <prop key="show_sql">trueprop>    
  72.                     
  73.                 <prop key="hibernate.jdbc.batch_size">50prop>    
  74.                 <prop key="show_sql">50prop>    
  75.             props>    
  76.         property>    
  77.     bean>    
  78.         
  79.     <bean id="transactionManager"    
  80.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">    
  81.             
  82.         <property name="sessionFactory" ref="sessionFactory" />    
  83.     bean>    
  84.         
  85.     <bean id="transactionInterceptor"    
  86.         class="org.springframework.transaction.interceptor.TransactionInterceptor">    
  87.             
  88.         <property name="transactionManager" ref="transactionManager" />    
  89.             
  90.         <property name="transactionAttributes">    
  91.             <props>    
  92.                     
  93.                 <prop key="browse*">PROPAGATION_REQUIRED,readOnlyprop>    
  94.                 <prop key="list*">PROPAGATION_REQUIRED,readOnlyprop>    
  95.                 <prop key="load*">PROPAGATION_REQUIRED,readOnlyprop>    
  96.                 <prop key="get*">PROPAGATION_REQUIRED,readOnlyprop>    
  97.                 <prop key="is*">PROPAGATION_REQUIRED,readOnlyprop>    
  98.                     
  99.                 <prop key="*">PROPAGATION_REQUIREDprop>    
  100.             props>    
  101.         property>    
  102.     bean>    
  103.         
  104.     <bean    
  105.         class="org.springframework.aop.framework.autoproxy.      
  106. BeanNameAutoProxyCreator">    
  107.             
  108.         <property name="beanNames">    
  109.             <list>    
  110.                 <value>adminServicevalue>    
  111.                 <value>columnsServicevalue>    
  112.                 <value>newsServicevalue>    
  113.                 <value>crawlServicevalue>    
  114.                 <value>memberLevelServicevalue>    
  115.                 <value>memberServicevalue>    
  116.                 <value>categoryServicevalue>    
  117.                 <value>merServicevalue>    
  118.                 <value>cartServicevalue>    
  119.                 <value>ordersServicevalue>    
  120.                 <value>trafficServicevalue>    
  121.             list>    
  122.         property>    
  123.             
  124.         <property name="proxyTargetClass">    
  125.             <value>truevalue>    
  126.         property>    
  127.             
  128.         <property name="interceptorNames">    
  129.             <list>    
  130.                 <value>transactionInterceptorvalue>    
  131.             list>    
  132.         property>    
  133.     bean>    
  134.         
  135.     <bean id="dao" class="com.eportal.DAO.BaseDAOImpl">    
  136.         <property name="sessionFactory" ref="sessionFactory" />    
  137.     bean>    
  138.         
  139.     <bean id="adminService" class="com.eportal.service.AdminServiceImpl">    
  140.         <property name="dao" ref="dao" />    
  141.     bean>    
  142.         
  143.     <bean id="columnsService" class="com.eportal.service.ColumnsServiceImpl">    
  144.         <property name="dao" ref="dao" />    
  145.     bean>    
  146.         
  147.         
  148.     <bean id="ordersService" class="com.eportal.service.OrderServiceImpl">    
  149.         <property name="dao" ref="dao" />    
  150.     bean>    
  151.         
  152.     <bean id="trafficService" class="com.eportal.service.TrafficServiceImpl">    
  153.         <property name="dao" ref="dao" />    
  154.     bean>    
  155.         
  156.     <bean id="adminAction" class="com.eportal.struts.action.    
  157. AdminAction"    
  158.         scope="prototype">    
  159.         <property name="service" ref="adminService" />    
  160.     bean>    
  161.         
  162.     <bean id="columnsAction" class="com.eportal.struts.action.    
  163. ColumnsAction"    
  164.         scope="prototype">    
  165.         <property name="service" ref="columnsService" />    
  166.     bean>    
  167.         
  168.     <bean id="newsAction" class="com.eportal.struts.action.    
  169. NewsAction"    
  170.         scope="prototype">    
  171.         <property name="service" ref="newsService" />    
  172.         <property name="columnsService" ref="columnsService" />    
  173.     bean>    
  174.         
  175.     <bean id="crawlAction" class="com.eportal.struts.action.    
  176. CrawlAction"    
  177.         scope="prototype">    
  178.         <property name="service" ref="crawlService" />    
  179.         <property name="columnsService" ref="columnsService" />    
  180.     bean>    
  181.         
  182. beans>     

转自:http://book.51cto.com/art/201004/193743.htm

Spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"。Java EE程序员必须学会并灵活应用这份"图纸"准确地表达自己的"生产意图"。Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。

在学会动手"绘制图纸"之前,先要学会"阅读图纸",熟能生巧讲的就是这个道理,"熟读唐诗三百首,不会作诗也会吟"。

下面列举的是一份比较完整的配置文件模板,文档中各XML标签节点的基本用途也给出了详细的解释,这些XML标签节点在后续的知识点中均会用到,熟练掌握了这些XML节点及属性的用途后,为我们动手编写配置文件打下坚实的基础。

 
 
 
 


 

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">    
  6.         
  7.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">    
  8.             
  9.         <property name="driverClass">    
  10.             <value>com.mysql.jdbc.Drivervalue>    
  11.         property>    
  12.             
  13.         <property name="jdbcUrl">    
  14.             <value>jdbc:mysql://localhost:3306/eportal?useUnicode=    
  15.                 true&characterEncoding=gbkvalue>    
  16.         property>    
  17.             
  18.         <property name="user">    
  19.             <value>rootvalue>    
  20.         property>    
  21.             
  22.         <property name="password">    
  23.             <value>rootvalue>    
  24.         property>    
  25.             
  26.         <property name="maxPoolSize">    
  27.             <value>20value>    
  28.         property>    
  29.             
  30.         <property name="minPoolSize">    
  31.             <value>2value>    
  32.         property>    
  33.             
  34.         <property name="initialPoolSize">    
  35.             <value>2value>    
  36.         property>    
  37.             
  38.         <property name="maxIdleTime">    
  39.             <value>20value>    
  40.         property>    
  41.     bean>    
  42.         
  43.     <bean id="sessionFactory"    
  44.         class="org.springframework.orm.    
  45. hibernate3.LocalSessionFactoryBean">    
  46.             
  47.         <property name="dataSource" ref="dataSource" />    
  48.             
  49.         <property name="mappingResources">    
  50.             <list>    
  51.                 <value>com/eportal/ORM/News.hbm.xmlvalue>    
  52.                 <value>com/eportal/ORM/Category.hbm.xmlvalue>    
  53.                 <value>com/eportal/ORM/Memberlevel.hbm.xmlvalue>    
  54.                 <value>com/eportal/ORM/Cart.hbm.xmlvalue>    
  55.                 <value>com/eportal/ORM/Traffic.hbm.xmlvalue>    
  56.                 <value>com/eportal/ORM/Newsrule.hbm.xmlvalue>    
  57.                 <value>com/eportal/ORM/Merchandise.hbm.xmlvalue>    
  58.                 <value>com/eportal/ORM/Admin.hbm.xmlvalue>    
  59.                 <value>com/eportal/ORM/Orders.hbm.xmlvalue>    
  60.                 <value>com/eportal/ORM/Cartselectedmer.hbm.xmlvalue>    
  61.                 <value>com/eportal/ORM/Newscolumns.hbm.xmlvalue>    
  62.                 <value>com/eportal/ORM/Member.hbm.xmlvalue>    
  63.             list>    
  64.         property>    
  65.             
  66.         <property name="hibernateProperties">    
  67.             <props>    
  68.                     
  69.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>    
  70.                     
  71.                 <prop key="show_sql">trueprop>    
  72.                     
  73.                 <prop key="hibernate.jdbc.batch_size">50prop>    
  74.                 <prop key="show_sql">50prop>    
  75.             props>    
  76.         property>    
  77.     bean>    
  78.         
  79.     <bean id="transactionManager"    
  80.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">    
  81.             
  82.         <property name="sessionFactory" ref="sessionFactory" />    
  83.     bean>    
  84.         
  85.     <bean id="transactionInterceptor"    
  86.         class="org.springframework.transaction.interceptor.TransactionInterceptor">    
  87.             
  88.         <property name="transactionManager" ref="transactionManager" />    
  89.             
  90.         <property name="transactionAttributes">    
  91.             <props>    
  92.                     
  93.                 <prop key="browse*">PROPAGATION_REQUIRED,readOnlyprop>    
  94.                 <prop key="list*">PROPAGATION_REQUIRED,readOnlyprop>    
  95.                 <prop key="load*">PROPAGATION_REQUIRED,readOnlyprop>    
  96.                 <prop key="get*">PROPAGATION_REQUIRED,readOnlyprop>    
  97.                 <prop key="is*">PROPAGATION_REQUIRED,readOnlyprop>    
  98.                     
  99.                 <prop key="*">PROPAGATION_REQUIREDprop>    
  100.             props>    
  101.         property>    
  102.     bean>    
  103.         
  104.     <bean    
  105.         class="org.springframework.aop.framework.autoproxy.      
  106. BeanNameAutoProxyCreator">    
  107.             
  108.         <property name="beanNames">    
  109.             <list>    
  110.                 <value>adminServicevalue>    
  111.                 <value>columnsServicevalue>    
  112.                 <value>newsServicevalue>    
  113.                 <value>crawlServicevalue>    
  114.                 <value>memberLevelServicevalue>    
  115.                 <value>memberServicevalue>    
  116.                 <value>categoryServicevalue>    
  117.                 <value>merServicevalue>    
  118.                 <value>cartServicevalue>    
  119.                 <value>ordersServicevalue>    
  120.                 <value>trafficServicevalue>    
  121.             list>    
  122.         property>    
  123.             
  124.         <property name="proxyTargetClass">    
  125.             <value>truevalue>    
  126.         property>    
  127.             
  128.         <property name="interceptorNames">    
  129.             <list>    
  130.                 <value>transactionInterceptorvalue>    
  131.             list>    
  132.         property>    
  133.     bean>    
  134.         
  135.     <bean id="dao" class="com.eportal.DAO.BaseDAOImpl">    
  136.         <property name="sessionFactory" ref="sessionFactory" />    
  137.     bean>    
  138.         
  139.     <bean id="adminService" class="com.eportal.service.AdminServiceImpl">    
  140.         <property name="dao" ref="dao" />    
  141.     bean>    
  142.         
  143.     <bean id="columnsService" class="com.eportal.service.ColumnsServiceImpl">    
  144.         <property name="dao" ref="dao" />    
  145.     bean>    
  146.         
  147.         
  148.     <bean id="ordersService" class="com.eportal.service.OrderServiceImpl">    
  149.         <property name="dao" ref="dao" />    
  150.     bean>    
  151.         
  152.     <bean id="trafficService" class="com.eportal.service.TrafficServiceImpl">    
  153.         <property name="dao" ref="dao" />    
  154.     bean>    
  155.         
  156.     <bean id="adminAction" class="com.eportal.struts.action.    
  157. AdminAction"    
  158.         scope="prototype">    
  159.         <property name="service" ref="adminService" />    
  160.     bean>    
  161.         
  162.     <bean id="columnsAction" class="com.eportal.struts.action.    
  163. ColumnsAction"    
  164.         scope="prototype">    
  165.         <property name="service" ref="columnsService" />    
  166.     bean>    
  167.         
  168.     <bean id="newsAction" class="com.eportal.struts.action.    
  169. NewsAction"    
  170.         scope="prototype">    
  171.         <property name="service" ref="newsService" />    
  172.         <property name="columnsService" ref="columnsService" />    
  173.     bean>    
  174.         
  175.     <bean id="crawlAction" class="com.eportal.struts.action.    
  176. CrawlAction"    
  177.         scope="prototype">    
  178.         <property name="service" ref="crawlService" />    
  179.         <property name="columnsService" ref="columnsService" />    
  180.     bean>    
  181.         
  182. beans>     

你可能感兴趣的:(java)