【问题标题】:Spring @Autowired variable is null inside of @Component [duplicate]Spring @Autowired 变量在 @Component 内部为空 [重复]
【发布时间】:2015-09-14 12:03:00
【问题描述】:

这是我的组件,其中包含 userService 自动连线。问题是未初始化,始终为空。用户服务在控制器中运行良好。

如何在组件内部自动装配服务?

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>

【问题讨论】:

  • UserService上有Service注解吗?可以加个限定符注解吗?
  • @duffymo,是的,UserService 可以在其他 @Controller 类中正常工作。
  • 无法从这段代码中看出你做错了什么。
  • 你如何使用ModelUtil这个类?如果您使用new ModelUtil() 自己实例化它,那么它不是 Spring 托管 bean,并且不会完成自动装配。见:Spring Dependency Injection Autowiring Null
  • 显示你调用warp的班级。

标签: java spring autowired


【解决方案1】:

您应该只对组件扫描使用一种定义。最后一个覆盖了之前的定义。

<context:component-scan base-package="com.boro.orange"/>

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 2016-11-27
    • 1970-01-01
    • 2016-11-14
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多