Spring框架配置文件 application.xml 示例

  注解版

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 4     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
10         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
11         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
13         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
14         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
15         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
16         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
17     <!-- 
18     #开启动态注解扫描,spring容器会根据base-package设置的包(含子包)下扫描所有java类 
19     a.注解下spring管理组件的方式:
20         @Scope("singleton/prototype/request/session")  设置组件的作用域    
21             singleton是单例    
22             prototype是非单例模式,默认延迟
23             request 每次http请求有各自的bean
24             session 一次会话使用一个bean 实例    
25         @Lazy(true/false) 延迟加载
26             true 延迟加载
27             false 非延迟加载
28         @PostConstruct  //该注解告诉spring这个方法是当前bean的  初始化的时候调用的方法
29         @PreDestroy  //销毁的时候调用的方法    只在单例模式才能生效@Scope("singleton")
30     b.注解下的类扫描和依赖注入
31         @Component    :泛指组件,当不好归类时使用
32         @Repository    :标注dao类
33         @Service    :标注业务类
34         @Controller    :标注控制器类
35     c.依赖关系的注入:
36         注意:a.此类注解可以在属性上面设置,也可以在set方法上面设置
37             b.一般用于属性上,因为注解实现下set方法是可以省略不写的
38             c.下面的多个依赖注解,只需要使用一个Resource即可        
39         @Value(值)     给一些基本类型(包括String)赋值的注解
40     d.引用关系的依赖注入:
41         @Resource /@Resource(name="xxx") 用于引用类型属性注入,先按名称查找,再按类型查找
42         @Autowired/@Qualifier("xxx")
43             @Autowired 按照类型查找,再按照名称查找
44              @Qualifier("empDaoImpl") 按照指定bean的名称查找
45     -->
46     <context:component-scan base-package="根包名"></context:component-scan>
49 </beans>

配置版

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  4     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
  6     xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  7     xsi:schemaLocation="
  8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
 10         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
 11         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
 12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
 13         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
 14         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
 15         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
 16         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
 17     <!-- spring 读取配置文件的标记并根据标记通过反射创建对象 -->
 18     <!-- ====bean实例化例子 ====
 19         #使用构造器创建对象 #
 20         <bean id="唯一标识" name="标识" class="类路径"></bean>
 21 
 22         #使用静态工厂实例化
 23         <bean id="唯一标识" class="类路径" > </bean>
 24         <bean id="唯一标识" class="类路径" factory-bean="创建对象的工厂bean对象"  factory-method="创建对象方法名"> </bean>
 25         
 26         #使用工厂实例化
 27         <bean id="唯一标识" factory-bean="继承标识" factory-method="创建方法"></bean>
 28         <bean id="继承标识" class="类路径"></bean>
 29 
 30         #延时实例化
 31         ==关键属性 :lazy-init="true"
 32         <bean id="唯一标识" class="类路径" lazy-init="true"></bean>
 33 
 34         #实例化作用域
 35             属性scope的值:singleton 默认单例
 36                         prototype 多对象
 37                         request 每次http请求有各自的bean
 38                         session 一次会话使用一个bean 实例        
 39         <bean id="唯一标识" class="类路径" scope="prototype"></bean>
 40         
 41         #有参构造方法注入
 42         <bean id="user1" class="beans.User">
 43             <constructor-arg index="0" value="骆武辉" type="java.lang.String"></constructor-arg>
 44             <constructor-arg index="1" value="luowuhui" type="java.lang.String"></constructor-arg>
 45         </bean>
 46         
 47         #属性注入
 48         <bean id="user2" class="beans.User">
 49             <property name="name" value="骆武辉01"></property>
 50             <property name="password" value="luowuhui"></property>
 51         </bean>
 52         
 53         #自动装配 
 54         <bean id="user3" class="beans.User" autowire="byType"></bean>
 55                  
 56         #属性注入常见集合 
 57         <bean id="exampleBean" class="beans.ExampleBean">
 58             #list集合
 59             <property name="cities">
 60                 <list>
 61                     <value>值</value>
 62                 </list>
 63             </property>
 64             #set集合
 65             <property name="names">
 66                 <set>
 67                     <value>值</value>
 68                 </set>
 69             </property>
 70             #map集合
 71             <property name="map">
 72                 <map>
 73                     <entry>
 74                         <key>
 75                             <value>键名</value>
 76                         </key>
 77                         <value>值</value>
 78                     </entry>
 79                 </map>
 80             </property>
 81             #properties类型的值注入 一
 82             <property name="标识名">
 83                 <props> <prop key="user">root</prop>
 84                 <prop key="password">123123</prop>
 85                 <prop key="url">jdbc:mysql://localhost:3306/test&amp;characterEncoding=utf-8</prop> 
 86                 <prop key="driver">com.mysql.jdbc.Driver</prop>
 87             </props> </property> 
 88             #properties类型的值注入 二
 89             #通过读.properties 方式注入 
 90             #读取配置文件转成Properties对象 id:唯一标识 location:从哪里读取文件 classpath: 通过类路径开始读取 
 91             <util:properties id="props" location="classpath:db.properties"></util:properties>
 92             <property name="props" ref="props"></property>
 93         </bean>
 94     -->
163 </beans>
原文地址:https://www.cnblogs.com/sylwh/p/7793265.html