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" xmlns:util="http://www.springframework.org/schema/util" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 10 11 <!-- 属性注入 通过set方法注入 --> 12 <bean id="helloWorld" class="com.spring.helloworld.HelloWorld"> 13 <property name="name" value="applicationContext_Spring"></property> 14 </bean> 15 <!-- 构造器 要求bean中有对应的构造器(无参构造) --> 16 <bean id="car" class="com.spring.helloworld.Car"> 17 <constructor-arg value="audo"></constructor-arg> 18 <constructor-arg> 19 <value><![CDATA[<ATARZA>]]></value> 20 </constructor-arg> 21 <constructor-arg value="200"></constructor-arg> 22 <constructor-arg value="200"></constructor-arg> 23 </bean> 24 <!-- 注入 --> 25 <bean id="car2" class="com.spring.helloworld.Car"> 26 <property name="name" value="car2Name"></property> 27 <property name="brand" value="car2Brand"></property> 28 <property name="price" value="1"></property> 29 <property name="speed" value="100000"></property> 30 </bean> 31 32 <!-- 引用 ref --> 33 <bean id="person" class="com.spring.helloworld.Person"> 34 <property name="name" value="jack"></property> 35 <property name="age" value="11"></property> 36 <property name="car" ref="car"></property> 37 </bean> 38 39 <!-- 集合属性 list --> 40 <bean id="personListCar" class="com.spring.helloworld.PersonListCar"> 41 <property name="name" value="jack"></property> 42 <property name="age" value="12"></property> 43 <property name="cars" ref="cars"> 44 <list> 45 <ref bean="car" /> 46 <ref bean="car2" /> 47 </list> 48 </property> 49 </bean> 50 51 <!-- 集合属性map --> 52 <bean id="personMapCar" class="com.spring.helloworld.PersonMapCar"> 53 <property name="name" value="toms"></property> 54 <property name="age" value="18"></property> 55 <property name="cars"> 56 <map> 57 <entry key="aaa" value-ref="car"></entry> 58 <entry key="bbb" value-ref="car2"></entry> 59 </map> 60 </property> 61 </bean> 62 63 <!-- properties map的子类 --> 64 <bean id="DataSource" class="com.spring.helloworld.DataSource"> 65 <property name="properties"> 66 <props> 67 <prop key="user">root</prop> 68 <prop key="password">123456</prop> 69 <prop key="jdbcurl">jdbc:mysql:///test</prop> 70 <prop key="driverClass">com.mysql.jdbc.Driver</prop> 71 </props> 72 </property> 73 </bean> 74 75 <!-- 配置单例的集合bean 以供多个bean引用 --> 76 <util:list id="cars"> 77 <ref bean="car" /> 78 <ref bean="car2" /> 79 </util:list> 80 81 <!-- 通过P命名空间为bean的属性配置 --> 82 <bean id="personP" class="com.spring.helloworld.Person" p:age="20" 83 p:name="pPerson"> 84 </bean> 85 86 87 <bean id="address" class="com.spring.autowire.Address" p:city="beijign" 88 p:street="lishuiqiao"> 89 </bean> 90 91 <bean id="car" class="com.spring.autowire.Car" p:name="mazida" 92 p:price="123"> 93 </bean> 94 <bean id="car2" class="com.spring.autowire.Car" p:name="mazida" 95 p:price="123"> 96 </bean> 97 98 99 <bean id="person" class="com.spring.autowire.Person" p:name="tom" 100 p:address-ref="address" p:car-ref="car"> 101 </bean> 102 <!-- 通过名字自动装配 根据bean的名字和当前bean的setter 风格的属性名进行自动装配 --> 103 <bean id="person" class="com.spring.autowire.Person" p:name="tom" 104 autowire="byName"> 105 </bean> 106 <!-- Person [name=tom, address=null, car=null] --> 107 108 <!-- 根据类型匹配 根据bean的类型和当前bean的属性类型进行自动装配 --> 109 <!-- 配置多个类型: [com.spring.autowire.Car] is defined: expected single matching 110 bean but found 2: car,car2 --> 111 <bean id="person" class="com.spring.autowire.Person" p:name="tom" 112 autowire="byType"> 113 </bean> 114 115 <!-- 生命周期 --> 116 <bean id="car" class="com.spring.cycle.Car" init-method="init" 117 destroy-method="destory"> 118 <property name="brand" value="audi"></property> 119 </bean> 120 121 <!-- 通过静态工厂方法配置bean 注意不是配置静态工厂方法实例,而是配置bean实例 class 工厂方法的全类名 factory-method 122 静态工厂的方法名 constructor-arg 静态工厂的参数 --> 123 <bean id="car1" class="com.spring.factory.staticCarFactory" 124 factory-method="getCar"> 125 <constructor-arg value="audi"></constructor-arg> 126 </bean> 127 <!-- 配置工厂的实例 --> 128 <bean id="carFactory" class="com.spring.factory.InstanceCarFactory"> 129 </bean> 130 <bean id="car2" factory-bean="carFactory" factory-method="getCar"> 131 <constructor-arg value="audi"></constructor-arg> 132 </bean> 133 134 <!-- 使用bean的scope属性来配置bean的范围 singleton:默认值,使用单例工厂模式(每次得到的都是同一个对象,用==作比较返回true) 135 prototype:原型的。容器初始化是不创建bean的实例。在每次请求是都创建一个新的bean实例,并返回 --> 136 137 <bean id="address" class="com.spring.scope.Address" p:city="beijing" 138 p:street="lishuiqiao" scope="singleton"> 139 </bean> 140 141 <!-- 抽象bean:bean的abstract属性为true ,这样的bean不能被IOC容器实例化,只能用来被继承 若某一个bean的class属性没有制定,则该bean必须是一个抽象bean --> 142 <bean id="address" p:city="beijign" p:street="lishuiqiao" 143 abstract="true"></bean> 144 145 <!-- bean的继承 使用bean的parent 属性指定继承 --> 146 147 <bean id="address2" class="com.spring.autowire.Address" p:street="dongzhimen" 148 parent="address"> 149 </bean> 150 <bean id="car" class="com.spring.autowire.Car" p:name="carName" 151 p:price="123"> 152 </bean> 153 154 <!-- bean的依赖,创建一个bean依赖于car这个bean --> 155 <bean id="person" class="com.spring.autowire.Person" p:name="jack" 156 p:address-ref="address2" depends-on="car"> 157 </bean> 158 159 <bean id="address" class="com.spring.spel.Address" p:city="beijign" 160 p:street="lishuiqiao"> 161 </bean> 162 163 <bean id="car" class="com.spring.spel.Car" p:name="mazida" 164 p:price="123"> 165 <!-- 使用spel引用类的属性的静态方法 --> 166 <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property> 167 </bean> 168 169 <bean id="person" class="com.spring.spel.Person"> 170 <property name="car" value="#{car}"></property> 171 <property name="name" value="tom"></property> 172 <!-- 使用spel 应用其他bean的属性 --> 173 <property name="city" value="#{address.city}"></property> 174 <!-- 使用spel 应用运算符 --> 175 <property name="info" value="#{car.price>30000?'白领':'金陵'}"></property> 176 177 </bean> 178 179 </beans>