目录
传统的Spring项目是基于XML配置文件来实例化Bean对象,最终实现由SpringIOC容器统一管理;对于Bean的实例化和属性注入,Spring框架提供了多种方式。
1、<property>标签实现属性注入
<property>表现实现Java Bean成员属性注入的方式对应于Java的setter方法,包括基本数据类型的value注入以及引用参数类型的ref注入,示例如下:
Java Bean定义:
package com.practice;
@Getter
@Setter
public class Owner{
private String name;
private String age;
}
@Getter
@Setter
public class Dog{
private Owner owner;
}
@Getter
@Setter
public class Cat{
private String age;
}
注意:实际Java源文件中,一个.java文件下只能包含一个public修饰的class,这里为了方便写到一起了!!!
以下是Spring XML配置文件:
<!-- 引用方式注入 -->
<bean id="owner" class="com.practice.Owner">
<property name="name" value="Tom" />
<property name="age" value="17" />
</bean>
<bean id="dog" class="com.practice.Dog">
<property name="owner" ref="owner" />
</bean>
<!-- 值注入 -->
<bean id="cat" class="com.practice.Cat">
<property name="age" value="3" />
</bean>
2、<constructor-arg>标签实现构造函数初始化
首先是Java Bean的定义:
package com.practice;
@Getter
@Setter
public class Owner{
private String name;
private String age;
}
public class Dog{
private Owner owner;
public Dog(Owner owner){
this.owner = owner;
}
}
以下是Spring XML配置文件:
<bean id="owner" class="com.practice.Owner">
<property name="name" value="Tom" />
<property name="age" value="17" />
</bean>
<bean id="dog" class="com.practice.Dog">
<constructor-arg ref="owner"/>
</bean>
当然,<constructor-arg>也支持value标签注入。
3、工厂方法实现Bean实例化及属性注入
3.1、静态工厂方法注入
package com.practice;
public WebContextExample{
private WebApplicationContext webContext;
public WebContextExample(WebApplicationContext webContext){
this.webContext = webContext;
System.out.println("WebContextExample 有参构造 ");
}
}
<!-- ContextLoader.getCurrentWebApplicationContext()是Springframework提供的一个静态方法,用于获取当前容器的WebApplicationContext对象,也就是web上下文对象 -->
<bean id="getCurrentWebApplicationContext" class="org.springframework.web.context.ContextLoader" factory-method="getCurrentWebApplicationContext"/>
<bean id="webContextExample" class="com.practice.WebContextExample">
<constructor-arg ref="getCurrentWebApplicationContext"/>
</bean>
3.2、非静态工厂方法注入
package com.practice;
public interface Animal {
void speak();
}
@Setter
@Getter
public class Cat implements Animal{
private Owner owner;
@Override
public void speak() {
owner.setPetType("Cat");
System.out.println("owner pet type: " + owner.getPetType());
System.out.println("Cat.speak");
}
}
@Setter
@Getter
public class Dog implements Animal{
private Owner owner;
@Override
public void speak() {
owner.setPetType("dog");
System.out.println("owner pet type: " + owner.getPetType());
System.out.println("Dog.speak");
}
}
@Getter
@Setter
public class Owner {
private String petType = null;
}
@Getter
@Setter
public class AnimalFactory{
private Owner owner;
public Animal create(String petType) {
if("dog".equalsIgnoreCase(petType)){
Dog dog = new Dog();
dog.setOwner(owner);
return dog;
} else if("cat".equalsIgnoreCase(petType)){
Cat cat = new Cat();
cat.setOwner(owner);
return cat;
}
return null;
}
}
<bean id="owner" class="com.practice.Owner"/>
<bean id="animalFactory" class="com.practice.AnimalFactory">
<property name="owner" ref="owner" />
</bean>
<bean id="cat" factory-bean="animalFactory" factory-method="create">
<constructor-arg value="cat"/>
</bean>
声明:本文内容是作者个人基于实践的理解,如有错误请指正;本文部分内容或图片参考互联网,如有侵权请联系删除,谢谢!