9

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

    }
ses
  • 13,174
  • 31
  • 123
  • 226

3 Answers3

7

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.

rolve
  • 10,083
  • 4
  • 55
  • 75
  • Ok, I changed my question then. – ses Oct 11 '12 at 16:29
  • ok. it works. I Also moved BeanSample - not to be inner one. Also, have provided default constructor for BeanSample. Trying to understand why I need: @Configuration then.. – ses Oct 11 '12 at 16:42
  • Updated my answer again. Basically, Spring features like dependency injection only work if you use the class as `@Configuration` and not instantiate it directly. – rolve Oct 11 '12 at 16:53
  • Yes..ok. then BeanSample is not bean.. but it is triggered by spring if it is with Configuration.. I see. Also it requires: cglib to be added to my pom.xml (otherwise @Configuration is not working). [I'm going to put my code like it is into the post]. Thanks. – ses Oct 11 '12 at 17:44
6

The bean produced by

@Bean
public SomeBean getSomeBean() 

will have the default name -- and that is the name of the producer method getSomeBean

So you can do two things

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

or

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

Some complete example I used AnnotationConfigApplicationContext instead of ClassPathXmlApplicationContext

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

    }
}

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

Ralph
  • 118,862
  • 56
  • 287
  • 383
4

Your test should look like this with @Configuration bean configuration(what you would have previously defined using a context xml file is now defined using your @Configuration java code)

This will create an application context with BeanSample providing the bean configuration:

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanSample.class);

Now, in your @Configuration:

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

the bean name is the method name..so above the method name is "someBean", in your case you had the bean name as "getSomeBean"

So to look up the bean you have to do:

SomeBean bean = appContext.getBean("someBean", SomeBean.class);
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125