How do I resolve spring configuration property without #ConfigurationProperties? - java

I'm using Java 8 and Spring Boot. And I'm trying to solve the warning "Cannot resolve configuration property" from IDEA:
(application.properties)
But for this param I don't have any #ConfigurationProperties class to be handled by the "spring-boot-configuration-processor"
I'm only using this param like this:
import org.springframework.beans.factory.annotation.Value;
#Value("${some.param}")
private String param;
I tried to create de metadata file manually but that didn't solve the problem either.
Does anyone have a tip for this?
This is my Application Class:
#SpringBootApplication
#EnableConfigurationProperties
#ComponentScan(basePackages = {"com.sample"})
#ConfigurationPropertiesScan
#EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Looks like you can use additional metadata for this. Although it would be nice to automatically generate this from the #Value (or similar) annotation automatically...
https://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html#appendix.configuration-metadata.annotation-processor.adding-additional-metadata

Related

getClass().getAnnotations() does not return all the annotations when using spring application context

When I ask the spring application context to give me the main class of my application, using applicationContext.getBeansWithAnnotation(...), I can only access two of the class annotations :
#SpringBootApplication
#EnableJpaRepositories
So if I ask for the bean with the annotation #EntityScan, the spring context correctly give me the main class, but I can not access the #EntityScan annotation, as if it was not present.
Why ?
Main class
#SpringBootApplication(scanBasePackages = "foo")
#EnableJpaRepositories(basePackages = "foo.repository", repositoryBaseClass = AppRepositoryImpl.class)
#EntityScan(basePackages = "foo.entity") // Spring annotation -> I don't see it
#MiscScan(basePackages = "foo.misc") // My own annotation -> I don't see it
public class APIApplication {
public static void main(String[] args) {
SpringApplication.run(APIApplication.class, args);
}
}
Retrieving the main class from the spring application context
// I can search the main class using any of the 4 aforementioned annotations, it does not matter.
applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass().getAnnotations()
Note that :
Changing the order of the annotations does not matter.
APIApplication.class.getAnnotations() correctly give me all the annotations.
EDIT : The class was proxied by spring, so the get the real class and all the annotations I have to do this :
applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass().getSuperclass().getAnnotations()
EDIT 2 : I found a cleaner solution :
org.springframework.util.ClassUtils.getUserClass(applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass()).getAnnotations()

After adding #ComponentScan to load beans from a jar my controllers isn't scan and i get 404

I have a spring-boot application (Java8, spring-boot 2.1.4-RELEASE).
One of the services in the business layer need to #Autowire a bean from a certain jars in my classpath. In order to achieve that i had to add #ComponentScan({"package.to.my.bean.inside.the.jar"}) and it was magically scanned and wired successfully (this was added to the main spring-boot class which declare the main method).
However, since then my controllers aren't scanned hence the DispatcherServlet is returning 404 for every request i trigger (default dispatcher).
Actually my entire spring-boot app annotations is being ignored - no scan is performed.
Just to emphasis - the application worked perfectly before adding the #ComponentScan.
Main spring-boot app class:
package com.liav.ezer;
// This is the problematic addition that cause the endpoints to stop
// inside a jar in the classpath, in com.internal.jar package resides an
// object which i need to wire at run time
#ComponentScan({"com.internal.jar"})
#SpringBootApplication
public class JobsApplication {
public static void main(String[] args) {
SpringApplication.run(JobsApplication .class, args);
}
}
Controller example:
package com.liav.ezer.controller;
#RestController
#EnableAutoConfiguration
#RequestMapping(path = "/jobs")
public class JobController {
#GetMapping(path="/create", produces = "application/json")
#ResponseStatus(HttpStatus.OK)
String createJob(#RequestParam() String jobName) String jobName){
return "job created...";
}
}
I tried adding my spring-boot app base package to the list of packages in the #ComponentScan with no luck.
I tried narrowing down the scope of the package declaration to be only on the class which i need with no luck.
Here is the code
According to Spring documentation
Configures component scanning directives for use with #Configuration
classes. Provides support parallel with Spring XML's
element. Either basePackageClasses() or
basePackages() (or its alias value()) may be specified to define
specific packages to scan. If specific packages are not defined,
scanning will occur from the package of the class that declares this
annotation.
In your case when you are adding
#ComponentScan({"com.internal.jar"})
you are disabling scanning of
com.liav.ezer.controller
To fix it you can do the following configuration
#ComponentScan(basePackages = {"com.internal.jar", "com.liav.ezer.controller"})
If so, remove #ComponentScan, can declare that bean in yourself configuration.
try below
#SpringBootApplication
public class JobsApplication {
public static void main(String[] args) {
SpringApplication.run(JobsApplication .class, args);
}
#Bean
public BeanInOtherJar xxBean(){
return new com.internal.jar.XXX();
}
}

