Spring config didn't work as expected

I'm building a new project, and I thought I'd try a new way of loading my Spring config. I found the @Configuration annotation and decided to give it a try.

@Configuration
@ImportResource("classpath:myApp-config.xml")
public class MyAppConfig
{
    @Autowired
    private MyClass myClass;

    @Bean(name="someOtherBeanName")
    public MyClass getMyClass ()
    {
        return myClass;
    }

    public void setMyClass( myClass m)
    {
        this.myClass= m;
    }
}

In the spring config file:

<context:annotation-config/>
<bean name="someOtherBeanName" class="com.MyClass">
    <property name="myClass">
        <map>
            <!-- details not relevant -->
        </map>
    </property>
</bean>

To make use of this, I have code like this:

//class member
private static MyAppConfig cfg = new MyAppConfig();
...
...
...
//In the class that needs the configuration
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MyAppConfig.class);
ctx.refresh();
//appMgr = cfg.getMyClass();
appMgr = (MyClass) ctx.getBean("someOtherBeanName");

As you can see, I'd thought I could get a spring-configured instances of MyClass from my configuration object, but instead I had to get it from my context object.

I guess I misunderstood the way @Configuration and @Bean work. Am I pretty close or way off?

Upvotes: 0

Views: 154

Answers (4)

tyrantqiao
tyrantqiao

Reputation: 339

Add something that you might meet, if u still can not find config.xml or javaconfig.class. Check your file structure.

  • src
    • config.xml or javaconfig.java

To save your config in your path(default package is src)

Upvotes: 0

Pavel Horal
Pavel Horal

Reputation: 18224

You are pretty way off... going full Java-based config will be for your example:

@Configuration
public class MyAppConfig {

    @Bean
    public MyClass someOtherBeanName() {
        MyClass myClass = new MyClass();
        myClass.setMyProp(null /* details not relevant */);
        return myClass;
    }

}

Somewhere else in the main method (this is unchanged):

//In the class that needs the configuration
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MyAppConfig.class);
ctx.refresh();
//appMgr = cfg.getMyClass();
appMgr = (MyClass) ctx.getBean("someOtherBeanName");

Upvotes: 0

Jaiwo99
Jaiwo99

Reputation: 10017

You cannot get your bean from cfg.getMyClass();, there are some misunderstanding.

@Configuration is only another representation of spring configuration, you should understand it just like your application-context.xml, nothing is new here.

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280168

This

private static MyAppConfig cfg = new MyAppConfig();

is not a Spring managed bean so you will get null when calling getMyClass().

Also, the following

@ImportResource("classpath:myApp-config.xml")

with

@Autowired
private MyClass myClass;

@Bean(name="someOtherBeanName")
public MyClass getMyClass ()
{
    return myClass;
}

is redundant. Because of the @ImportResource, the bean from the XML configuration is already in the context.

Indicates one or more resources containing bean definitions to import.

You don't need an additional @Bean method in between.

Upvotes: 0

Related Questions