Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring,使用@Configuration和@Bean注释_Java_Spring_Annotations - Fatal编程技术网

Java Spring,使用@Configuration和@Bean注释

Java Spring,使用@Configuration和@Bean注释,java,spring,annotations,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 So

我有一个密码:

@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'的bean

事实上,我不明白我应该使用哪个应用程序上下文来获取我的bean

关于@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");

    }

首先,如果您使用Java配置,您必须像下面这样实例化您的上下文:

new AnnotationConfigApplicationContext(BeanSample.class)
其次,
@Configuration
注释不会从被注释的类中生成bean。只有
@Bean
方法用于创建Bean

如果您也想要一个
BeanSample
bean,那么必须创建另一个
@bean
方法来创建一个。但话说回来,你为什么要这样?我认为a
@Configuration
类应该只用作配置容器,而不用于其他任何东西

第三,
@bean
的默认bean名称不遵循属性getter的约定。方法名称直接使用,这意味着在您的示例中,bean将被命名为
getSomeBean
,而不是
someBean
。将方法更改为:

@Bean(destroyMethod = "stop")
public SomeBean someBean() throws Exception {
    return new SomeBean("somebean name1");
}

最后,不应实例化
@Configuration
类。其方法仅用于创建bean。然后Spring将处理它们的生命周期、注入属性等等。相反,如果实例化类并直接调用方法,则返回的对象将只是与Spring无关的普通对象。

您的测试应该与
@Configuration
bean配置类似(以前使用上下文xml文件定义的内容现在使用@Configuration java代码定义)

这将创建一个应用程序上下文,其中BeanSample提供bean配置:

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanSample.class);
现在,在您的
@配置中

@Bean
public SomeBean someBean() throws Exception {
    return new SomeBean("somebean name1");
}
bean名称是方法名称..因此方法名称上方是“someBean”,在您的例子中,bean名称是“getSomeBean”

因此,要查找bean,您必须执行以下操作:

SomeBean bean = appContext.getBean("someBean", SomeBean.class);
豆子

@Bean
public SomeBean getSomeBean() 
将具有默认名称——这是生产者方法的名称
getSomeBean

所以你可以做两件事

@Bean
public SomeBean getSomeBean() {...}   
...
SomeBean bean = (SomeBean) appContext.getBean("getSomeBean");
if (bean != null) System.out.println("OK");


我使用了
AnnotationConfigApplicationContext
而不是
ClassPathXmlApplicationContext

输出:

 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");

    }
[org.springframework.context.annotation.internalConfigurationAnnotationProcessor, org.springframework.context.annotation.InternalAutowiredNotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor, org.springframework.context.annotation.internalPersistenceAnnotationProcessor, 例如, org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0, 好的


好的,我改变了我的问题。好的。它起作用了。我还移动了BeanSample-而不是内部的。另外,我已经为BeanSample提供了默认构造函数。试图理解为什么我需要:@Configuration then..再次更新了我的答案。基本上,Spring的依赖项注入等功能只有在将类用作
@Configuration
而不是直接实例化它。是的..好的。那么BeanSample不是bean..但是如果是配置的话它是由spring触发的..我明白了。它还需要:cglib被添加到我的pom.xml中(否则@Configuration不起作用)。[我将把我的代码像放在文章中一样放进去]。谢谢。为什么我要在这里使用@Configuration?(我在帖子中添加了这个问题)以便将这个类标记为配置spring上下文的类
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");

    }
}