Bean注入
- 源代码在最后
- 构造方法注入
- 索引匹配注入
- 参数类型匹配注入
- 引用其他Bean
- 内部bean
- null值
- 级联属性
- 集合属性
- Map
- Properties
- 集合bean
- p命名
- 具体代码
- Car.java(com.test.sping.beans.Car)
- Person.java(com.test.sping.beans.Person)
- Main.java(com.test.sping.beans.Main)
- Person.java(com.test.sping.beans.collections.Person)
- NewPerson.java(com.test.sping.beans.collections.NewPerson)
- DataSource.java(com.test.sping.beans.collections.DataSource)
- Main.java(com.test.sping.beans.collections.Main)
- applicationContext.xml
源代码在最后
构造方法注入
索引匹配注入
<!-- 通过构造方法来配置 bean 的属性 -->
<bean id="car" class="com.test.sping.beans.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="Shanghai" index="1"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean>
参数类型匹配注入
<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器! -->
<bean id="car2" class="com.test.sping.beans.Car">
<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
<!-- 如果字面值包含特殊字符可以使用 <![CDATA[]]> 包裹起来 -->
<!-- 属性值也可以使用 value 直接点进去配置 -->
<constructor-arg type="java.lang.String">
<value><![CDATA[<Shanghai^>]]></value>
</constructor-arg>
<constructor-arg type="int">
<value>250</value>
</constructor-arg>
</bean>
引用其他Bean
<bean id="person" class="com.test.sping.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<!-- 可以是用 property 的 ref 属性建立 bean 之间的引用关系。 -->
<!-- 方法一
<property name="car" ref="car2"></property>
-->
<!-- 方法二 -->
<property name="car">
<ref bean="car2"></ref>
</property>
</bean>
内部bean
<bean id="person" class="com.test.sping.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<!-- 内部bean,不能被外部引用,只能在内部使用,故设置 id 没有用,不用设置 -->
<property name="car">
<bean class="com.test.sping.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Changan"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg>
</bean>
</property>
<property name="car.maxSpeed" value="260"></property>
</bean>
null值
<bean id="person2" class="com.test.sping.beans.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<!-- 测试赋值 null -->
<constructor-arg><null></null></constructor-arg>
</bean>
级联属性
<bean id="person2" class="com.test.sping.beans.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<constructor-arg ref="car"></constructor-arg>
<!-- 为级联属性赋值。 注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常,和 Struts2 不同。 -->
<property name="car.maxSpeed" value="250"></property>
</bean>
集合属性
<!-- 测试如何配置集合属性 -->
<bean id="person3" class="com.test.sping.beans.collections.Person">
<property name="name" value="Mike"></property>
<property name="age" value="27"></property>
<property name="cars">
<!-- 使用 list 节点为 List 类型的属性赋值 -->
<list>
<ref bean="car"></ref>
<ref bean="car2"></ref>
<bean class="com.test.sping.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Changan"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg>
</bean>
</list>
</property>
</bean>
Map
<!-- 配置 Map 属性值 -->
<bean id="newPerson" class="com.test.sping.beans.collections.NewPerson">
<property name="name" value="Rose"></property>
<property name="age" value="28"></property>
<property name="cars">
<!-- 使用 map 节点及 map 的 entry 子节点配置 Map 类型的成员变量 -->
<map>
<entry key="AA" value-ref="car"></entry>
<entry key="BB" value-ref="car2"></entry>
</map>
</property>
</bean>
Properties
<!-- 配置 Properties 属性值 -->
<bean id="dataSource" class="com.test.sping.beans.collections.DataSource">
<property name="properties">
<!-- 使用 props 和 prop 子节点来为 Properties 属性赋值 -->
<props>
<prop key="user">root</prop>
<prop key="password">123456</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="dricerClass">com.mysql.jdbc.Dricer</prop>
</props>
</property>
</bean>
集合bean
<!-- 配置单例的集合 bean,以供多个 bean 进行引用,需要导入 util 命名空间 -->
<util:list id="cars">
<ref bean="car"></ref>
<ref bean="car2"></ref>
</util:list>
p命名
<bean id="person4" class="com.test.sping.beans.collections.Person">
<property name="name" value="Jack"></property>
<property name="age" value="29"></property>
<property name="cars" ref="cars"></property>
</bean>
<!-- 通过 p 命名空间为 bean 的属性赋值,需要先导入 p 命名空间,相对于传统的配置方式更加的简洁。 -->
<bean id="person5" class="com.test.sping.beans.collections.Person"
p:age="30" p:name="Queen" p:cars-ref="cars"></bean>
具体代码
Car.java(com.test.sping.beans.Car)
package com.test.sping.beans;
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpeed;
public Car(String brand, String corp, double price) {
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", corp='" + corp + '\'' +
", price=" + price +
", maxSpeed=" + maxSpeed +
'}';
}
public void setPrice(double price) {
this.price = price;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
Person.java(com.test.sping.beans.Person)
package com.test.sping.beans;
public class Person {
private String name;
private int age;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
}
public Person() {
}
public Person(String name, int age, Car car) {
this.name = name;
this.age = age;
this.car = car;
}
}
Main.java(com.test.sping.beans.Main)
package com.test.sping.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// 1. 创建 Spring 的 IOC 容器对象
// ApplicationContext 代表 IOC 容器
// ClassPathXmlApplicationContext: 是 ApplicationContext 接口的实现类,该实现类路径下来加载配置文件。
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从 IOC 容器中获取 Bean 实例
// 利用 id 定位到 IOC 容器中的 bean
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
// 利用类型返回 IOC 容器中的 Bean,但要求 IOC 容器中必须只能有一个该类型的 Bean
// HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
System.out.println(helloWorld);
// 3. 调用 hello 方法
helloWorld.hello();
Car car = (Car) ctx.getBean("car");
System.out.println(car);
car = (Car) ctx.getBean("car2");
System.out.println(car);
Person person = (Person) ctx.getBean("person");
System.out.println(person);
person = (Person) ctx.getBean("person2");
System.out.println(person);
}
}
Person.java(com.test.sping.beans.collections.Person)
package com.test.sping.beans.collections;
import com.test.sping.beans.Car;
import java.util.List;
public class Person {
private String name;
private int age;
private List<Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", cars=" + cars +
'}';
}
public Person() {
}
public Person(String name, int age, List<Car> cars) {
this.name = name;
this.age = age;
this.cars = cars;
}
}
NewPerson.java(com.test.sping.beans.collections.NewPerson)
package com.test.sping.beans.collections;
import com.test.sping.beans.Car;
import java.util.List;
import java.util.Map;
public class NewPerson {
private String name;
private int age;
private Map<String, Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Map<String, Car> getCars() {
return cars;
}
public void setCars(Map<String, Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "NewPerson{" +
"name='" + name + '\'' +
", age=" + age +
", cars=" + cars +
'}';
}
public NewPerson() {
}
public NewPerson(String name, int age, Map<String, Car> cars) {
this.name = name;
this.age = age;
this.cars = cars;
}
}
DataSource.java(com.test.sping.beans.collections.DataSource)
package com.test.sping.beans.collections;
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 +
'}';
}
}
Main.java(com.test.sping.beans.collections.Main)
package com.test.sping.beans.collections;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) ctx.getBean("person3");
System.out.println(person);
person = (Person) ctx.getBean("person4");
System.out.println(person);
person = (Person) ctx.getBean("person5");
System.out.println(person);
NewPerson newPerson = (NewPerson) ctx.getBean("newPerson");
System.out.println(newPerson);
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getProperties());
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--
配置 bean
class: bean 的全类名,通过反射的方式在 IOC 容器中创建 Bean , 所以要求 Bean 中必须有无参数的构造器
id: 标识容器中的 bean. id 唯一。
-->
<bean id="helloWorld" class="com.test.sping.beans.HelloWorld">
<property name="name" value="Sping"></property>
</bean>
<!-- 通过构造方法来配置 bean 的属性 -->
<bean id="car" class="com.test.sping.beans.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="Shanghai" index="1"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean>
<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器! -->
<bean id="car2" class="com.test.sping.beans.Car">
<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
<!-- 如果字面值包含特殊字符可以使用 <![CDATA[]]> 包裹起来 -->
<!-- 属性值也可以使用 value 直接点进去配置 -->
<constructor-arg type="java.lang.String">
<value><![CDATA[<Shanghai^>]]></value>
</constructor-arg>
<constructor-arg type="int">
<value>250</value>
</constructor-arg>
</bean>
<bean id="person" class="com.test.sping.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<!-- 可以是用 property 的 ref 属性建立 bean 之间的引用关系。 -->
<!-- 方法一
<property name="car" ref="car2"></property>
-->
<!-- 方法二
<property name="car">
<ref bean="car2"></ref>
</property>
-->
<!-- 内部bean,不能被外部引用,只能在内部使用,故设置 id 没有用,不用设置 -->
<property name="car">
<bean class="com.test.sping.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Changan"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg>
</bean>
</property>
<property name="car.maxSpeed" value="260"></property>
</bean>
<bean id="person2" class="com.test.sping.beans.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<!--
<constructor-arg ref="car"></constructor-arg>
-->
<!-- 测试赋值 null -->
<!--
<constructor-arg><null></null></constructor-arg>
-->
<constructor-arg ref="car"></constructor-arg>
<!-- 为级联属性赋值。 注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常,和 Struts2 不同。 -->
<property name="car.maxSpeed" value="250"></property>
</bean>
<!-- 测试如何配置集合属性 -->
<bean id="person3" class="com.test.sping.beans.collections.Person">
<property name="name" value="Mike"></property>
<property name="age" value="27"></property>
<property name="cars">
<!-- 使用 list 节点为 List 类型的属性赋值 -->
<list>
<ref bean="car"></ref>
<ref bean="car2"></ref>
<bean class="com.test.sping.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Changan"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg>
</bean>
</list>
</property>
</bean>
<!-- 配置 Map 属性值 -->
<bean id="newPerson" class="com.test.sping.beans.collections.NewPerson">
<property name="name" value="Rose"></property>
<property name="age" value="28"></property>
<property name="cars">
<!-- 使用 map 节点及 map 的 entry 子节点配置 Map 类型的成员变量 -->
<map>
<entry key="AA" value-ref="car"></entry>
<entry key="BB" value-ref="car2"></entry>
</map>
</property>
</bean>
<!-- 配置单例的集合 bean,以供多个 bean 进行引用,需要导入 util 命名空间 -->
<util:list id="cars">
<ref bean="car"></ref>
<ref bean="car2"></ref>
</util:list>
<bean id="person4" class="com.test.sping.beans.collections.Person">
<property name="name" value="Jack"></property>
<property name="age" value="29"></property>
<property name="cars" ref="cars"></property>
</bean>
<!-- 通过 p 命名空间为 bean 的属性赋值,需要先导入 p 命名空间,相对于传统的配置方式更加的简洁。 -->
<bean id="person5" class="com.test.sping.beans.collections.Person"
p:age="30" p:name="Queen" p:cars-ref="cars"></bean>
</beans>