Spring@Component 中的@Autowired 变量为空
Spring @Autowired variable is null inside of @Component
这是我的组件,其中包含 userService
自动连接。问题是没有初始化,一直是null。控制器的用户服务工作正常。
组件内部的自动装配服务应该怎么做?
package com.boro.orange.component;
@Component("modelUtil")
public class ModelUtil {
@Autowired
private UserService userService; //null
static Logger log = Logger.getLogger(ModelUtil.class.getName());
public ModelMap warp(ModelMap model) {
Object springUserObject = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (springUserObject == null || !(springUserObject instanceof User)) {
return model;
}
User springUser = (User) springUserObject;
String userEmailAddress = springUser.getUsername();
com.boro.orange.entity.User signedInUser = userService.getUserByEmailAddress(userEmailAddress);
if (signedInUser == null) {
String errorMsg = "Failed to find user by email address[" + userEmailAddress + "]";
log.error(errorMsg);
model.addAttribute("Error", errorMsg);
// TODO add error messages
} else {
String userFirstName = signedInUser.getFirstName();
String userLastName = signedInUser.getLastName();
model.addAttribute("userFirstName", userFirstName);
model.addAttribute("userLastName", userLastName);
}
return model;
}
}
root-context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.boro.orange.dao"/>
<context:component-scan base-package="com.boro.orange.service"/>
<context:component-scan base-package="com.boro.orange.component"/>
<context:component-scan base-package="com.boro.orange.controller"/>
<import resource="data.xml"/>
</beans>
您应该只使用一个组件扫描定义。最后一个覆盖了之前的定义。
<context:component-scan base-package="com.boro.orange"/>
这是我的组件,其中包含 userService
自动连接。问题是没有初始化,一直是null。控制器的用户服务工作正常。
组件内部的自动装配服务应该怎么做?
package com.boro.orange.component;
@Component("modelUtil")
public class ModelUtil {
@Autowired
private UserService userService; //null
static Logger log = Logger.getLogger(ModelUtil.class.getName());
public ModelMap warp(ModelMap model) {
Object springUserObject = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (springUserObject == null || !(springUserObject instanceof User)) {
return model;
}
User springUser = (User) springUserObject;
String userEmailAddress = springUser.getUsername();
com.boro.orange.entity.User signedInUser = userService.getUserByEmailAddress(userEmailAddress);
if (signedInUser == null) {
String errorMsg = "Failed to find user by email address[" + userEmailAddress + "]";
log.error(errorMsg);
model.addAttribute("Error", errorMsg);
// TODO add error messages
} else {
String userFirstName = signedInUser.getFirstName();
String userLastName = signedInUser.getLastName();
model.addAttribute("userFirstName", userFirstName);
model.addAttribute("userLastName", userLastName);
}
return model;
}
}
root-context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.boro.orange.dao"/>
<context:component-scan base-package="com.boro.orange.service"/>
<context:component-scan base-package="com.boro.orange.component"/>
<context:component-scan base-package="com.boro.orange.controller"/>
<import resource="data.xml"/>
</beans>
您应该只使用一个组件扫描定义。最后一个覆盖了之前的定义。
<context:component-scan base-package="com.boro.orange"/>