Spring_(4)属性配置细节
字面值
- 字面值:可用字符串表示的值,可以通过元素标签或value属性进行注入。
- 基本数据类型及其封装类、String等类型都可以采取字面值注入的方式。
- 若字面值中包含特殊字符,可以使用<![CDATA[[这里填特殊字符]]>把字面值包裹起来。
引用其他Bean
- 组成应用程序的Bean经常需要相互协作以完成应用程序的功能。要使Bean能够相互访问,就必须在Bean配置文件中指定对Bean的利用。
- 在Bean的配置文件中,可以通过元素****或ref属性为Bean的属性或构造器参数指定对Bean的引用。
- 也可以在属性或构造器里包含Bean的声明,这样的Bean称为内部Bean
注入参数详解:null值和级联属性
- 可以使用专用的元素标签为Bean的字符串或其他对象类型的属性注入null值
- 和Struts.Hibernate等框架一样,Spring支持级联属性的配置
例子程序
程序基本结构
Car.java
package com.spring.beans;
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpead;
public Car(String brand,String corp,double price){
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand,String corp,int maxSpead){
super();
this.brand = brand;
this.corp = corp;
this.maxSpead = maxSpead;
}
public void setPrice(double price) {
this.price = price;
}
public void setMaxSpead(int maxSpead) {
this.maxSpead = maxSpead;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", corp='" + corp + '\'' +
", price=" + price +
", maxSpead=" + maxSpead +
'}';
}
}
HelloWorld.java
package com.spring.beans;
public class HelloWorld {
private String name;
public void setName(String name){
System.out.println("setName"+name);
this.name = name;
}
public void hello(){
System.out.println("hello:"+ name);
}
public HelloWorld(){
System.out.println("HelloWorld Constractor...");
}
}
Person.java
package com.spring.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){
super();
this.name = name;
this.age = age;
this.car = car;
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置bean
class:bean 的全类名, 通过反射的方式 在IOC容器中创建Bean, 所以要求Bean中必须有无参数的构造器
id: 标识容器中的 bean, id 唯一,
-->
<bean id ="helloworld" class="com.spring.beans.HelloWorld">
<property name="name" value="Spring"></property><!--这个是通过setter方法进行最常用的属性注入-->
</bean>
<!--通过构造方法来配置bean的属性-->
<bean id="car" class="com.spring.beans.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="300000" index="2"></constructor-arg>
</bean>
<!--使用构造器注入属性值可以指定参数的位置和类型!以区分重载的构造器!-->
<bean id="car2" class="com.spring.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.spring.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"/>
</property>-->
<!--内部bean,不能被外部引用,只能在内部使用-->
<property name="car">
<bean class="com.spring.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.maxSpead" value="260"></property>
</bean>
<bean id="person2" class="com.spring.beans.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<!--<constructor-arg ref="car"></constructor-arg>-->
<!--测试赋值null-->
<!--<constructor-arg><null/></constructor-arg>-->
<constructor-arg ref="car"></constructor-arg>
<!--为级联属性赋值,注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常,和Struts不同-->
<property name="car.maxSpead" value="250"></property>
</bean>
<!--<bean id ="helloworld2" class="com.spring.beans.HelloWorld">
<property name="name" value="Spring"></property>
</bean>-->
</beans>
Main.java
package com.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
/* //创建HelloWorld的一个对象
HelloWorld helloWorld = new HelloWorld();
//为name 属性赋值
helloWorld.setName("atguigu");*/
//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);
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 person2 = (Person) ctx.getBean("person2");
System.out.println(person2);
//3.调用hello方法
//helloWorld.hello();
}
}