Spring,使用 @Configuration 和 @Bean 注解
我有一个代码:
@Configuration
public class BeanSample {
@Bean(destroyMethod = "stop")
public SomeBean someBean() throws Exception {
return new SomeBean("somebean name1");
}
class SomeBean {
String name;
public SomeBean(String name) {
this.name = name;
}
public void stop() {
System.out.println("stop");
}
}
public static void main(String[] args) throws Exception {
BeanSample beanSample = new BeanSample();
SomeBean someBean1 = beanSample.someBean();
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] {"appContext.xml"});
SomeBean someBean2 = (SomeBean) appContext.getBean("someBean");
if (someBean1 == someBean2) System.out.println("OK");
}
}
我期待,一旦我启动应用程序,BeanSample.getSomeBean() 然后 SomeBean 就可以被someBean"使用了.
I'm expecting, once I start app, the BeanSample.getSomeBean() then SomeBean is started to be available by 'someBean'.
现在我有一个错误:没有定义名为someBean"的bean
实际上,我不明白应该使用哪个应用上下文来获取我的 bean?
Actually, I dot not understand which app-context I should use to pick my beans up?
关于@Configuration:
任何原因,为什么我应该在这里使用@Configuration 注释?(有了这个,我的 IDE 突出显示了我的类,因为它当时与 Spring 相关,所以它应该是有意义的)
Any reasons, why I should use @Configuration annotation here? (with this one, my IDE highlights my classes as it were Spring-related then, so it should make sense )
-- 好的:得到答案后,我的代码如下所示:
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(BeanSample.class);
SomeBean someBean2 = (SomeBean) appContext.getBean("someBean");
if (someBean2 != null) System.out.println("OK");
}
推荐答案
首先,如果你使用 Java 配置,你必须像这样实例化你的上下文:
First, if you use the Java config, you have to instantiate your context like this:
new AnnotationConfigApplicationContext(BeanSample.class)
其次,@Configuration
注释不会从被注释的类中生成 bean.只有 @Bean
方法用于创建 bean.
Second, the @Configuration
annotation doesn't make a bean out of the class that is annotated. Only the @Bean
methods are used to create beans.
如果你也想拥有一个 BeanSample
bean,你必须创建另一个 @Bean
方法来创建一个.但话又说回来,你为什么想要那个?我认为 @Configuration
类只能用作配置容器,不能用于其他任何东西.
If you want to have a BeanSample
bean too, you have to create another @Bean
method that creates one. But then again, why would you want that? I think the a @Configuration
class should only be used as the configuration container and not for anything else.
第三,@Bean
的默认 bean 名称不遵循属性 getter 的约定.直接使用方法名称,这意味着在您的示例中,bean 将被命名为 getSomeBean
而不是 someBean
.把方法改成这样:
Third, the default bean names for @Bean
do not follow the conventions of property getters. The method names are used directly, meaning in your example, the bean would be named getSomeBean
and not someBean
. Change the method to this:
@Bean(destroyMethod = "stop")
public SomeBean someBean() throws Exception {
return new SomeBean("somebean name1");
}
最后,@Configuration
类不应被实例化.它的方法仅用于创建 bean 的目的.然后 Spring 将处理它们的生命周期、注入属性等.相反,如果你实例化类并直接调用方法,返回的对象将只是普通的对象,与 Spring 没有任何关系.
Finally, the @Configuration
class should not be instantiated. Its methods only serve the purpose of creating the beans. Spring will then handle their lifecycle, inject properties and so on. In contrast, if you instantiate the class and call the methods directly, the returned objects will be just normal objects that don't have anything to do with Spring.
相关文章