Spring boot how to pick externalized spring properties file

I have this configurations which needs to be used for a spring boot application.
server.port=8085
server.servlet.context-path=/authserver
#data source
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=<url>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
By default spring-boot picks up up the application.properties file located in src/main/resources/
I want to alter this path and direct spring boot to different application.properties file
I can achieve this using
java -jar app.jar --spring.config.location=classpath:/another-location.properties
Is there any any alternative solution I can achieve this without passing args through command line?
I was using this
#PropertySource("file:C:\Users\test\.test\test.properties")
#ConfigurationProperties(prefix = "spring")
public class Configuration {
private String ddlAuto;
private String url;
private String username;
private String password;
private String driverClassName;
}
in my Main class
#SpringBootApplication
#EnableConfigurationProperties(Configuration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
There after I tried executing the app commenting out all datasource properties in application.properties under src/main/resources/
But it keeps giving me the error mentioned bellow and application fails to start
I was referring this tutorial : https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/
but as it's mentioned I get this error when i start the spring boot application
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target org.springframework.boot.context.properties.bind.BindException:
Any help on this would be appreciated
The recommended way to have externalized properties is to use the spring.config.location system property, by starting your application like so:
java -jar -Dspring.config.location=/path/to/my/file.properties app.jar
The reason for this is that you don't add coupling between your code and your filesystem hierarchy.
Before Spring Boot 2.0 this property is additive, meaning that it will complement the default locations. After Spring Boot 2.0, spring.config.location replaces the default locations (e.g. classpath src/main/resources/application.properties). To keep the additive behaviour after 2.0, use spring.config.additional-location instead.
Please see here for official documentation on this matter.
I am able to make it work properly on Spring Boot 2.1.2.RELEASE. This is what I have done:
I have a test.properties in my /tmp folder with the following content:
test.myprop=hello
I also have the usual property file in the resources folder:
myprop=world
I have created a class for the custom property file:
#Configuration
#PropertySource("file:/tmp/test.properties")
#ConfigurationProperties(prefix = "test")
public class TestConfig {
private String myprop;
public String getMyprop() {
return myprop;
}
public void setMyprop(String myprop) {
this.myprop = myprop;
}
}
And then in my main class I have enabled to configuration properties:
#EnableConfigurationProperties(TestConfig.class)
#SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Now I have this test controller:
#RestController
public class TestController {
#Value("${test.myprop}")
private String externalisedProp;
#Value("${myprop}")
private String prop;
#GetMapping("test")
public void test() {
System.out.println("externalised: " + externalisedProp);
System.out.println("not externalised" + prop);
}
}
Which, once called, is properly printing:
externalised: hello
not externalised: world
My TestConfig class is in the same package as the MyApp main class.
What I have done is very similar, almost identical, to your solution, are you sure your path is correct? Also, I can see that the content of your property file is not matching what you have in your config class, the prefix is different. Maybe that is the problem?
Edit:
I have tried to remove the #Configuration annotation from my property class (which you do not have as well) and it is not able to pick up the externalised properties anymore. The error is different though but you should try to add it.

How to read properties file with IntelliJ

