ses*_*ses 8 java spring annotations
我有一个代码:
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
我期待,一旦我启动应用程序,BeanSample.getSomeBean()然后SomeBean开始由'someBean'提供.
现在我有一个错误:没有定义名为'someBean'的bean
实际上,我不明白我应该用哪种app-context来挑选我的豆子?
关于@Configuration:
有什么理由,为什么我应该在这里使用@Configuration注释?(有了这个,我的IDE突出了我的类,因为它与Spring相关,所以它应该有意义)
- 好的:在我得到答案后,我的代码看起来像这样:
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");
}
Run Code Online (Sandbox Code Playgroud)
首先,如果您使用Java配置,则必须像下面这样实例化您的上下文:
new AnnotationConfigApplicationContext(BeanSample.class)
Run Code Online (Sandbox Code Playgroud)
其次,@Configuration注释不会使bean被注释掉.只有@Bean方法用于创建bean.
如果你也想要一个BeanSamplebean,你必须创建另一个@Bean创建一个bean的方法.但话又说回来,你为什么要这样呢?我认为一个@Configuration类只应该用作配置容器而不能用于其他任何东西.
第三,默认的bean名称@Bean不遵循属性getter的约定.方法名称是直接使用的,在您的示例中,bean将被命名getSomeBean而不是someBean.将方法更改为:
@Bean(destroyMethod = "stop")
public SomeBean someBean() throws Exception {
return new SomeBean("somebean name1");
}
Run Code Online (Sandbox Code Playgroud)
最后,@Configuration不应该实例化该类.它的方法仅用于创建bean.然后Spring将处理它们的生命周期,注入属性等等.相反,如果您实例化类并直接调用方法,则返回的对象将只是与Spring无关的普通对象.
生产的豆子
@Bean
public SomeBean getSomeBean()
Run Code Online (Sandbox Code Playgroud)
将具有默认名称 - 这是生产者方法的名称 getSomeBean
所以你可以做两件事
@Bean
public SomeBean getSomeBean() {...}
...
SomeBean bean = (SomeBean) appContext.getBean("getSomeBean");
if (bean != null) System.out.println("OK");
Run Code Online (Sandbox Code Playgroud)
要么
@Bean(name="someBean")
public SomeBean getSomeBean() {...}
...
SomeBean bean = (SomeBean) appContext.getBean("someBean");
if (bean != null) System.out.println("OK");
Run Code Online (Sandbox Code Playgroud)
我用的一些完整的例子代替了AnnotationConfigApplicationContextClassPathXmlApplicationContext
import java.util.Arrays;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanSample {
@Bean(name="someBean")
public SomeBean getSomeBean() throws Exception {
return new SomeBean("somebean name1");
}
class SomeBean {
String name;
public SomeBean(final String name) {
this.name = name;
}
public void stop() {
System.out.println("stop");
}
}
public static void main(final String[] args) throws Exception {
AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(BeanSample.class);
BeanSample beanSample = (BeanSample) appContext.getBean("beanSample");
//next time use this to have a look at the beans in the context!
System.out.println(Arrays.toString(appContext.getBeanDefinitionNames()));
SomeBean bean = (SomeBean) appContext.getBean("someBean");
if (bean != null) System.out.println("OK");
}
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
[org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor ,beanSample,org.springframework.context.annotation.ConfigurationClassPostProcessor $ ImportAwareBeanPostProcessor#0,someBean] OK
| 归档时间: |
|
| 查看次数: |
44652 次 |
| 最近记录: |