我的组件是:
package com.netpc.recruitment.models.user;
@Component
public class UserAuth {
@Autowired
private HttpSession httpSession;
@Autowired
private IUserDAO userDAO;
}
Vars httpSession
和userDAO
在控制器中创建对象时为null
。 userDAO
配置正确,可以在com.netpc.recruitment.controllers.IndexController
班@Controller
中正常使用。
我的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>
它有什么问题吗?为什么它是null
?
答案 0 :(得分:2)
您的组件扫描仅在“com.netpc.recruitment.controllers”上扫描。还应该扫描包com.netpc.recruitment.models.user。 web.xml中的更改:
<context:component-scan base-package="com.netpc.recruitment.controllers" />
到此:
<context:component-scan base-package="com.netpc.recruitment.controllers, com.netpc.recruitment.models.user" />
希望这适合你!