上一篇,用 setter 方式举例说明了 Spring 中的依赖注入支持的数据类型。
这篇,看看依赖注入的实现方式。
首先,bean 的配置文件可以通过 xml 和 properties 两种方式。其中 xml 是主流,properties 基本不用,具体实现方式:
setter 方法
构造器
接口回调
注解
API
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="ConstXiong" class="constxiong.impltype.User">
<constructor-arg>
<value>1</value>
</constructor-arg>
<property name="name" value="ConstXiong"/>
</bean>
<bean id="favorites" class="java.lang.String">
<constructor-arg>
<value>写代码、睡觉</value>
</constructor-arg>
</bean>
<!-- 开启注解能力 -->
<context:component-scan base-package="constxiong"/>
</beans>
bean 的类代码
package constxiong.impltype;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class User implements ApplicationContextAware {
//构造方法注入
private Integer id;
public User(Integer id) {
this.id = id;
}
// set 方法注入
private String name;
public void setName(String name) {
this.name = name;
}
//实现接口 ApplicationContextAware 及其回调方法注入 applicationContextAware
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
// xml 开启注解能力
// 使用 @Autowired 给属性字段注入
@Autowired
private String favorites;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", applicationContext=" + applicationContext +
", friend=" + favorites +
'}';
}
}
测试代码,包含 bean 通过 api 注册与注入
package constxiong.impltype;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 依赖注入的实现方式
*/
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring-dependency-injection-impltype.xml");
User constxiong = (User)context.getBean("ConstXiong");
System.out.println(constxiong);
//api 构造、注入、组装、注册 bean
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getBeanFactory();
AbstractBeanDefinition userBeanDefinition = new RootBeanDefinition(User.class);
//构造方法注入
beanFactory.registerBeanDefinition("ApiUser", userBeanDefinition);
ConstructorArgumentValues argValues = new ConstructorArgumentValues();
argValues.addIndexedArgumentValue(0, 1);
//set 方法注入
userBeanDefinition.setConstructorArgumentValues(argValues);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue(new PropertyValue("name", "ApiUser"));
userBeanDefinition.setPropertyValues(propertyValues);
System.out.println(beanFactory.getBean("ApiUser"));
}
}
结果打印
User{id=1, name='ConstXiong', applicationContext=org.springframework.context.support.ClassPathXmlApplicationContext@1a6c5a9e, started on Mon Jan 11 23:10:11 CST 2021, friend=写代码、睡觉}
User{id=1, name='ApiUser', applicationContext=org.springframework.context.support.ClassPathXmlApplicationContext@1a6c5a9e, started on Mon Jan 11 23:10:11 CST 2021, friend=写代码、睡觉}
代码加了注释很容易看懂,强调几点:
ConstXiong 备案号:苏ICP备16009629号-3