#Value not working even if key is resolved [Spring 4] - spring

My constants.java is like this:
package com.sample.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component(value="ConstantValues")
public class Constants {
static {
System.out.println("Class loaded: Constants");
}
#Value("${allowxmlvalue}")
private String logXMLString;
public String getLogXMLString() {
return logXMLString;
}
public void setLogXMLString(String logXMLString) {
this.logXMLString = logXMLString;
}
}
I am loading Application using spring boot like this:
#Configuration
#PropertySources({
#PropertySource("file:/home/ubuntu/config/properties/app.properties"),
#PropertySource("file:/home/ubuntu/config/properties/util.properties")})
#ComponentScan(basePackages = {
"com.sample.utils"
})
#EnableAutoConfiguration
public class Sample {
static Logger logger = Logger.getLogger(Sample.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(Sample.class, args);
Constants cons = new Constants();
logger.info("Print XML: "+cons.getLogXMLString());
}
}
I can see following print in logs:
20:40:45,865 DEBUG PropertySourcesPropertyResolver:90 - Found key
'allowxmlvalue' in [URL
[/home/ubuntu/config/properties/app.properties]] with type [String]
and value 'print xml value'
But still Print XML: null is printed out, What I may be missing here?

You're creating the Constants instance yourself instead of getting the Spring managed bean. Obviously its fields won't have been processed by Spring.
So get it from Spring
ApplicationContext ctx = SpringApplication.run(Sample.class, args);
Constants cons = ctx.getBean(Constants.class);
or inject it into the Sample bean.

Related

Spring boot get #value within a class extending a dependency

Folks, am trying to access config properties from within a class that I've extended from a dependency. Apparently the config class is returning a null pointer in my implementation class (CustomUtil).
pom.xml
...
<dependency>
<groupId>com.utilapp</groupId> //3rd party library that I need to use
<artifactId>util-lib</artifactId>
</dependency>
...
The BaseUtil is from the dependency. CustomUtil is a bean in my SpringBoot App & am overriding the method as below to check the key from the config property ignoreKeyList .
package com.myapp
...
#Component
public class CustomUtil extends BaseUtil {
#Autowired
private ClientConfig clientConfig; // This returns null!
#Override
protected boolean shouldExcludeFromList(String key) {
return this.clientConfig.getIgnoreKeyList.contains(key); // their library/util excludes certain keywords from the content being formatted
}
}
Config class in my SpringBoot App.
package com.myapp
...
#Configuration
#Getter
#Setter
public class ClientConfig {
#Value("${myapp.ignorekeys}")
private List<String> ignoreKeyList;
...
...
}
Main class in my app.
package com.myapp
...
#SpringBootApplication
public class MyClientApplication {
public static void main(String[] args) {
SpringApplication.run(MyClientApplication.class, args);
}
}
Service implementation class in my SpringBoot App.
package com.myapp
import com.utilapp.formatters.FormatBuilder;
import com.utilapp.formatters.TextFormatter;
...
#Service
public class ServiceImpl implements MyService {
#Autowired
private ClientConfig clientConfig; // works here!
public String formatContent(String content) {
TextFormatter formatter = this.getTextFormatter();
return formatter.parseAndFormat(content);
}
private TextFormatter getTextFormatter() {
return FormatBuilder.custom() // FormatBuilder is from the dependency
.withApplication(this.clientConfig.getAppName()) // In their library, the BaseUtil gets instantiated which am extending in my CustomUtil bean
...
...
.withIndentSupport(Boolean.TRUE)
.build();
}
}
Controller in my app
package com.myapp
...
#RestController
#RequestMapping("/app/v1")
public class StyleController {
#Autowired
private MyService myService;
#GetMapping("/format")
public String formatContent(#RequestParam String content) {
return this.myService.formatContent(content);
}
}
When I debug ClientConfig, it loads the properties from the config file; application.properties in src\main\resources.
But for some reason the ClientConfig is returning null from within the CustomUtil bean. Not sure why!
I originally thought it was due to ordering of bean instantiation. I tried playing around with #Order, #DependsOn, #ComponentScan, #ScanBasePackages etc, but none yields.
Any pointers/guidance please.
Thanks.

Spring Boot/Batch with multiple #Configuration classes not instantiating bean (or loading properties)

