Spring 容器
在Spring IOC容器读取Bean配置创建Bean实例之前必须对它进行实例化,只有在容器实例化后才可以从IOC容器里获取Bean实例并使用。
Spring提供了两种类型的IOC容器实现:
1、BeanFactory:IOC容器的基本实现。
2、ApplicationContext:提供了更多的高级特性,是BeanFactory的子接口。
Spring 注入方式:
1.属性注入(setter方法注入) ---> Helloworld.java
2.构造器注入(<construct-arg>方法注入)-> Car.java
注入属性的细节:通过标志value的type来区别重载
字面值:可用字符串表示的值,可以通过<value>元素标签火value属性进行注入。
如果字面值中包含这特殊字符,可以使用<![CDATA[]]>把字面值包裹起来。
引用其他的Bean: --->Person.java
1.在Bean配置文件中,可以通过<ref>元素或ref属性为Bean的属性或构造器参数指定对Bean的引用。
2.也可以在属性 或 构造器里面包含Bean的声明,称为内部Bean. --person2
[注意构造器里面的话,要加上无参构造函数和有参构造函数,之后xml文件里面的是<construct-arg>来配置]
级联属性:注意属性需要先初始化后才可以为级联属性赋值 --person3
集合属性:为list配置 --person5 --->newPersonList
为map配置 --person6 --->newPersonMap
为properties配置 --person7 --->dataSource
使用util设置单例的集合bean:
<util:list id="cars">
<ref bean="car"/>
<ref bean="car"/>
</util:list>
这样这个集合就可以被其他的bean所共享。
代码:
1.主要的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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloworld" class="HelloWorld">
<property name="name" value="Spring"></property>
</bean>
<bean id="helloworld2" class="HelloWorld">
<property name="name" value="Spring"></property>
</bean>
<!--通过构造方法来配置bean的属性-->
<bean id="car" class="Car">
<constructor-arg value="Audi" index="1"></constructor-arg>
<constructor-arg value="Shanghai" index="0"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean>
<bean id="car2" class="Car">
<constructor-arg value="Ford"></constructor-arg>
<!--<constructor-arg value="Shanghai"></constructor-arg>-->
<!--如果字面值有含特殊字符可以使用<![CDATA[]]> 包裹起来 属性值也可以使用value 子节点进行配置-->
<constructor-arg type="java.lang.String">
<value><![CDATA[Shanghai-->]]></value>
</constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>
<bean id="car3" class="Car">
<constructor-arg value="Baoma"></constructor-arg>
<constructor-arg value="xiamen"></constructor-arg>
<constructor-arg type="int">
<value>266</value>
</constructor-arg>
</bean>
<!--///////////可以使用property的ref 属性建立bean 之间的引用关系-->
<bean id="person" class="Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car" ref="car2"></property>
</bean>
<!--<bean id="person2" class="Person">-->
<!--<property name="name" value="Tom"></property>-->
<!--<property name="age" value="24"></property>-->
<!--<property name="car">-->
<!--<ref bean="car3"/>-->
<!--</property>-->
<!--</bean>-->
<!--内部bean-->
<bean id="person2" class="Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car">
<bean class="Car">
<constructor-arg value="FordAnn"></constructor-arg>
<constructor-arg value="changan"></constructor-arg>
<constructor-arg value="456" type="int"></constructor-arg>
</bean>
</property>
</bean>
<!--构造器实现内部的bean-->
<bean id="person3" class="Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<constructor-arg ref="car2"></constructor-arg>
<!--为级联属性赋值.注意属性需要先初始化后才可以为级联属性赋值,否则会报异常-->
<property name="car.price" value="290000"></property>
</bean>
<!-- 测试如何配置List集合属性-->
<bean id="person5" class="newPersonList">
<property name="name" value="Mike"></property>
<property name="age" value="27"></property>
<property name="cars" >
<list>
<ref bean="car"/>
<ref bean="car2"/>
<ref bean="car3"/>
</list>
</property>
</bean>
<!-- 测试如何配置Map集合属性-->
<bean id="person6" class="newPersonMap">
<property name="name" value="Rose"></property>
<property name="age" value="27"></property>
<property name="cars">
<map>
<entry key="AA" value-ref="car"></entry>
<entry key="BB" value-ref="car2"></entry>
</map>
</property>
</bean>
<!-- 配置 Properties 属性值-->
<bean id="dataSource" class="myDataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdncurl">jdbc:mysql.jdbc.Driver</prop>
<prop key="driverClass">com.spring.hello</prop>
</props>
</property>
</bean>
</beans>
2.Car:
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpeed;
public Car(String brand,String crop,double price) {
super();
this.brand = brand;
this.corp = crop;
this.price = price;
}
public Car(String brand,String crop,int maxSpeed) {
super();
this.brand = brand;
this.corp = crop;
this.maxSpeed = maxSpeed;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Car{");
sb.append("brand='").append(brand).append('\'');
sb.append(", corp='").append(corp).append('\'');
sb.append(", price=").append(price);
sb.append(", maxSpeed=").append(maxSpeed);
sb.append('}');
return sb.toString();
}
}
3.Person:
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;
}
public Person() {
}
public Person(String name, int age, Car car) {
this.name = name;
this.age = age;
this.car = car;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Person{");
sb.append("name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append(", car=").append(car);
sb.append('}');
return sb.toString();
}
}
4.newPersonList:
public class newPersonList {
private String name;
private int age;
private List<Car> cars;
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = 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 newPersonList() {
}
public newPersonList(String name, int age, List<Car> cars) {
this.name = name;
this.age = age;
this.cars = cars;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Person{");
sb.append("name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append(", cars=").append(cars);
sb.append('}');
return sb.toString();
}
}
5.newPersonMap:
public class newPersonMap {
private String name;
private int age;
private Map<String,Car> cars;
public newPersonMap(String name, int age, Map<String, Car> cars) {
this.name = name;
this.age = age;
this.cars = cars;
}
public newPersonMap() {
}
public Map<String, Car> getCars() {
return cars;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("newPersonMap{");
sb.append("name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append(", cars=").append(cars);
sb.append('}');
return sb.toString();
}
public void setCars(Map<String, Car> cars) {
this.cars = cars;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
6.myDataSource:
public class myDataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("dataSource{");
sb.append("properties=").append(properties);
sb.append('}');
return sb.toString();
}
}
7.测试用的main文件:两个
第一个:
public class Main {
public static void main(String[] arg) {
//1.创建Spring 的IOC 容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC 容器中获取Bean实例
//利用id定位到IOC容器中的bean
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloworld2");
//利用类型返回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);
car = (Car) ctx.getBean("car3");
System.out.println(car);
Person person = (Person) ctx.getBean("person");
System.out.println(person);
person = (Person) ctx.getBean("person2");
System.out.println(person);
person = (Person) ctx.getBean("person3");
System.out.println(person);
}
}
第二个:
public class newMain {
public static void main(String[] arg){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
newPersonList person = (newPersonList) ctx.getBean("person5");
System.out.println(person);
newPersonMap newPersonMap = (newPersonMap) ctx.getBean("person6");
System.out.println(newPersonMap);
myDataSource dataSource = (myDataSource) ctx.getBean("dataSource");
System.out.println();
System.out.println(dataSource.getProperties());
}
}
项目层次:
推荐阅读