Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Autowired null

I have several classes in a Spring Boot project, some work with @Autowired, some do not. Here my code follows:

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 does not work):

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.

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.

like image 619
dexBerlin Avatar asked Oct 20 '14 08:10

dexBerlin


People also ask

Why my Autowired Bean is null?

If the application starts and your field appears to be null it is generally due to one of the following issues: Using @Autowired on a static field. Omitted @Autowired on a field. Instance of bean not visible to Spring.

Why service is null in Spring boot?

Having no clue of the existence of this MyService object, Spring is not able to inject a MyComponent bean inside it. Thus, the MyComponent instance inside the MyService object we created will remain null, causing the NullPointerException we get when we try to call a method on this object.

Why is @autowired not working?

When @Autowired doesn't work. There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.

What does @autowired do in Spring boot?

Starting with Spring 2.5, the framework introduced annotations-driven Dependency Injection. The main annotation of this feature is @Autowired. It allows Spring to resolve and inject collaborating beans into our bean.


2 Answers

I guess you call init() before the autowiring takes place. Annotate init() with @PostConstruct to make it call automatically after all the spring autowiring.

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

like image 83
sinujohn Avatar answered Oct 18 '22 09:10

sinujohn


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,

@Configuration
public class SomeConfiguration{

    @Bean
    public SOAPConfiguration createSOAPConfiguration(){

        return new SOAPConfiguration();
    }

}

public class SomeOtherClass{

    @Autowired
    private SOAPConfiguration soapConfiguration;
}
like image 39
furkan3ayraktar Avatar answered Oct 18 '22 11:10

furkan3ayraktar