3

I' m working in a Spring Boot project, consisted in several applications booted with embedded Tomcat and a unique application used only as library for running applications. In that library application I need to retrieve a value from application.properties file for a variable. Being it a library application, it has not a Main class, so the variable will be always null. My try was these:

 public class AuthAdapter implements IAuthAdapter {

    @Value("${signin.key}")
    private String key;

    @Override
    public String getSigningKey() {
        return key;
    }
}

When the getSigninKey() is invoked by an application that uses it, the variable key is null. How can I do to fill the variable key with the value present on appliaction.properties file, considering the situation explained before?

Marco
  • 31
  • 2
  • 3
  • two possible solutions would be (1) If you have controll over the library which provides `AuthAdapter` add component scan for package in which the `AuthAdapter` resides and annotate the `AuthAdapter` with `@Component` annotation (2) create a class annotated with `@Configuration` and in that class create a method annotated with `@Bean` which returns an instance of `AuthAdapter` –  Mar 25 '19 at 16:20
  • Yes, i have the control of the entire library, 'cos i wrote it from scratch. Can you provide me a simple snippet, based on solution 1)? thank you anyway :) – Marco Mar 25 '19 at 16:22
  • Possible duplicate of [How to read data from java properties file using Spring Boot](https://stackoverflow.com/questions/38281512/how-to-read-data-from-java-properties-file-using-spring-boot) – sam Mar 25 '19 at 17:11

4 Answers4

0

You need to annotate your AuthAdapter class with @Service/@Component/@Repository/@Configuration based on your requirement. By doing so, spring boot will inject the application.properties to your class during the spring context initialization.

Tech Guy
  • 417
  • 2
  • 7
  • 23
  • as the `AuthAdapter` comes from a library, component scan should be added to tell spring boot to scan the package of `AuthAdapter` –  Mar 25 '19 at 16:22
0

@Value Spring annotation

This annotation can be used for injecting values into fields in Spring-managed beans

So make AuthAdapter as spring bean in some config class

@Configuration
public class ApplicationConfig {

 @Bean
 public AuthAdapter getAuthAdapter() {

  return new AuthAdapter():
  }
}

with property in application.yml or application.properties

application.yml

signin:
  key: value

Or if AuthAdapter is annotated with any stereotype annotations here, Just enable that class in ComponentScan

@SpringBootApplication
@ComponentScan({ "com.project1", "com.library.AuthAdapter" })
public class Main {

    public static void main(String[] args) {

        SpringApplication.run(Main.class);

     }
}
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • Thank for your answer, i tried your second version (the @ComponentScan way) but the compiler says me that he could not resolve the placeholder. I answered my own question with the error code if you want to check it. – Marco Mar 25 '19 at 16:39
0

If the variable key==null, this means that Spring did not inject it. This could come from sevreral reasons:

  1. the property "signin.key" is not present in the application.properties

  2. SpringBoot did not start => make sure that in your application there is a main annoted with @springbootapplication

  3. The AuthAdapter is not instanciate by Spring:

=> The AuthAdapter shall not be instanciate like:

authAdapter = new AuthAdapter();

Instead, it must be instanciate and injected by Spring itself like:

@Autowired
private authAdapter AuthAdapter;

For that the AuthAdapter must be annoted with a springBoot annotation: @Service/@Component/@Repository/@Configuration.

Finally, the package containing the AuthAdapter class must be in the component scan perimeter to tell Spring to instanciate it.

David ROSEY
  • 1,414
  • 1
  • 7
  • 20
  • I think that the class is IAuthAdapter to autowire it. However i tried your version, but i still get the error i posted above. – Marco Mar 25 '19 at 16:51
0

Make sure that the "signin.key" is present in the application.properties and that the is present in the jar generated

David ROSEY
  • 1,414
  • 1
  • 7
  • 20