I have a Spring Boot service, and I'm using IntelliJ to run it. I have a file call "application.properties" in my resources folder. I want intelliJ to read that .properties file, how do I do that? the only way I get it to use the properties in .properties file is to add them directly to Environment VM Option.
I tried doing things like
-Dspring.config.location:/src/main/resources/application.properties but that doesnt work.
Folder Structure:
Services
-src
-main
-resources
-application.properties
-target
-pom.xml
#Component
#Configuration
#EnableAutoConfiguration
#PropertySource({ "classpath:application.properties"})
#EntityScan(basePackages = "com.org")
public class AppConfig {
}
In case of Spring Boot, you don't have to pass any option when starting boot application.
When Spring boot application loads, it automatically checks if properties file exists in certain locations including src/main/resources, src/main/resources/config.
If you keep your properties file in these folders, the app automatically picks up the files and register properties. So in your AppConfig you don't need #Component, #EnableAutoConfiguration, #PropertySource, and #EntityScan because #Configuration already includes #Component which enables #Value to work.
I think the problem may arise when you call the property in the constructor of AppConfig because when the class is being constructed the #Value is not injected yet.
If you want to check if the property value is injected by Spring you can make a small test in the application class such as following
#SpringBootApplication
public class ApppropApplication {
#Autowired
private AppConfig appConfig;
public static void main(String[] args) {
SpringApplication.run(ApppropApplication.class, args);
}
#PostConstruct
public void init(){
System.out.println(appConfig.getTestProperty());
}
}
If your problem still exists, it would be great to provide more info (error logs and entire class structure)
Hope this helps! Happy Coding :)
The ideal way for your springboot project to include the properties file would be to import using the annotation "#PropertySource" in your starter class. Please re check your starter class. It should include something like this below
#PropertySource({ "classpath:application-example.properties"})
#EntityScan(basePackages = "com.org")
public class YourProjectLauncher extends SpringBootServletInitializer {
#Value("${your.db.url}")
private transient String dataSourceUrl;
#Value("${your.db.username}")
private transient String userName;
#Value("${your.db.password}")
private transient String password;
#Value("${your.db.driver-class-name}")
private transient String driverClass;
public static void main(String... args) {
SpringApplication.run(YourProjectLauncher.class, args);
}
Let me know if you have already done this and still facing the issue.
Also it would be best if you add the starter class in your question, that way it is easier to analyse the problem you are facing.
Note - If you have already done this, please add more information to the question, will be happy to help.

Spring Boot auto configuration order from external dependency

I have a problem trying to get my autoconfiguration working. I have two jars as follows, each have a spring.factories file where these two are enabled for EnableAutoConfigurationProperties.
This configuration is in my-package-mock.jar, it depends on my-package-real.jar below:
package org.packages.package.packageA;
#Configuration
#AutoConfigureBefore(AutoConfigurationB.class)
public class AutoConfigurationA {
#Bean
public MyService mockService() {
return new MyMockService();
}
}
This configuration is in my-package-real.jar:
package org.packages.package.packageB;
#Configuration
#ConditionalOnMissingBean(MyService.class)
public class AutoConfigurationB {
#Bean
public MyService realService() {
return new MyRealService();
}
}
Now the idea is that if my-package-mock.jar is included then AutoConfigurationB will not be processed as A is ordered to be before and by the time it gets to B MyService is already defined.
However, it does not work when used in a third project that includes these jars. It looks like the AutoConfigureOrder annotation is skipped when loading these jars from the classpath and these configurations are processed in the order the jvm loads these classes. In my particular case it does B first and at that point MyService is not yet defined and thus will instantiate the RealService bean. How can I get this to work?
Obviously this is a small example where a #Primary annotation on the mock will do the job, but that is not what I'm looking for.
Edit: it seems if the #SpringBootApplication annotated main is not a part of the package where these configurations are then things do work. E.g. the annotation is not in "org.packages.package" but "org.somewhereelse" then things work.
package org.packages.package;
#SpringBootApplication
public class TestApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(Collections.singletonList(TestApplication.class).toArray(), args);
}
}
#AutoConfigureBefore and #AutoConfigureAfter only apply when a configuration class is loaded as a result of auto-configuration being enabled and it being listed in spring.factories. When your auto-configuration classes are in org.packages.package (or a sub-package) and your main application class is in the same package, they're being found by Spring Framework's standard component scanning. This happens because #SpringBootApplication enables component scanning for the package of the class that it's annotating. As a result of this the auto-configuration-specific ordering doesn't apply.
To avoid the problem, you should places your auto-configuration classes in a package that isn't used by any application code.

Categories

Resources