I have several classes in the Spring boot project, some work with @Autowired, some do not. Here is my code:
Application.java (@Autowired works):
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):
package com.example.myproject.controller; @Controller class HomeController { @Resource MyRepository myRepository;
MyService (@Autowired not working):
package com.example.myproject.service; @Service public class MyServiceImpl implements MyService { @Autowired public SOAPConfiguration soapConfiguration;
I am not getting SOAPConfiguration, but my application crashes with an exceptional exception when I try to access it.
I already read a lot of topics here and searched Google, but have not yet found a solution. I tried to provide all the necessary information, please let me know if something is missing.
source share