1

I have been working on my first Spring project, and I've come across an annoying problem.

I have a class called 'UsernameService' which is configured as a bean in the dispatcher-servlet.xml:

<bean id="usernameService" class="service.UsernameService" scope="session" >
    <aop:scoped-proxy />
</bean>

and when this bean is created in one of my classes (bean definition:)

<bean id="testController" class="controller.TestController" />

as such:

 @Autowired
UsernameService uns;

it works absolutely fine. However, when I try and do the same in another class, LogController:

<bean id="logController" class="controller.LogController" />

then it does not work, and I get the following error:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/flexitime] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException

I've managed to (I believe) ascribe this error to the fact that uns is never actually set/created and remains as null inside LogController.

I have Googled this extensively and have found many 'solutions', however as yet none of them have worked.

Thanks! James

ymhr
  • 11
  • 2
  • How is the LogController accessed. Do you have a method with @RequestMapping which is getting invoked in response to a webrequest or are you doing new LogController somewhere in your code? – gkamal Apr 11 '12 at 17:54
  • Object may not be created by `new` for injection to work. – Thorbjørn Ravn Andersen Apr 11 '12 at 17:54
  • @gkamal LogController is accessed from another controller, and it itself is an autowired bean: `@Autowired (new line) LogController ls`. @thorb I am not using the new keyword to initialise the class. – ymhr Apr 11 '12 at 18:02
  • Can you double check the code to make sure you are indeed using the above ls variable (it hasn't been masked by some localVariable). @Autowired field will not be null if the bean was created by spring. It is very likely that the logController object that you are accessing was not created by spring. You can also add a log statement in the constructor (create a dummy one if you don't have one) and verify that it is getting created only once during spring context initialization. – gkamal Apr 11 '12 at 18:13
  • No I'm not using any other declaration of the class. I made a constructor as you suggested and as far as I can tell it is only being created once (each time the page is accessed, but afaik that is correct). – ymhr Apr 11 '12 at 19:08

1 Answers1

0

Add the auto-wire attribute to your bean:

<bean id="usernameService" class="service.UsernameService" scope="session" autowire="byName">
    <aop:scoped-proxy />
</bean>
kiritsuku
  • 52,967
  • 18
  • 114
  • 136
Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
  • That gives me the error: `Attribute 'default-autowire' is not allowed to appear in element 'bean'` – ymhr Apr 11 '12 at 19:03
  • Updated it, sorry I should have noticed that my self :( Unfortunately that still doesn't work. – ymhr Apr 11 '12 at 19:37