Eclipse 中 Spring @Autowired 的 FindBugs 空问题
FindBugs null issue with Spring @Autowired in Eclipse
我正在一起使用 findbugs Eclipse plugin (3.0.1.20150306-5afe4d1), spring (4.2.2.RELEASE), and eclipse (Mars.1 (4.5.1)),我在 Eclipse 中收到以下 FindBugs 错误。
Non-null field env
is not initialized by new
org.test.app.config.AppConfiguration()
[Scary(8), Normal confidence]
我正在使用默认构造函数并使用自动装配来初始化 env
变量。我还有一个 PostConstruct
注释,它在连接所有内容后被调用并访问 env
变量以确保它被正确初始化。
如何在不关闭 FindBugs 插件并仍然使用 @Autowired
注释的情况下使此错误消失?
package org.test.app.config;
import java.util.Arrays;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@ComponentScan(basePackages = { "org.test.app" })
@PropertySource("classpath:/${spring.profiles.active:local}.properties")
public class AppConfiguration {
private static final Logger log = LoggerFactory.getLogger(AppConfiguration.class);
@Autowired
private Environment env;
/**
* Dump profile info.
*/
@PostConstruct
public void details() {
log.debug("** App application context, active profile(s)={}", Arrays.toString(env.getActiveProfiles()));
}
}
更新
我尝试按照@spoonybard896 的建议使用构造函数,但没有成功。我收到以下错误。
java.lang.NoSuchMethodException: org.test.app.config. AppConfiguration $$EnhancerBySpringCGLIB$$cbece1d7.<init>()
[STDOUT] at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_60]
[STDOUT] at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_60]
[STDOUT] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) ~[na:na]
改用非默认构造函数怎么样?
private final Environment env;
@Autowired
public AppConfiguration(final Environment env) {
this.env = env;
}
编辑
上述方法适用于 @Controller
个实例,但不适用于 @Configuration
。在做了一些快速 research 之后,结果是:
@Configuration
is meta-annotated with @Component
, therefore @Configuration
classes are candidates for component scanning (typically using Spring XML's <context:component-scan/>
element) and therefore may also take advantage of @Autowired
/@Inject
at the field and method level (but not at the constructor level).
我在想,除非有某种 FindBugs 的插件可以理解 Spring 注释(我不知道有一个),否则你可能只需要对 FindBugs 插件应用一个过滤器并拥有它会忽略该特定文件中的特定错误(或通常在任何配置 class 中)。在 Eclipse 中查看 Preferences -> Java -> Findbugs -> Filter Files,然后查看 this link which describes a similar problem and resolution, but just make sure to filter out only the error you want。目标不是关闭 FindBugs,而是让它忽略这种情况。
编辑 2
将 annotation 添加到 class 将仅抑制此文件的 FindBugs 错误。
@SuppressFBWarnings(
value="NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR",
justification="Overriding the check on the env variable because Spring will automatically initialize the variable after the constructor is called and before any public methods are called.")
我正在一起使用 findbugs Eclipse plugin (3.0.1.20150306-5afe4d1), spring (4.2.2.RELEASE), and eclipse (Mars.1 (4.5.1)),我在 Eclipse 中收到以下 FindBugs 错误。
Non-null field
env
is not initialized bynew org.test.app.config.AppConfiguration()
[Scary(8), Normal confidence]
我正在使用默认构造函数并使用自动装配来初始化 env
变量。我还有一个 PostConstruct
注释,它在连接所有内容后被调用并访问 env
变量以确保它被正确初始化。
如何在不关闭 FindBugs 插件并仍然使用 @Autowired
注释的情况下使此错误消失?
package org.test.app.config;
import java.util.Arrays;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@ComponentScan(basePackages = { "org.test.app" })
@PropertySource("classpath:/${spring.profiles.active:local}.properties")
public class AppConfiguration {
private static final Logger log = LoggerFactory.getLogger(AppConfiguration.class);
@Autowired
private Environment env;
/**
* Dump profile info.
*/
@PostConstruct
public void details() {
log.debug("** App application context, active profile(s)={}", Arrays.toString(env.getActiveProfiles()));
}
}
更新
我尝试按照@spoonybard896 的建议使用构造函数,但没有成功。我收到以下错误。
java.lang.NoSuchMethodException: org.test.app.config. AppConfiguration $$EnhancerBySpringCGLIB$$cbece1d7.<init>()
[STDOUT] at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_60]
[STDOUT] at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_60]
[STDOUT] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) ~[na:na]
改用非默认构造函数怎么样?
private final Environment env;
@Autowired
public AppConfiguration(final Environment env) {
this.env = env;
}
编辑
上述方法适用于 @Controller
个实例,但不适用于 @Configuration
。在做了一些快速 research 之后,结果是:
@Configuration
is meta-annotated with@Component
, therefore@Configuration
classes are candidates for component scanning (typically using Spring XML's<context:component-scan/>
element) and therefore may also take advantage of@Autowired
/@Inject
at the field and method level (but not at the constructor level).
我在想,除非有某种 FindBugs 的插件可以理解 Spring 注释(我不知道有一个),否则你可能只需要对 FindBugs 插件应用一个过滤器并拥有它会忽略该特定文件中的特定错误(或通常在任何配置 class 中)。在 Eclipse 中查看 Preferences -> Java -> Findbugs -> Filter Files,然后查看 this link which describes a similar problem and resolution, but just make sure to filter out only the error you want。目标不是关闭 FindBugs,而是让它忽略这种情况。
编辑 2
将 annotation 添加到 class 将仅抑制此文件的 FindBugs 错误。
@SuppressFBWarnings(
value="NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR",
justification="Overriding the check on the env variable because Spring will automatically initialize the variable after the constructor is called and before any public methods are called.")