Spring MVC Autowired null in Component

ovnia

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?

Kornelito Benito

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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring @Autowired variable is null inside of @Component

From Dev

spring mvc autowired sessionFactory gives null object

From Dev

autowired component is null

From Dev

Spring Autowired null if not found

From Dev

Spring Boot Autowired null

From Dev

Spring @Autowired comes as null

From Dev

Spring - autowired fields are null

From Dev

Spring @Autowired field is null?

From Dev

Spring - autowired fields are null

From Dev

Spring autowired is null

From Dev

Spring use @Autowired field not in @Component

From Dev

Autowired bean is null in MVC Controller

From Java

Spring JUnit: How to Mock autowired component in autowired component

From Dev

Spring MVC add bean and autowired

From Dev

Autowired is null and not working with Jersey + Spring

From Dev

Spring @Autowired(required = true) is null

From Dev

Jersey 2 + Spring: @Autowired is null

From Dev

Spring Boot Autowired Repository null

From Dev

Spring @Autowired not working and returning null

From Dev

Spring @Autowired(required = true) is null

From Dev

spring @Autowired a repository returns null

From Dev

Spring @Autowired bean giving null

From Dev

Spring MVC - Autowired Repository NullPointerException in static context

From Dev

Testing Spring Boot MVC with autowired repositories

From Dev

Hibernate Spring MVC injected autowired issue

From Dev

Spring Mvc test webapp @Autowired annotation is not working

From Dev

FindBugs null issue with Spring @Autowired in Eclipse

From Java

Why is my Spring @Autowired field null?

From Dev

Spring DI - Autowired property is null in a REST service