So first, apologies if this seems to similar to other problems - I've looked, tried the proposed solutions, and none have solved the problems. First the sanitized code...
package com.mine.batchMain;
#SpringBootApplication
#Configuration
public class MyApplication implements CommandLineRunner {
//...
public static void main(String[] args) {
//....
}
}
package com.mine.batchMain;
//...
import com.min.batchMain.firstSteps.FirstStepConfigHolder;
//...
#Configuration
#EnableBatchProcessing
public class BatchConfigurer {
//....
#Autowired
private FirstStepConfigHolder firstStep;
//...
#Bean
public Step defineFirstStep() {
return stepBuilder.get("First Step")
.chunk<MyPOJO, MyPOJO>(batchSize)
.readerfirstStep.fetcher())
.writer(firstStep.extracter())
.listener(firstStep.listen())
.build();
}
//....
}
package com.mine.batchMain.firstSteps;
//...
import com.mine.batchMain.common.MyRepo;
import com.mine.batchMain.firstSteps.DocFetcher;
//...
#Configuration
#EnableJPARepositories
public class FirstStepConfigHolder {
//....
#Value("${myapp.dbUrl}")
String dbUrl;
#Value("${myapp.dbSchema}")
String dbSchema;
#Value("${myapp.dbUser}")
String dbUser;
#Value("${myapp.encDbPass}")
String encryptDbPass;
#Value("${myapp.dbDriver}")
String dbDriver;
#Value("${myapp.maxDocSize}")
String maxDocSize;
#Value("${myapp.maxNumDocs}")
String maxNumDocs;
#Bean
public DocFetcher fetcher() {
log.trace("Creating DocFetcher.")
return new DocFetcher(myDb());
}
#Bean
public MyRepo myDb() {
log.trace("Creating repo.");
MyRepo retDb = new MyRepo(myDataSource());
retDb.setMaxNumDocs(Integer.valueOf(maxNumDocs));
retDb.setMaxDocSize(Integer.valueOf(maxDocSize));
log.debug("Confirming db class state:"+retDb.toString());
return retDb;
}
private DataSource myDataSource() {
DriverManagerDataSource retDs = new DriverManagerDataSource(dbUrl, dbUser, decrypt(encryptDbPass));
retDs.setDriverClassName(dbDriver);
return retDs;
}
}
The problems/symptoms are these:
1) MyRepo is not picking up maxDocSize and maxNumDocs. (Logging shows the defaults)
2) Logging shows the trace call to "Creating DocFetcher", but not to "Creating repo.", nor is it showing the debug of the Repo state.
Which is frustrating, as according to what I know & understand, it should be picking them up. What am I missing and/or not understanding correctly?
Make sure that your BatchConfigurer.java file should be in the same package as your MainApplication.java file .
or,
Import BatchConfigurer.java in your MainApplication.java file.
Check this image for reference

Autowire working in unit test but not in main java class

