Lak*_*ner 5 spring dependency-injection
最奇怪的 Spring 问题是,使用依赖注入创建 Spring bean,然后在 bean 上运行某些方法后,在 bean 方法调用期间设置的实例变量都会返回到它们的默认 Java 值。我们从 Spring 2.5.5 迁移到 Spring 3.0.5 后发现这种情况发生。
因此,为了清楚起见,这里是示例
原型 bean:
@Component("bean1")
@Scope("prototype")
public class Bean1{
String someString;//There are other instance variables but same happens to them
@Autowired
@Qualifier("otherBean")
private OtherBean otherBean;
public void doSomething(){
someString="1234ABC";
}
//setters and getters ....
}
Run Code Online (Sandbox Code Playgroud)
从 spring 获取 bean 并使用它的代码:
Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
bean1.doSomething();//updates some instance variables in bean1
String value = bean1.getSomeString(); //Instance variables is null
Object otherObject = bean1.getOtherBean(); //This Spring injected bean is correctly initialized
Run Code Online (Sandbox Code Playgroud)
因此,如果我调试代码,则在 doSomething 方法调用中将实例变量 (someString) 设置在 bean 中,但在我返回后,该值将返回 null。
最糟糕的是,这一切在 2.5.5 中都按预期工作,但在更新的 Spring 3.0.5 中却不然
这是遗留代码,所以我知道您应该编写接口代码,因此 Bean1 应该是一个接口,而实现该接口的类应该完成实际工作。我也将代码更改为该模型,但仍然不起作用。
归档时间: |
|
查看次数: |
1420 次 |
最近记录: |