Bean的配置

先说一下两个概念

IoC:

DI:

 

ApplicationContext的实现类型有两种

1)ClassPathXmlApplicationContext:从类路径下加载配置文件

2)FileSystemXmlApplicationContext:从文件系统中加载配置文件

ApplicationContext初始化上下文是就就实例化所有单例的bean

从IoC容器中获取bean的实例

//从IoC容器中获取bean
HelloWorld helloWorld = (HelloWorld) cxt.getBean("helloWorld");
//缺点:当容器中配置多个类的实例,此种方法获取bean报异常
HelloWorld helloWorld = cxt.getBean(HelloWorld.class);

 

bean的注入有三种

1)属性setter方式注入

	<!-- 
		配置Bean
		class: bean的全类名,可以通过反射的方式在IOC容器中创建bean,所以要求bean中必须有无惨构造函数
		id:在IoC容器中唯一,不存在则使用全类名做标记(com.hous.spring.HelloWorld)
	 -->
	<bean id="helloWorld" class="com.hous.spring.HelloWorld">
		<property name="name" value="shanshanbox.com"></property>
	</bean>

 

 

 2)构造器的方式注入

 

	<!-- 通过构造器方式配置bean -->
	<bean id="car" class="com.hous.spring.Car">
		<constructor-arg value="pasate" 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.hous.spring.Car">
		<constructor-arg value="pasate" index="0"></constructor-arg>
		<constructor-arg value="shanghai" type="java.lang.String"></constructor-arg>
		<constructor-arg value="200" type="int"></constructor-arg>
	</bean>

 

 注意:字符串表示的值可以通过<value>元素标签或者value属性进行注入

基本类型及其封装类,String等类型都可以使用字面值注入

若字面值中包含特殊字符串,可以使用<![CDATA[]>把字面值包裹起来

	<!-- 可以通过参数位置和参数类型,区分重载的构造器 -->
	<bean id="car2" class="com.hous.spring.Car">
		<constructor-arg value="pasate" index="0"></constructor-arg>
		<!-- 含有特殊字符可以使用<![CDATA[]]>包裹起来 -->
		<constructor-arg type="java.lang.String">
			<value><![CDATA[<shanghai>]]></value>
		</constructor-arg>
		<constructor-arg type="int">
			<value>280</value>
		</constructor-arg>
	</bean>

在IoC容器中,关于bean之间的引用,可以使用ref属性或者<ref>标签

	<bean id="person" class="com.hous.spring.Person">
		<property name="name" value="hous"></property>
		<property name="age" value="25"></property>
		<!-- 使用bean的ref属性或标签建立bean之间的引用关系 -->
		<!-- <property name="car" ref="car"></property> -->
		<!-- 
		<property name="car">
			<ref bean="car" />
		</property> 
		-->
		<!-- 内部bean不能被外部bean引用 -->
		<property name="car">
			<bean class="com.hous.spring.Car">
				<constructor-arg value="mazida" index="0"></constructor-arg>
				<constructor-arg value="changan" index="1"></constructor-arg>
				<constructor-arg value="300000" type="double"></constructor-arg>
			</bean>
		</property>
	</bean>
	
	<bean id="person2" class="com.hous.spring.Person">
		<constructor-arg value="zhangsan"></constructor-arg>
		<constructor-arg value="28"></constructor-arg>
		<constructor-arg ref="car"></constructor-arg>
	</bean>

 在IoC容器中可以直接使用<null />赋值,或者使用car.price=300级联赋值

注意:级联赋值需要对bean对象初始化,后才可以使用级联赋值

关于集合的注入

Spring中可以通过内置的xml标签,如(<list>或<set>或<map>等配置集合属性)

在内部可以使用value指定简单值,使用ref引用别的bean

java.util.Map可以使用<map>标签,内部可以有多个<entry>做为子标签,每个<entry>有一个key和一个value值,可以将Map的键和值做为<entry>属性定义,简单常量使用key和value,bean的引用可以通过key-ref和value-ref引用

java.util.Properties可以使用<props>标签定义,内部存在多个<prop>做为子标签,必须定义key属性

1)使用List集合

	<bean id="person" class="com.hous.spring.Person">
		<property name="name" value="hous"></property>
		<property name="age" value="25"></property>
		<property name="cars">
			<list>
				<ref bean="car" />
				<ref bean="car2" />
			</list>
		</property> 
	</bean>
package com.hous.spring;

import java.util.List;

public class Person {
	private String name;
	private int age;
	private List<Car> cars;

	public String getName() {
		return name;
	}

	public Person() {
		super();
	}

	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 + "]";
	}
}
	<bean id="person" class="com.hous.spring.Person">
		<property name="name" value="shanshanbox"></property>
		<property name="age" value="25"></property>
		<property name="cars">
			<map>
				<entry key="AA" value-ref="car"></entry>
				<entry key="BB" value-ref="car2"></entry>
			</map>
		</property> 
	</bean>
	<bean id="dataSource" class="com.hous.spring.DataSource">
		<property name="properties">
			<props>
				<prop key="driverClass">com.mysql.jdbc.Driver</prop>
				<prop key="url">jdbc:mysql://test</prop>
				<prop key="user">root</prop>
				<prop key="pass">root</prop>
			</props>
		</property>
	</bean>

配置独立的集合bean供多个bean使用,还有可以使用p命名空间赋值

	<!-- 配置单例的集合bean供多个bean使用,需要引入util命名空间 -->
	<util:list id="cars">
		<ref bean="car"/>
		<ref bean="car2"/>
	</util:list>
	
	<bean id="person" class="com.hous.spring.Person">
		<property name="name" value="shanshanbox"></property>
		<property name="age" value="25"></property>
		<property name="cars" ref="cars"></property> 
	</bean>
	<!-- 通过p命名空间为bean的属性赋值,需要引用p命名空间 -->
	<bean id="person2" class="com.hous.spring.Person" 
		p:name="box" 
		p:age="28" 
		p:cars-ref="cars"
	/>

好了,就这么多。来一首根羊的小诗送个大家,呵呵

黑夜给了我一双黑色的眼睛,我要用它来寻找光明,

即使在那遥远的地方,也有驼铃的响起,

我的思念飘向远方的故乡。

猜你喜欢

转载自shuizhongyue.iteye.com/blog/2291926