Java Spring Boot Autowired null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26461697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Autowired null
提问by dexBerlin
I have several classes in a Spring Boot project, some work with @Autowired, some do not. Here my code follows:
我在 Spring Boot 项目中有几个类,有些与 @Autowired 一起工作,有些则没有。这里我的代码如下:
Application.java (@Autowired works):
Application.java(@Autowired 有效):
package com.example.myproject;
@ComponentScan(basePackages = {"com.example.myproject"})
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "com.example.myproject.repository")
@PropertySource({"classpath:db.properties", "classpath:soap.properties"})
public class Application {
@Autowired
private Environment environment;
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public SOAPConfiguration soapConfiguration() {
SOAPConfiguration SOAPConfiguration = new SOAPConfiguration();
SOAPConfiguration.setUsername(environment.getProperty("SOAP.username"));
SOAPConfiguration.setPassword(environment.getProperty("SOAP.password"));
SOAPConfiguration.setUrl(environment.getProperty("SOAP.root"));
return SOAPConfiguration;
}
HomeController (@Autowired works):
HomeController(@Autowired 有效):
package com.example.myproject.controller;
@Controller
class HomeController {
@Resource
MyRepository myRepository;
MyService (@Autowired does not work):
MyService(@Autowired 不起作用):
package com.example.myproject.service;
@Service
public class MyServiceImpl implements MyService {
@Autowired
public SOAPConfiguration soapConfiguration; // is null
private void init() {
log = LogFactory.getLog(MyServiceImpl.class);
log.info("starting init, soapConfiguration: " + soapConfiguration);
url = soapConfiguration.getUrl(); // booom -> NullPointerException
I do not get the SOAPConfiguration but my application breaks with a null pointer exception when I try to access it.
我没有得到 SOAPConfiguration,但是当我尝试访问它时,我的应用程序因空指针异常而中断。
I have already read many Threads here and googled around, but did not find a solution yet. I tried to deliver all necessary information, please let me know if anything misses.
我已经在这里阅读了许多线程并用谷歌搜索,但还没有找到解决方案。我试图提供所有必要的信息,如果有任何遗漏,请告诉我。
采纳答案by sinujohn
I guess you call init()
before the autowiring takes place. Annotate init()
with @PostConstruct to make it call automatically after all the spring autowiring.
我猜你init()
在自动装配之前打电话。init()
使用@PostConstruct 进行注释,使其在所有 spring 自动装配后自动调用。
EDIT: after seeing your comment, I guess you are creating it using new MyServiceImpl()
. This takes away the control of the MyServiceImpl from Spring and gives it to you. Autowiring won't work in those case
编辑:看到您的评论后,我猜您是使用new MyServiceImpl()
. 这从 Spring 中夺走了 MyServiceImpl 的控制权,并将其交给您。在这种情况下自动装配不起作用
回答by furkan3ayraktar
Did you created a bean for the class SOAPConfiguration in any of your configuration classes? If you want to autowire a class in your project, you need to create a bean for it. For example,
您是否在任何配置类中为类 SOAPConfiguration 创建了一个 bean?如果你想在你的项目中自动装配一个类,你需要为它创建一个 bean。例如,
@Configuration
public class SomeConfiguration{
@Bean
public SOAPConfiguration createSOAPConfiguration(){
return new SOAPConfiguration();
}
}
public class SomeOtherClass{
@Autowired
private SOAPConfiguration soapConfiguration;
}