I've a domain class that I want to auto-populate from external config. Here is my domain class:
#Data
#Configuration
#PropertySource("classpath:application.properties")
public class StudioVo {
#Value("${studio.code}")
private code;
#Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
Here is my context xml:
<bean class="org.springframework.batch.core.scope.StepScope" />
<bean id="ItemReader" class="com.sdm.studio.reader.StudioReader" scope="step">
<property name="studioVo" ref="StudioVo" />
</bean>
<bean id="StudioConfigVo" class="com.sdm.studio.domain.StudioVo" />
</bean>
Here is the class where I want to use the vo:
#Slf4j
#Data
public class StudioReader implements ItemReader<List<Studio>> {
private StudioVo studioVo;
public List<Studio> read() throws Exception {
System.out.println("getCode: " + studioVo.getCode()); //code is null here
return null;
}
}
However when I run it via unit test by autowiring, it runs fine. Like this:
#RunWith(SpringRunner.class)
#SpringBootTest
public class StudioTest {
#Autowired
private StudioVo studioVo;
#Test
public void testAutoPopulationOfStudio(){
System.out.println("getCode: "+ studioVo.getCode()); // works!
// Assert.assertTrue(studioVo.getCode().equals("102"));
}
}
Not sure what's going on here - I'm working with an old Spring Batch application wrapped in Spring Boot (so there is a mix of XML based and Java based config - and may be that is the cause of this issue). What am I missing?
In your StudioTest, you are autowiring StudioReader where as you missed the #Autowired in your StudioReader code, so add it as shown below:
#Slf4j
#Data
public class StudioReader implements ItemReader<List<Studio>> {
#Autowired //add this so that studioVo can be injected
private StudioVo studioVo;
//add other code
}
Please be certain to note that using #Autowire requires a chain of Spring-managed beans below it from wherever you are using it including the class in which you are using #Autowire. That is because Spring needs the precedent references to match up the object-reference hierarchy. I.e., in business logic layer ClassA, you want to #Autowire a field. ClassA itself needs to be a managed bean. Further, if the field you want to #Autowire holds an object that has referential dependencies to other objects (and most do), these also must be Spring-managed.
For example, the following will work:
package com.example.demo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MessageRunner {
private static SetterMessage setterMessage;
public static void main(String[] args) {
setterMessage = (SetterMessage) (new AnnotationConfigApplicationContext(DemoConfiguration.class)).getBean("setterMessage");
setterMessage.setMessage("Finally it works.");
p(setterMessage.getMessage());
}
private static void p(String s) {
System.out.println(s);
}
}
DemoConfiguration.java looks like this:
package com.example.demo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan(basePackages = "com.example.demo")
public class DemoConfiguration {
}
SetterMessage.java, this:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
#Service
#Scope("prototype")
public class SetterMessage {
private String message = null;
#Autowired
private SetterMessage2 setterMessage2;
public String getMessage(){
return message+setterMessage2.getSubMessage();
}
public void setMessage(String message) {
this.message = message;
setterMessage2.setSubMessage("("+message+")");
}
}
SetterMessage2.java:
package com.example.demo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
#Service
#Scope("prototype")
public class SetterMessage2 {
private String subMsg = "";
public void setSubMessage(String msg) {
subMsg = msg;
}
public String getSubMessage() {
return subMsg;
}
}
Note that SetterMessage2.java is annotated as a Component (#Service) but no field in it is autowired. That is because it's the end of the object reference chain. But because it is a Component, it can be autowired into SetterMessage.java. However look at MessageRunner.java's main() method and field declarations. Note that the class field SetterMessage is NOT autowired. If it were annotated as #Autowired, main() would fail at runtime, throwing an NPE with the reference to setterMessage in main(). This is because MessageRunner.java is not marked as some kind of component. So we need to grab a valid instance of MessageSetter from the application context and use it.
To emphasize, the following version of MessageRunner.java's main() method WILL FAIL, throwing an NPE, if MessageRunner.java looked like this:
...
public class MessageRunner {
#Autowired // <-- This will not do the job for us
private static SetterMessage setterMessage;
public static void main(String[] args) {
setterMessage.setMessage("Finally it works."); // NPE here on ref to setterMessage
p(setterMessage.getMessage());
}
...
This is a real gotchya for people new to Spring. In fact, I'd place it among the Top Five Spring Newbie Discouragers and a really evil, pernicious detail that has caused new Spring programmers countless hours in aggravation and Google searches. So I do hope that noting this phenom here will save at least some newbies time and high blood pressure spikes.
Note: If you go to create the above classes in your IDE, bear in mind these were developed with Spring Boot enabled.

Spring Boot: NoUniqueBeanDefinitionException between test and main

I have a SpringBoot main/Application.java class
#SpringBootApplication
#ComponentScan(value = "com.nfl.dm.shield", excludeFilters =
{
#ComponentScan.Filter(value = MemoryRepository.class, type = FilterType.ASSIGNABLE_TYPE)
}
)
public class Application {
final static Logger LOG = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
LOG.info("Booting application...");
SpringApplication.run(Application.class, args);
}
}
and a similar one for Test
#Configuration
#ComponentScan(basePackages = {"com.nfl.dm.shield"}, excludeFilters =
{
#ComponentScan.Filter(value = MySqlRepository.class, type = FilterType.ASSIGNABLE_TYPE)
}
)
public class ApplicationTestConfig {
}
The main code runs correctly. The test code throws NoUniqueBeanDefinitionException, appearing to not properly filter out the unwanted MySqlRepository component.
After over a day of trying many different ways to exclude the unwanted bean, the core issue turned out to be the fact that #ComponentScan was pulling in both Application and ApplicationTest, resulting in an additional scan for Application, resulting in the unwanted service being loaded.
The solution, add:
#ComponentScan.Filter(value = Application.class, type = FilterType.ASSIGNABLE_TYPE)
to the list in ApplicationTestConfig.java. So, when ApplicationTestConfig is loaded and triggers the component scan, it ignores Application (and all of Application's specific configurations).

Spring Boot not using application.properties for spring.groovy.template.cache

I have a very simple Spring Boot application with classes detailed below.
My problem is with the application.properties file and how they get auto-configured. I'm trying to get Groovy Templates to update in dev by setting 'spring.groovy.template.cache: false', however this is not working. I added two more properties to see if the application.properties file was being read. The 'logging.level.org.springframework.web: ERROR' still results in INFO level messages printed to the console. However, some.prop is read correctly into the MyBean class on application start.
Is there a configuration declaration I'm missing for these properties?
src/main/resources/application.properties:
spring.groovy.template.cache: false
logging.level.org.springframework.web: ERROR
some.prop: bob
src/main/java/sample/MyBean.java:
#Component
public class MyBean {
#Value("${some.prop}")
private String prop;
public MyBean() {}
#PostConstruct
public void init() {
System.out.println("================== " + prop + "================== ");
}
}
and src/main/java/sample/Application.java:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
and src/main/java/sample/com/example/MainController.java
#Controller
public class MainController {
#RequestMapping(value="/login", method = RequestMethod.GET)
public ModelAndView risk(#RequestParam Optional<String> error) {
return new ModelAndView("views/login", "error", error);
}
}
It seems you missing scanned your package "sample". Please make sure that you have scanned it.
#ComponentScan({
"sample" })
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Also, your application.properties is right. No problem with it.
It appears the solution was much simpler than I thought:
gradle bootRun
should be used to hot reload templates
gradle run does not work (all compiled classes are just built in build/ )

Resources