ovnia
ovnia

Reputation: 2500

Spring MVC Autowired null in Component

My component is:

package com.netpc.recruitment.models.user;
@Component
public class UserAuth {
    @Autowired
    private HttpSession httpSession;

    @Autowired
    private IUserDAO userDAO;
}

Vars httpSession and userDAO, while creating object in controllers, are null. userDAO is configured properly and it works fine in my com.netpc.recruitment.controllers.IndexController class @Controller.

My web.xml

<beans ......>
    <context:component-scan base-package="com.netpc.recruitment.controllers" />
    <bean id="userDAO" class="com.netpc.recruitment.models.user.JDBCUserDAO">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

Whats wrong with it? Why it's null?

Upvotes: 2

Views: 1209

Answers (1)

Kornelito Benito
Kornelito Benito

Reputation: 1107

Your component scan is scanning only on "com.netpc.recruitment.controllers". Should also scan in package com.netpc.recruitment.models.user. Changes in web.xml:

  <context:component-scan base-package="com.netpc.recruitment.controllers" />

to this:

  <context:component-scan base-package="com.netpc.recruitment.controllers, com.netpc.recruitment.models.user" />

Hope this works for you!

Upvotes: 2

Related Questions