如何解决Spring:为什么注入 bean 的公共字段仅在依赖 bean 中为 null?
@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 举报,一经查实,本站将立刻删除。