使用xml配置Bean属性之前需要先定义对应的XML和对应xsd文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
依赖注入主要有三种方式:
1.setter注入
2.构造器注入
3.接口注入
由于接口注入使用很少,这里主要写下前面两种注入属性的方式。
1.使用setter方式
Java文件代码:
package com.study.beans;
public class HelloWorld {
private String name;
public void setName(String name) {
System.out.println("setName()"+name);
this.name = name;
}
public HelloWorld(){
System.out.println("HelloWorld的无参构造器");
}
public void hello(){
System.out.println("hello"+name);
}
}
xml文件代码:
<!--setter方式属性注入 -->
<bean id="helloWorld" class="com.study.beans.HelloWorld">
<property name="name" value="zhang"></property>
</bean>
测试代码:
public class TestDemo {
@Test
public void test(){
//1.加载spring配置文件,根据配置创建对象
//ClassPathXmlApplicationContext是ApplicationContext接口的实现类,该类从类路径下加载配置文件
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取Bean
HelloWorld helloWorld=(HelloWorld) app.getBean("helloWorld");
System.out.println(helloWorld);
}
}
测试结果:
HelloWorld的无参构造器
setName()zhang
2.使用构造器方式
Java文件代码:
package com.study.beans;
public class CarDemo {
private String brand;
private String corp;
private double price;
private int maxSpeed;
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public CarDemo(String brand, String corp, double price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
public CarDemo(String brand, String corp, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
public CarDemo(String brand, String corp, double price, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
this.maxSpeed = maxSpeed;
}
@Override
public String toString() {
return "CarDemo [brand=" + brand + ", corp=" + corp + ", price="
+ price + ", maxSpeed=" + maxSpeed + "]";
}
}
xml文件代码:
<!--构造器方式属性注入 -->
<bean id="carDemo" class="com.study.beans.CarDemo">
<constructor-arg value="BMW"></constructor-arg>
<constructor-arg value="HEFEI"></constructor-arg>
<constructor-arg value="6900"></constructor-arg>
</bean>
3.属性值特殊字符的注入,如xml中的特殊字符"<"">"使用<!CDATA[xxx]>
xml文件代码:
<!--特殊字符可以使用<!CDATA[xxx]>方式注入 -->
<bean id="carDemo1" class="com.study.beans.CarDemo">
<constructor-arg index="0" type="String">
<value><![CDATA[<DGDG>]]></value>
</constructor-arg>
<constructor-arg index="1" type="String">
<value>822</value>
</constructor-arg>
<constructor-arg value="6900" index="2" type="int"></constructor-arg>
</bean>
测试结果:
CarDemo [brand=<DGDG>, corp=822, price=0.0, maxSpeed=6900]
4.null值的注入
<!-- null值使用<null/>注入 -->
<bean id="carDemo2" class="com.study.beans.CarDemo">
<constructor-arg value="Bmw" index="0" type="String"></constructor-arg>
<constructor-arg>
<null/>
</constructor-arg>
<constructor-arg >
<value>822.0</value>
</constructor-arg>
</bean>
5.使用ref引用其他Bean,引用的是其他Bean的id
java文件代码:
package com.study.beans;
public class Person {
private String name;
private int age;
private CarDemo car;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public CarDemo getCar() {
return car;
}
public void setCar(CarDemo car) {
this.car = car;
}
public Person() {
}
public Person(String name, int age, CarDemo car) {
super();
this.name = name;
this.age = age;
this.car = car;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
xml文件代码:
<!-- 使用ref引用其他Bean -->
<bean id="person1" class="com.study.beans.Person">
<property name="name" value="zhangshiwei"></property>
<property name="age" value="23"></property>
<property name="car" ref="carDemo2"></property>
</bean>
6.使用内部Bean
<!-- 内部Bean,只能在内部使用,不能被外部引用 -->
<bean id="person2" class="com.study.beans.Person">
<property name="name" value="zhangshiwei"></property>
<property name="age" value="23"></property>
<property name="car">
<bean class="com.study.beans.CarDemo">
<constructor-arg value="Bmw" index="0" type="String"></constructor-arg>
<constructor-arg><null/></constructor-arg>
<constructor-arg >
<value>822.0</value>
</constructor-arg>
</bean>
</property>
</bean>
7.为级联属性赋值
<!--为级联属性赋值 -->
<bean id="person3" class="com.study.beans.Person">
<constructor-arg name="name" value="zhangshiwei"></constructor-arg>
<constructor-arg name="age" value="23"></constructor-arg>
<constructor-arg name="car" ref="carDemo2"></constructor-arg>
<property name="car.maxSpeed" value="444"></property>
</bean>
上面代码中必须先初始化id为car的Bean,否则报错
8.为list集合属性赋值
将person类中的car属性改为:
public class PersonListCar {
private String name;
private int age;
private List<CarDemo> cars;
<!--为list集合属性赋值 -->
<bean id="personListCar" class="com.study.beans.PersonListCar">
<constructor-arg name="name" value="zhangshiwei"></constructor-arg>
<constructor-arg name="age" value="23"></constructor-arg>
<constructor-arg name="cars">
<list>
<ref bean="carDemo"/>
<ref bean="carDemo1"/>
<ref bean="carDemo2"/>
</list>
</constructor-arg>
</bean>
PersonListCar [name=zhangshiwei, age=23, cars=[CarDemo [brand=BMW, corp=HEFEI, price=0.0, maxSpeed=6900],
CarDemo [brand=<DGDG>, corp=822, price=0.0, maxSpeed=6900], CarDemo [brand=Bmw, corp=null, price=822.0, maxSpeed=444]]]
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
public class PersonMapCar {
private String name;
private int age;
private Map<String,CarDemo> car;
<!--为Map类型属性赋值 -->
<bean id="personMapCar" class="com.study.beans.PersonMapCar">
<constructor-arg name="name" value="zhangshiwei"></constructor-arg>
<constructor-arg name="age" value="23"></constructor-arg>
<constructor-arg name="car">
<map>
<entry key="car1" value-ref="carDemo"/>
<entry key="car2" value-ref="carDemo1"/>
<entry key="car3" value-ref="carDemo2"/>
</map>
</constructor-arg>
</bean>
PersonMapCar [name=zhangshiwei, age=23, car={car1=CarDemo [brand=BMW, corp=HEFEI, price=0.0, maxSpeed=6900],
car2=CarDemo [brand=<DGDG>, corp=822, price=0.0, maxSpeed=6900], car3=CarDemo [brand=Bmw, corp=null, price=822.0, maxSpeed=444]}]
package com.study.beans;
import java.util.Properties;
public class DataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "DataSource [properties=" + properties + "]";
}
}
<!--配置Properties类型属性值 -->
<bean id="dataSource" class="com.study.beans.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>
DataSource [properties={driverClass=com.mysql.jdbc.Driver, user=root, password=root, jdbcUrl=jdbc:mysql:///test}]
<!-- 使用util命名空间,配置独立的集合Bean,可供其他Bean引用 ,需要先引入util命名空间-->
<util:list id="car">
<ref bean="carDemo"/>
<ref bean="carDemo1"/>
<ref bean="carDemo2"/>
</util:list>
<bean id="personListCar1" class="com.study.beans.PersonListCar">
<constructor-arg name="name" value="zhangshiwei"></constructor-arg>
<constructor-arg name="age" value="23"></constructor-arg>
<constructor-arg name="cars" ref="car"></constructor-arg>
</bean>
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--使用p命名空间为Bean的属性赋值 ,需要先导入p命名空间-->
<bean id="personListCar2" class="com.study.beans.PersonListCar"
p:name="ZhangShiWei" p:age="23" p:cars-ref="car"></bean>
xmlns:p="http://www.springframework.org/schema/p"
<!--使用c命名空间注入构造参数的属性 ,需要先导入c命名空间 -->
<bean id="personListCar3" class="com.study.beans.PersonListCar"
c:_0="ZHANGSHIwei" c:_1="23" c:_2-ref="car"></bean>
xmlns:c="http://www.springframework.org/schema/c"