春天,@Configuration和@Bean注释工作 [英] Spring, working with @Configuration and @Bean annotations
问题描述
我有一个code:
@Configuration
公共类BeanSample { @Bean(与destroyMethod =停止)
公共SomeBean someBean()抛出异常{
返回新SomeBean(somebean名称1);
}
类SomeBean { 字符串名称; 公共SomeBean(字符串名称){
this.name =名称;
} 公共无效停止(){
的System.out.println(停止);
}
} 公共静态无效的主要(字串[] args)抛出异常{ BeanSample beanSample =新BeanSample();
SomeBean someBean1 = beanSample.someBean(); ClassPathXmlApplicationContext的appContext =新的ClassPathXmlApplicationContext(
新的String [] {appContext.xml}); SomeBean someBean2 =(SomeBean)appContext.getBean(someBean); 如果(someBean1 == someBean2)的System.out.println(OK); }
}
我期待,一旦我开始应用时,BeanSample.getSomeBean(),然后SomeBean启动时可通过someBean。
卜现在我有一个错误:否豆名为someBean被定义
其实我DOT非了解哪些应用程序上下文中应该使用来接我的豆子呢?
关于@Configuration
任何理由,我为什么要在这里使用@Configuration注解? (这一个,我的IDE凸显我的课,因为它是那么Spring相关的,所以它应该是有意义)
- 确定:当我得到的回答我的code是这样的:
公共静态无效的主要(字串[] args)抛出异常{ AnnotationConfigApplicationContext appContext =新AnnotationConfigApplicationContext(BeanSample.class); SomeBean someBean2 =(SomeBean)appContext.getBean(someBean); 如果(someBean2!= NULL)的System.out.println(OK); }
首先,如果你使用Java的配置,你必须实例化上下文是这样的:
新AnnotationConfigApplicationContext(BeanSample.class)
二, @Configuration
批注不使一个bean出被标注类的。只有 @Bean
方法被用来创建豆类。
如果你想有一个 BeanSample
豆太,你必须创建一个用于创建另一个 @Bean
方法。但话又说回来,为什么你想呢?我认为,一个 @Configuration
类应该只能作为配置容器,而不是为别的。
三, @Bean
默认的bean的名称不遵循属性获取的约定。该方法的名称直接使用,在你的榜样的意思,这个bean将被命名为 getSomeBean
,而不是 someBean
。该方法改成这样:
@Bean(与destroyMethod =停止)
公共SomeBean someBean()抛出异常{
返回新SomeBean(somebean名称1);
}
最后, @Configuration
类不应该被实例化。它的方法只会创建豆的目的。那么Spring将处理它们的生命周期,注入性质等。相反,如果你实例化类并直接调用方法,返回的对象将是没有什么关系的春天只是普通的对象。
I have a code:
@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");
}
}
I'm expecting, once I start app, the BeanSample.getSomeBean() then SomeBean is started to be available by 'someBean'.
Bu now I have an error: No bean named 'someBean' is defined
Actually, I dot not understand which app-context I should use to pick my beans up?
About @Configuration:
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 )
-- OK: after I got an answer my code looks like this:
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");
}
First, if you use the Java config, you have to instantiate your context like this:
new AnnotationConfigApplicationContext(BeanSample.class)
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.
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.
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");
}
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.
这篇关于春天,@Configuration和@Bean注释工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!