Spring:为什么注入 bean 的公共字段仅在依赖 bean 中为 null?

如何解决Spring:为什么注入 bean 的公共字段仅在依赖 bean 中为 null?

我的服务 bean 在构造函数中设置了两个公共字段

@Service
public class MyService {
    private static final Logger log = LoggerFactory.getLogger(MyService.class);
    
    public Boolean nativeHome;
    public String url;
    
    @Autowired
    public MyService(
            @Value("${homepage.native}") Boolean nativeHome,@Value("${homepage.url}") String url) {
        this.nativeHome = nativeHome;
        this.url = url;
        log.debug(this.nativeHome + ""); // not null
        log.debug(this.url); // not null
    }
}

这个 bean 通过它的构造函数被注入到控制器组件中:

@RestController
public class MyController {
    private static final Logger log = LoggerFactory.getLogger(MyController.class);
    
    private MyService myService;

    @Autowired
    public MyController(MyService myService) {
        this.myService = myService;
        log.debug(this.myService.nativeHome + ""); // null
        log.debug(this.myService.url); // null 
    }
}

Spring bootstrap 日志以及记录器调试消息显示这两个字段在服务 bean 中设置正确,但它们在控制器 bean 中是 null

15:47:44.397 [main] DEBUG c.r.spring.service.MyService.<init>(40) - false
15:47:44.397 [main] DEBUG c.r.spring.service.MyService.<init>(41) - http://...
[...]
15:47:46.160 [main] DEBUG c.r.s.controller.MyController.<init>(34) - null
15:47:46.160 [main] DEBUG c.r.s.controller.MyController.<init>(35) - null

如果我将这两个字段声明为 private 并在控制器中使用 getter,那么这两个字段不再是 null。为什么会这样?

为了完整性,我将提供完整的项目设置,即使它可能与问题无关。


Spring 版本:5.2.8.RELEASESpring 的根 applicationContext.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config />
    
    <aop:aspectj-autoproxy />
    
    <context:component-scan base-package="c.r.spring">
        <context:exclude-filter type="regex" expression="c\.r\.spring\.controller.*"/>
    </context:component-scan>
    
    <import resource="classpath:spring/spring-jdbc.xml" />
    
</beans>

Web 应用程序上下文在 dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">

    <mvc:annotation-driven>
        <mvc:path-matching trailing-slash="true" />
    </mvc:annotation-driven>

    <mvc:default-servlet-handler />

    <mvc:cors>
        <mvc:mapping path="/**"
            allowed-origins="http://devel.r.com:3000"
            allowed-methods="GET,POST,PUT,DELETE,OPTIONS"
            allowed-headers="Authorization,Content-Type"
            exposed-headers="Access-Control-Allow-Origin"
            allow-credentials="true" />
    </mvc:cors>

    <context:component-scan base-package="c.r.spring.controller" />
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/spring/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

以及 web.xml 的相关部分:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?