This question is similar to this one : Spring: How to inject a value to static field?
but with a twist..
I need to use the injected values in the static methods @BeforeClass
& @AfterClass
.
I tried creating @Component
and feeding the values from application.properties
to the static fields on @PostConstruct
Alas... i get a null pointer exception.
So the question can be rephrased as
"How can i use values from application properties inside my @AfterClass / @BeforeClass methods ?"
The @Component
:
package com.vulog.pdfgenerator.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class GlobalValue {
@Value("${spring.thymeleaf.prefix}")
protected String prefix;
@Value("${template.tmpfolder}")
private String tmpfolder;
public static String tmpFolderStatic;
@Value("${template.classpath}")
private String classpath;
public static String classpathStatic;
/**
* Hack! we want to use the static methods BeforeClass/AfterClass
* so the app property values should be kept static as well.
*/
@PostConstruct
public void init(){
classpathStatic = classpath;
tmpFolderStatic = tmpfolder;
}
}
I autowire this to a test class and try to get the GlobalValue.classpathStatic but it throws.
@Autowired
GlobalValue globalValue;