我在 osx 上的 Eclipse 中使用 spring 版本 3.0.6,并且不明白为什么我无法为 bean 设置任何属性。部分代码:
@Component("desktopBuilderCallback")
@Scope("prototype")
public class DesktopBuilderCallback implements BuilderCallback {
private Socket desktopSocket;
@Override
public Transformation afterTransform(Transformation transformation)
throws Exception {
\\ some implementation
}
@Override
public void handleStatusUpdate(Transformation transformation)
throws Exception
{
\\ some implementation
}
public Socket getDesktopSocket() {
return desktopSocket;
}
public void setDesktopSocket(Socket desktopSocket) {
this.desktopSocket = desktopSocket;
}
}
@Component("transformationBuilder")
@Scope("prototype")
public class TransformationBuilder implements Runnable,
SourceNotificationCallback, TargetNotificationCallback,
ApplicationContextAware {
int test;
BuilderCallback builderCallback;
public BuilderCallback getBuilderCallback() {
return builderCallback;
}
public void setBuilderCallback(BuilderCallback builderCallback) {
this.builderCallback = builderCallback;
}
public int getTest() {
return this.test;
}
public void setTest(int theTest) {
this.test = theTest;
}
}
public static void main(String[] args) throws IOException {
TransformationBuilder transformationBuilder = (TransformationBuilder) ctx.getBean("transformationBuilder");
DesktopBuilderCallback callback = (DesktopBuilderCallback) ctx.getBean("desktopBuilderCallback");
transformationBuilder.setBuilderCallback(callback);
transformationBuilder.setTest(5);
}
代码执行后,test和builderCallback不会被初始化。 test 将为 0,builderCallback 为 nil。不知道怎么可能,我是java和spring的新手...,但我检查过调试器跳转到setter(setTest,setBuilderCallback),也许是因为项目是为JRE 1.5开发的,但我使用 1.8...
最佳答案
根据您迄今为止发布的代码,我猜测您的问题与 bean 的范围设置为 prototype
有关。
来自spring docs :
The non-singleton, prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made (that is, it is injected into another bean or it is requested via a programmatic getBean() method call on the container). As a rule of thumb, you should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans.
每次您从应用程序上下文中获取 transformationBuilder
bean (ctx.getBean("transformationBuilder")
) 时,您都会获得一个新实例<TransformationBuilder
的/strong>。在这些实例中,不属于标准 bean 初始化过程一部分的每个手动初始化都将不可用。
要正确初始化您的 bean,您可以使用 @Value
注释从配置中注入(inject)值,使用 @Autowired
注入(inject)其他 bean 实例,或使用 @PostConstruct
方法来执行额外的、重要的初始化。
对于您的类(class),这可能如下所示:
@Component("transformationBuilder")
@Scope("prototype")
public class TransformationBuilder implements Runnable,
SourceNotificationCallback, TargetNotificationCallback,
ApplicationContextAware {
// either use the value of the property 'some.property.key' or 5 as default
@Value("${some.property.key:5}")
int test;
@Autowired
@Qualifier("desktopBuilderCallback")
BuilderCallback builderCallback;
...
}
当然,您也可以使用singleton
范围(实际上是spring bean的默认范围)。这将导致每次请求此 bean 时 spring 返回相同的实例。但对于 singleton
范围,bean 的初始化也应该由 DI 容器处理。
关于java - 设置后的属性始终为nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48891061/