Spring 超类中带有@Autowired 的工厂 bean
Spring factory bean with @Autowired in superclass
我在 Spring 中实现了一个工厂 bean 来实例化超级 class 的不同子 class。我遇到的问题是 superclass 属性不是 @Autowired
(我猜是由于工厂方法中的 new
命令)。这是我的代码:
@Component
public class ConfigBeanImpl implements ConfigBean{
@Override
public String expandParam(String param) {
return String.format("expanded %s", param);
}
}
public abstract class FactoryBean {
@Autowired
protected ConfigBean configBean;
private String property;
protected FactoryBean() {
this.property = configBean.expandParam("property");
}
public abstract String getProperty();
public static FactoryBean GET(int id) {
return new FactoryBeanGet(id);
}
public static FactoryBean POST(String param){
return new FactoryBeanPost(param);
}
}
public class FactoryBeanGet extends FactoryBean {
private int id;
protected FactoryBeanGet(int id) {
this.id = id;
}
@Override
public String getProperty() {
return Integer.toString(id);
}
}
public class FactoryBeanPost extends FactoryBean {
private String param;
protected FactoryBeanPost(String param) {
this.param = param;
}
@Override
public String getProperty() {
return param;
}
}
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
FactoryBean bean = (FactoryBean) context.getBean("factoryBeanGet", 12);
System.out.println(bean.getProperty());
bean = (FactoryBean) context.getBean("factoryBeanPost", "test param");
System.out.println(bean.getProperty());
}
}
和 applicationContext.xml:
<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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.spring" />
<bean id="factoryBeanGet" scope="prototype" class="com.spring.bean.FactoryBean"
factory-method="GET">
</bean>
<bean id="factoryBeanPost" scope="prototype" class="com.spring.bean.FactoryBean"
factory-method="POST">
</bean>
protected ConfigBean configBean
属性 抽象 class FactoryBean
不是 @Autowired
因此是 null
并且构造函数抛出一个NullPointerException
。如果我将它放在每个 subclasses 中,它工作正常,但它会是重复的代码。有没有办法解决这个问题,还是我做错了什么?
设身处地为Spring着想。它必须实例化 FactoryBean
,然后初始化其 configBean
字段。所以,它要做的第一件事就是调用构造函数。然后,一旦对象存在,它将初始化对象的字段。如果对象尚不存在,它显然无法初始化该字段。因此,在调用构造函数时,该字段仍然为空。
使用构造函数注入,或使用注解的方法 @PostConstruct
来调用 configBean
.
也就是说,您尝试初始化的私有 property
字段未在任何地方使用,因此您也可以删除它,并删除 configBean
字段。
我在 Spring 中实现了一个工厂 bean 来实例化超级 class 的不同子 class。我遇到的问题是 superclass 属性不是 @Autowired
(我猜是由于工厂方法中的 new
命令)。这是我的代码:
@Component
public class ConfigBeanImpl implements ConfigBean{
@Override
public String expandParam(String param) {
return String.format("expanded %s", param);
}
}
public abstract class FactoryBean {
@Autowired
protected ConfigBean configBean;
private String property;
protected FactoryBean() {
this.property = configBean.expandParam("property");
}
public abstract String getProperty();
public static FactoryBean GET(int id) {
return new FactoryBeanGet(id);
}
public static FactoryBean POST(String param){
return new FactoryBeanPost(param);
}
}
public class FactoryBeanGet extends FactoryBean {
private int id;
protected FactoryBeanGet(int id) {
this.id = id;
}
@Override
public String getProperty() {
return Integer.toString(id);
}
}
public class FactoryBeanPost extends FactoryBean {
private String param;
protected FactoryBeanPost(String param) {
this.param = param;
}
@Override
public String getProperty() {
return param;
}
}
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
FactoryBean bean = (FactoryBean) context.getBean("factoryBeanGet", 12);
System.out.println(bean.getProperty());
bean = (FactoryBean) context.getBean("factoryBeanPost", "test param");
System.out.println(bean.getProperty());
}
}
和 applicationContext.xml:
<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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.spring" />
<bean id="factoryBeanGet" scope="prototype" class="com.spring.bean.FactoryBean"
factory-method="GET">
</bean>
<bean id="factoryBeanPost" scope="prototype" class="com.spring.bean.FactoryBean"
factory-method="POST">
</bean>
protected ConfigBean configBean
属性 抽象 class FactoryBean
不是 @Autowired
因此是 null
并且构造函数抛出一个NullPointerException
。如果我将它放在每个 subclasses 中,它工作正常,但它会是重复的代码。有没有办法解决这个问题,还是我做错了什么?
设身处地为Spring着想。它必须实例化 FactoryBean
,然后初始化其 configBean
字段。所以,它要做的第一件事就是调用构造函数。然后,一旦对象存在,它将初始化对象的字段。如果对象尚不存在,它显然无法初始化该字段。因此,在调用构造函数时,该字段仍然为空。
使用构造函数注入,或使用注解的方法 @PostConstruct
来调用 configBean
.
也就是说,您尝试初始化的私有 property
字段未在任何地方使用,因此您也可以删除它,并删除 configBean
字段。