rainerhahnekamp
rainerhahnekamp

Reputation: 1136

Autowired not working as expected

I'm using Spring Roo and want to access a bean within of Controller class which has following configuration in applicationContext.xml:

<bean class="com.reservation.jobs.Configuration" id="jobsConfiguration" autowire="byType">
 <property name="skipWeeks" value="4" />
</bean>

The configuration class itself is:

package com.reservation.jobs;

public class Configuration {
 private int skipWeeks;

 public void setSkipWeeks(int value) {
  System.out.println("SkipWeeks set auf: " + value);
  this.skipWeeks = value;
 }
 public int getSkipWeeks() {
  return this.skipWeeks;
 }
}

In my Controller I thought that a simple Autowired annotation should do the job

public class SomeController extends Controller {
 @Autowired
 private com.reservation.jobs.Configuration config;

}

During startup Spring prints the message within the setSkipWeeks method. Unfortunately whenever I call config.getSkipWeeks() within the controller it returns 0.

Have I to use the getBean method of the ApplicationContext instance or is there some better way?

Upvotes: 0

Views: 246

Answers (1)

Bozho
Bozho

Reputation: 597096

autowire="byType" is redundant. It indicates that fields of the Configuration class should be autowired, and you have just one primitive. So remove that attribute.

Apart from that, config.getSkipWeeks() must return 4 unless:

  • you are using a different instance (made by you with new)
  • you have called the setter somewhere with a value of 0

Upvotes: 2

Related Questions