春豆变得困惑 [英] Spring bean getting confused

查看:143
本文介绍了春豆变得困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹簧网络应用程序A和一个库B. B有一个配置文件。在App中使用B库时,我正在创建新的bean而不是自动装配。但仍然看到在库中初始化的bean正在编写我在App中执行的初始化。

I have a spring web app A and a library B. B has its one config file. When using the B library in App i am creating new beans instead of autowiring. but still seeing that the beans initialized in library are over writing the initialization i did in App.

这是一个虚拟配置文件,可以清楚地解释:

Here is a dummy config file to explain clearly:

AConfig.java

public class AConfig{

 @Bean 
public Info info(){
Info i =  new Info("A");
return i;
}

@Bean
public A a(){
A a = new A();
a.setInfo(info());
a.setB(b());
return A;
}

@Bean
public B b(){
B b = new B();
b.setInfo(info());
return b;
}

}

BConfig.java

public class BConfig(){

@Bean
public Info info(){
Info i = new Info("B);
}

@Bean

public B b(){

B b = new B();
b.setInfo(info());
return b;
}
}

Aconfig.java和BConfig.java都有info()方法来生成信息bean。我在A项目中没有BConfig.java文件我没有做任何自动装配。我期待在projectA中我必须看到信息bean有字符串A但我看到字符串B.有些如何创建Bean A时Bconfig.java的方法info()被调用而不是来自Aconfig.java的info方法。

Aconfig.java and BConfig.java both have info() methods to generate info bean. I am not having BConfig.java file in A project. i am not doing any autowiring. I am expecting that in projectA i must see the info bean to have String A but i am seeing string B. Some how the when creating Bean A the method info() of Bconfig.java is getting called instead of the info method from Aconfig.java.

有人可以解释为什么会发生这种情况。

Can someone explain why this is happening.

编辑

在我的春季日志文件中,我看到了这一行:

In my spring log file i saw this line:

Overriding bean definition for bean 'Info': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=AConfig; factoryMethodName=info; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/configs/AConfig.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=BConfig; factoryMethodName=info; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/configs/BConfig.class]]

感觉就像我初始化的bean在AConfig被BCOnfig.java覆盖但不确定为什么会被春天覆盖。

feels like the bean i initialized in AConfig is being overwritten by BCOnfig.java but not sure why is spring overwriting.

推荐答案

行为是预期的,这是因为你有两个完全相同名称的bean info ,后面定义的那个(在A中)是在运行时创建的。行为与使用xml定义的bean发生的行为相同。

The behavior is expected, this is because you have two beans with the exact same name info and the one defined later(in A) is the one which gets created at runtime. The behavior is same as what would have happened with the beans defined using xml.

如何通过理解Spring @Configuration 的内部结构来解释这种情况,这里用Spring文档解释 - http://static.springsource.org/spring-framework/docs/3.2.3.RELEASE/spring-framework-reference/html/beans.html#beans-java-further-information- Java的配置。简而言之,即使您直接在 AConfig 中调用 info()方法,它实际上并没有调用该方法。真实的 AConfig 实例,而不是在代理上调用它,它具有确保返回正确的bean实例的逻辑。

How this happens is best explained by understanding the internals of Spring @Configuration which is explained with the Spring documentation here - http://static.springsource.org/spring-framework/docs/3.2.3.RELEASE/spring-framework-reference/html/beans.html#beans-java-further-information-java-config. In short, even if you call info() method directly in AConfig, it is not actually invoking the method on the real AConfig instance, instead it is invoked on a proxy, which has the logic to make sure that the correct bean instance is returned.

这篇关于春豆变得困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