1.spring注入方式
1.1属性注入
即通过setter()方法注入bean的属性值或者依赖的对象,属性注入使用<property>,使name属性值对应bean
的属性名称,value属性或者<value>子节点指定属性值。
<bean id="hello" class="it.com.cn.spring.HelloWord">
<property name="name" value="张无忌"></property>
</bean>
1.2构造器注入
即通过构造方法注入bean的属性值或者依赖的对象,可以保证bean实例化后可以使用,使用<constructor-arg></constructor-arg>标签中声明属性
<!--通过构造方法注入-->
<bean id="car" class="it.com.cn.spring.Car">
<constructor-arg value="奥迪"></constructor-arg>
<constructor-arg value="红色"></constructor-arg>
<constructor-arg value="2000"></constructor-arg>
</bean>
<bean id="car" class="it.com.cn.spring.Car">
<constructor-arg value="奥迪"></constructor-arg>
<!-- <constructor-arg value="红色"></constructor-arg>-->
<constructor-arg type="java.lang.String">
<!--<![CDATA[]]>可以包含特殊符号-->
<value><![CDATA[<红色>]]></value>
</constructor-arg>
<constructor-arg >
<value>20000</value>
</constructor-arg>
</bean>
一个bean中引入其他bean
<!--bean中引入另一bean-->
<bean id="person" class="it.com.cn.spring.Person">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<property name="car" ref="car"></property>
</bean>
内部bean
<!--内部bean,不能被外部引用-->
<bean id="person2" class="it.com.cn.spring.Person">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<property name="car">
<bean class="it.com.cn.spring.Car">
<constructor-arg value="ford"></constructor-arg>
<constructor-arg value="蓝色"></constructor-arg>
<constructor-arg value="15200"></constructor-arg>
</bean>
</property>
</bean>
null值标记
<constructor-arg><null></constructor-arg>
为级联属性赋值
必须先将对象初始化,否则会抛异常
<property name="person.age" value="18"></property>
集合
<!--集合-->
<bean id="person3" class="it.com.cn.spring.Person">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<property name="cars">
<list>
<ref bean="car"></ref>
<ref bean="car2"></ref>
<ref bean="car3"></ref>
</list>
</property>
</bean>
map
<!--map-->
<bean id="person4" class="it.com.cn.spring.Person">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<property name="cars">
<list>
<ref bean="car"></ref>
<!--<ref bean="car2"></ref>
<ref bean="car3"></ref>-->
</list>
</property>
<property name="map">
<map>
<entry key="张无忌" value="赵敏"></entry>
<entry key="杨过" value="小龙女"></entry>
<entry key="周芷若" value="张无忌"></entry>
</map>
</property>
</bean>
配置properties
<!--配置properties-->
<bean id="dataSource" class="it.com.cn.spring.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
配置单个集合bean,以供其他bean使用
需要引入约束util
xmlns:util="http://www.springframework.org/schema/util"
<!--配置单个集合bean,以供其他bean使用-->
<util:list id="list">
<ref bean="car"></ref>
</util:list>
<bean id="person5" class="it.com.cn.spring.Person">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<property name="cars" ref="list"></property>
</bean>
p约束简化bean配置
xmlns:p="http://www.springframework.org/schema/p"
<bean id="person6" class="it.com.cn.spring.Person"
p:name="张无忌" p:age="18" p:cars-ref="list">
</bean>
spEl表达式