-1

I've created a class and I've annotated with @Component, and this class is under the right package where it will do component scan. But still, if I try to Autowire, it's not able to inject. Here is my configuration.

@Component
@ConfigurationProperties(prefix = "request-default-values")
public class DefaultConfig {

 //Getters and setters

} 

My class where I inject

@Component
public class ProcessRequest {

    @Autowired
    DefaultConfig defaultConfig;

    @Autowired
    SalesForceService salesForceService;

    public String getUpdateRequest(String req) {

        // defaultConfig is coming as null

    }
}

The above class is under root package com.bb.app

My spring main application

@SpringBootApplication(scanBasePackages = {"com.bb.app"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
   }
}

Is there anything still I'm missing?

Syed
  • 2,471
  • 10
  • 49
  • 89
  • 1
    For starters - your `ProcessRequest` class isn't wired as a `@Component` or `@Configuration`, so Spring wouldn't know to look at it. – Makoto Dec 10 '19 at 17:35
  • 1
    Please post the exact and complete stack trace of the exception you're getting. Show the relevant code. – JB Nizet Dec 10 '19 at 17:40
  • @Makoto, I tried with that even, it throws the same exception. Null pointer – Syed Dec 10 '19 at 17:40
  • 1
    See [Why is my Spring `@Autowired` field `null`?](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) (Are you creating an instance of `ProcessRequest` somewhere using `new`?). – Jesper Dec 10 '19 at 17:41
  • 1
    @Jesper, Yes, I'm creating an instance. You are right. Sorry, I didn't observe that earlier. – Syed Dec 10 '19 at 17:46
  • 1
    Show the code where you are using class `ProcessRequest` that leads to the `NullPointerException`. – Jesper Dec 10 '19 at 17:47

2 Answers2

2

@Component public class ProcessRequest {}

How does spring know how to create ProcessRequest with autowired beans? You ask it to by specifying its also a spring managed bean. You forgot to annotate the class.

Karthik R
  • 5,523
  • 2
  • 18
  • 30
0

Every Java class where you want to inject beans must be also be a Spring managed bean, even if it isn't explicitly used anywhere else.

Depending on the size of your project, I'd recommend using explicit configuration anyway. This, for example, would work:

@Configuration
public class ApplicationConfiguration {
  @Bean
  public ProcessRequest processRequest() {
    return new ProcessRequest();
  }

  @Bean
  public DefaultConfig defaultConfig() {
    return new DefaultConfig();
  }
} 

Then, on your main method class,

@SpringBootApplication(scanBasePackageClasses = ApplicationConfiguration.class)
public class Application { ... }

Alternately, just annotate all Spring managed beans with @Component

In addition, if this configuration class is nothing but properties, you should look into @ConfigurationProperties

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38
  • "*I'd recommend using explicit configuration anyway*" beeecause? – Michael Dec 10 '19 at 17:51
  • Many reasons: Complete view of registered beans without hunting for `@Component`, ability to override beans (e.g, `@ConditionalOnMissingBean`), easier constructor injection (e.g. environment properties), ability to not explicitly tie an implementation to Spring. Probably more. – Christopher Schneider Dec 10 '19 at 17:56