1

When I try to print the variable that I have autowired, it prints "null" instead of the value I set it to, "Example." I can't quite figure out what I'm missing

In my AppConfig class I have:

@Configuration
public class ApplicationConfiguration {
   @Bean
     public String tableName(){
       return "Example";
     }
}

In my other class, DAOMethods, that I want to autowire the variable in:

@Component
public class DAOMethods {
    @Autowired
    private String tableName;

    public void print(){
        System.out.println(tableName);
    }
}
Dani Trevino
  • 21
  • 1
  • 4
  • do your Classes exists in the same root folder ? – DEV Aug 11 '21 at 18:50
  • 1
    @Hamza they exist in different packages; With AppConfig living in a config folder and DAOMethods in client->dynamodb->util folder. Config and Client are folders under the main->java folder – Dani Trevino Aug 11 '21 at 18:58
  • check the replies at ["Why is my spring autowired field null"](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null), duplicate – Padua Aug 11 '21 at 18:59
  • 1
    Does this answer your question? [Why is my Spring @Autowired field null?](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – Elmar Brauch Aug 11 '21 at 19:14
  • 1
    @ElmarBrauch I have looked through the replies, and it made me realize I was missing the @/Configuration notation in AppConfig but I'm still getting null values for the variables – Dani Trevino Aug 11 '21 at 19:18
  • @Padua I have looked through the replies, and it made me realize I was missing the @/Configuration notation in AppConfig but I'm still getting null values for the variables – Dani Trevino Aug 11 '21 at 19:20
  • 2
    By default, doesn't `@Configuration` only scans current and sub packages? Thus your `DAOMethods` is not detected by Spring and not initialized. – Gaël J Aug 11 '21 at 19:21
  • let me guess, `new DAOMethods().print();` ????? – Antoniossss Aug 11 '21 at 20:37

2 Answers2

2

They exist in different packages; With AppConfig living in a config folder and DAOMethods in client->dynamodb->util folder. Config and Client are folders under the main->java folder

The added @Configuration annotation scans for the beans in the current and its subpackages. You need to explicitly tell the application to scan the required packages. So you can do:

@SpringBootApplication (scanBasePackages = {"config", "client"})

OR,

You need to keep the config and other classes which uses that config in same root package. You can put the config folder and client folder under same package, say com.application and then add the following in your main class:

@SpringBootApplication(scanBasePackages = "com.application")

Now run the application.

devReddit
  • 2,696
  • 1
  • 5
  • 20
  • 1
    Or without having them in same package, you need to explicitly say which packages to scan – Gaël J Aug 11 '21 at 20:25
  • updated my answer, thanks for mentioning, missed that part while writing the answer – devReddit Aug 11 '21 at 20:31
  • @devReddit Thanks for your answer! This solved the issue – Dani Trevino Aug 12 '21 at 00:13
  • @DaniTrevino if you think this or any other answer is helpful and worked for you, please consider [accepting](https://stackoverflow.com/help/accepted-answer) the best answer – devReddit Aug 12 '21 at 00:17
  • My situation is that autowiring was working fine and works during the boot process. It's only during runtime that the beans are lost and a lot of fields that should have been autowired turned out to be null. I can print all the beans and even access the instance of the bean in the offending class during boot time but during runtime the fields become null. – TheRealChx101 Jun 19 '22 at 16:03
  • @TheRealChx101 please provide more details about your problem. If you think you cannot find the correct answer through browsing the existing questions, you may ask one, of course providing the scenario and necessary codes in detail. Share the link here if you want. Thank you – devReddit Jun 24 '22 at 11:05
  • @devReddit The same thing just happened again in another spring boot project after adding some JPA repositories and methods in a controller. I've commented out the code and the issue is still happening. I'm not sure what's causing the issue. Maybe my maven cache is broken or something? – TheRealChx101 Jun 26 '22 at 19:27
  • @devReddit I think I may have figured out the issue. The methods in the controller (annotated as HTTP/S endpoints) must be public or package private and not entirely private. If it's the latter, any injected instances end up being null. Perhaps this is spring boot's security feature? – TheRealChx101 Jun 26 '22 at 19:33
  • In my case, it was a problem that the configuration was not scanned at all (answer above) + providing an object as a formal parameter to the configuration bean, not as an autowired field. – JackTheKnife Dec 15 '22 at 19:43
0

You problem have many solutions F.e. you can create config-class with required set of parameters and next autowire it (Injecting values with @Value annotation from application configuration file is good practice):

@Component
public class CustomConfiguration {
    @Value("${table.name}")
    private String tableName;

    @Value("${some.value}")
    private Integer someValue;

    public String getTableName() {
            return tableName;
    }

     public Integer getsomeValue() {
            return someValue;
    }
}

And you application.properties will looks like:

some.value=1
table.name=Example

Or you can simply inject single value from configuration with @Value annotation

One of solutions is using bean name in @Value annotation:

@Configuration
public class ApplicationConfiguration {
   @Bean
     public String tableName(){
       return "Example";
     }
}

@Component
public class DAOMethods {
    @Value(#{tableName})
    private String tableName;
}

More examples you can see in this question: Autowire a string from Spring @Configuration class?