首发地址: http://inmethetiger.iteye.com/blog/1685973
主要涉及构造方法注入,静态工厂,实例化工厂,set方法这几类方法实例化bean
1:使用空构造器进行定义,使用此种方式,class属性指定的类必须有空构造器
接口:
package com.yiyou.spring;
public interface IHello {
public String sayHello();
}
接口实现:
package com.yiyou.spring;
public class HelloImpl implements IHello {
private String message;
//空的构造方法
public HelloImpl() {
this.message = "Hello Spring Use Empty args Constructor";
}
@Override
public String sayHello() {
return message;
}
}
配置
<?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-3.0.xsd">
<!-- 使用空构造器进行定义,使用此种方式,class属性指定的类必须有空构造器 -->
<bean name="bean1" class="com.yiyou.spring.HelloImpl"></bean>
</beans>
测试:
@Test
public void test1() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(
"beans.xml");
IHello iHello = beanFactory.getBean("bean1", IHello.class);
iHello.sayHello();
log.info(iHello.sayHello());//Hello Spring Use Empty args Constructor
}
结果:Hello Spring Use Empty args Contructor
2:使用含有参数的构造器,使用此种方式,class属性指定的类必须含有该参数的构造器
接口一样。
接口实现:
package com.yiyou.spring;
public class HelloImpl implements IHello {
private String message;
public HelloImpl(String message) {
this.message = message;
}
@Override
public String sayHello() {
return message;
}
}
配置
<?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-3.0.xsd">
<!-- 使用含有参数的构造器,使用此种方式,class属性指定的类必须含有该参数的构造器 -->
<bean id="bean2" class="com.yiyou.spring.HelloImpl">
<constructor-arg index="0" value="Hello Spring by Constructor"></constructor-arg>
</bean>
</beans>
测试
@Test
public void test2() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(
"beans.xml");
IHello iHello = beanFactory.getBean("bean2", IHello.class);
iHello.sayHello();
log.info(iHello.sayHello());//Hello Spring by Constructor
}
结果:Hello Spring by Constructor表明使用带参数构造函数
3:使用静态工厂实例化bean,使用此种方法必须指定class属性,还要指定factory-method属性来指定实例化bean的方法,而且使用静态化方法也允许指定方法参数
1:接口。同上
2:同带参数的构造方法相同
3:需要一个静态工厂方法,返回一个接口
package com.yiyou.spring;
public class HelloStaticFactory {
//工厂方法
public static IHello newInstance(String message){
return new HelloImpl(message);
}
}
4:配置
<?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-3.0.xsd"> <!-- 使用静态工厂实例化bean,使用此种方法必须指定class属性,还要指定factory-method属性来指定实例化bean的方法,而且使用静态化 方法也允许指定方法参数 --> <bean id="bean" class="com.yiyou.spring.HelloStaticFactory" factory-method="newInstance"> <constructor-arg index="0" value="静态方法初始化bean"></constructor-arg> </bean> </beans>
测试:
@Test
public void testStatic() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(
"beans.xml");
IHello iHello = beanFactory.getBean("bean", IHello.class);
iHello.sayHello();
log.info(iHello.sayHello());// 静态方法初始化bean
}
4:使用实例化工厂实例化bean,使用此种方法不能指定class属性,此刻必须使用factory-bean属性来指定工厂bean,同 样可以使用参数
一般都同上,只有以下不同
工厂方法:
package com.yiyou.spring;
public class HelloInstanceFactory {
//实例化工厂方法
public IHello newInstance(String message){
return new HelloImpl(message);
}
}
2:配置文件:
<?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-3.0.xsd"> <!-- 使用实例化工厂实例化bean,使用此种方法不能指定class属性,此刻必须使用factory-bean属性来指定工厂bean,同样可以使用参数 --> <!-- 实例化工厂bean初始化 --> <bean id="factoryBean" class="com.yiyou.spring.HelloInstanceFactory"></bean> <bean id="bean" factory-bean="factoryBean" factory-method="newInstance"> <constructor-arg index="0" value="实例化工厂方法初始化bean"></constructor-arg> </bean> </beans>
对静态工厂实例化和实例化工厂实例化来说,主要在配置文件中可以看出来。静态工厂中的配置文件只需要指定工厂类和工厂类的实例化方法。
而实例化工厂则需要实例化工厂类并且在实例化类中指定factory-bean
5:使用set方法注入。这个是最常见的一种,注入的对象必须存在set方法。
接口同上
实现
package com.yiyou.spring;
public class HelloImpl implements IHello {
private String message;
public HelloImpl() {
super();
// TODO Auto-generated constructor stub
}
public HelloImpl(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String sayHello() {
return message;
}
}
其中可以去掉构造方法。
<?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-3.0.xsd"> <!-- 使用set注入,使用此种方式,注入对象必须存在set方法 --> <bean name="hello" class="com.yiyou.spring.HelloImpl"> <property name="message" value="这是使用set方法注入的"></property> </bean> </beans>
测试:
@Test
public void test1() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(
"beans.xml");
IHello iHello = beanFactory.getBean("hello", IHello.class);
iHello.sayHello();
log.info(iHello.sayHello());//这是使用set方法注入的
}
------------------------------------------------防广告线-----------------------------------------------------------------