Issue loading external properties(which is added to classpath) in springboot - spring-boot

I need to load the application properties externally into my Springboot application. In my production system; we are adding the properties to classpath; so to replicate that I am adding the properties file to my class path and trying to load the properties using the #PropertyResource in SpringBoot but it is not loading
Using eclipse; I have added the properties file to my classpath(added the file to buildpath)
With Springboot and using #PropertyResource; the application is failing to load the properties.
#SpringbootApplication
#PropertySource(ignoreResourceNotFoind=true,value="classpath:myapp.properties")
public class MyApp {
public static void main(String[] args) {
springApplication.run(MyApp.class,args);
}
}
#Service
public class myService{
#Value("${name}")
private String name;
private void printName() {
System.out.println(" Name:"+name);
}
}

In order for #PropertySource to work you have to configure a PropertySourcesPlaceholderConfigurer. Add this to your MyApp class:
#Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfig() {
return new PropertySourcesPlaceholderConfigurer();
}
Also of note - ignoreResourceNotFoind is misspelled in your example (foind -> found)

Related

Auto reloading properties file after updating it in Spring. How to achieve this?

Instead of restarting the sever every time i make any change i want my properties file to be auto refreshed. My properties file in the src/main/resources
You can use the Actuator, basically you put your configuration beans inside a refresh scope :
#SpringBootApplication
public class ExampleServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleServiceApplication.class, args);
}
#RestController
#RefreshScope
class ExampleController {
#Value("${foo.bar}")
private String value;
#RequestMapping
public String sayValue() {
return value;
}
}
}
Then when you change the configuration. you call the refresh endpoint for the actuator.
curl -X POST http://localhost:8080/refresh

Identifying Start-Class during runtime in spring boot

I am trying to identify a way to know the name of the Main-Class that started SpringBoot.
e.g.
#SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
#RestController
public class SampleController
{
#GetMapping("/hello")
pubic String sayHello()
{
System.out.println("Need start class name: "+System.getProperty("sun.java.command"));
return "hello";
}
}
}
When I run the springboot using java -jar myappname.jar ,the System.getProperty("sun.java.command") returns org.springframework.boot.loader.JarLauncher
Can anyone advise, how can I get the name of actual run class. I have tried specifying the start-class attribute in the manifest.mf. It still gave me org.springframework.boot.loader.JarLauncher as the start-class.
You should be able to #Autowire in the ApplicationContext and then do context.getBeansWithAnnotation(SpringBootApplication.class).values().toArray()[0].getClass().getName(), which will give you the first (and presumably only) bean in the context annotated with #SpringBootApplication

How do I register my custom Environment Post Processor in Spring Boot 2.0?

I followed the exact steps in this documentation.
I have the following entries in my META-INF/spring.factories
org.springframework.boot.env.EnvironmentPostProcessor=com.mygroup.myapp.CustomEnvironmentPostProcessor
My post processor:
public class CustomEnvironmentPostProcessor
implements EnvironmentPostProcessor, Ordered {
..
}
I don't see anything in the logs as if it didn't get registered or not existing.
I unzipped the JAR and I can see META-INF/spring.factories. I can also see BOOT-INF/classes directly from the root.
What am I missing here?
There is no elegant way to solve this. You can make something like this :
#Component
public class CustomEnvironmentPostProcessor implements
EnvironmentPostProcessor, ApplicationListener<ApplicationEvent> {
private static final DeferredLog log = new DeferredLog();
#Override
public void postProcessEnvironment(
ConfigurableEnvironment env, SpringApplication app) {
log.error("This should be printed");
}
#Override
public void onApplicationEvent(ApplicationEvent event) {
log.replayTo(CustomEnvironmentPostProcessor.class);
}
}
define spring.factories file
Environment Post Processor
org.springframework.boot.env.EnvironmentPostProcessor=\
class name with package
As of Spring Boot 2.4, there are now optional constructor parameters that provide access to deferred logs:
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {
private final Log log;
public CustomEnvironmentPostProcessor(Log log) {
this.log = log;
}
#Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
log.info("CustomEnvironmentPostProcessor!!");
}
}
NOTE: The log messages are deferred so they will appear in the log output only after the logging system has been initialized.
See https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/env/EnvironmentPostProcessor.html

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/ )

Spring Boot WS application cannot load external property

In my Spring-Boot Web Service application, I want to load a property named appName with value defined in application.properties.
#Endpoint
public class RasEndpoint {
private static final String NAMESPACE_URI = "http://www.mycompany.com/schema/ras/ras-request/V1";
#Value("${appName}")
private String appName;
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "getProductRequest")
#ResponsePayload
public GetProductResponse getProduct(#RequestPayload GetProductRequest request) {
System.out.println("appName: " + appName);
GetProductResponse response = generateStubbedOkResponse();
return response;
}
application.properties has the following entry
appName=ras-otc
I get the application started via the main Application class as shown below
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
However, when I run the app, I get the following error
Caused by: java.lang.IllegalArgumentException: Could not resolve
placeholder 'appName' in string value "${appName}"
Do you guys know what I'm doing wrong?
Appreciate any help.
As Dave mentioned in the comment above, the properties file was not loaded into the classpath.
The properties file was located in /src/main/resources folder, which was added to source, under build path in Eclipse IDE, however had an exclusion rule applied which prevented the properties file from being loaded into the classpath. By removing the exclusion, I was able to load the properties correctly.

Resources