Spring Java config: import properties file - spring

How to import a properties file and access a property while using Java configuration to configure Spring.
I want to do all in java. Is there a way to do it?
I tried to use #ImportResource("classpath:config.properties") but did not work.

I've done this on my #Configuration class using:
#PropertySource(value="classpath:application.properties")
You can get the properties in number a number of ways:
Inject Environment into configuration beans that need the properties and use environment.getProperty("my.property.value"), or
Annotate a property with #Value as outlined here.

Related

Loading application.properties file in a non SpringBoot application

I've a set of microservices built with spring boot and using feign as client. All is working perfectly, but I have a problem with a non SpringBoot application.
In this case I would like to use configuration properties file (application.properties) to configure different client (like Ribbon).
In my configuration bean I've included #ImportAutoConfiguration for all the components, but configuration is not loaded from properties file.
Is there a way to perform this?
Thanks!
import org.springframework.context.annotation.PropertySource;
#RequestMapping("/url")
#Controller
#PropertySource("classpath:config.properties")
public class StudentController{
#Autowired
private Environment env;
//get properties using keys
//env.getProperty("key");
}
and put config.properties file inside src/main/resources

about spring Bean Factory Post Processors

How to configure BeanFactoryPostProcessors (like PropertyPlaceHolderConfigurer) in 100 percent code based Spring environment without using xml file?
i have configured through xml by using context:property-placeholder tag but when it comes to 100 percent code approach i want to eliminate xml file so i configured user defined class using stereotype annotation and i linked them with Configuration class by using #ComponentScan but i dont know how to configure this BeanFactoryPostProcessors.So this is my doubt.

How to add properties programmatically(just like adding key-value into application.properties)?

I have some common properties that every projects should set, such as
feign.hystrix.enabled=false
feign.httpclient.enabled=true
I don't want to repeatedly add these props in every project so I'm going to create an extra jar file containing #Configruation class. How to add properties in #Configuration class? Thanks!
PropertySources
You may load an application.properties from another jar this way:
#PropertySources({
#PropertySource("classpath:common.properties")
})
#Configuration
public class SomeJavaConfig {
}
You can find the reference in Spring's documentation:
Spring Boot uses a very particular PropertySource order that is
designed to allow sensible overriding of values. Properties are
considered in the following order:
...
#PropertySource annotations on your #Configuration classes.
Spring-cloud-config
I won't go in all the details, but another option is to use spring-cloud-config to define these properties in a git (using spring-cloud-config-server). Then, have your spring-boot application load the application.properties using spring-cloud-config-client directly from git.
Check this:
https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html
https://spring.io/guides/gs/centralized-configuration/

spring-boot without #SpringBootApplication

I am attempting to migrate a spring, non-boot, app to a boot app. The current one builds a war file. Following these instructions, I am walking through the steps to migrate.
I am finding that the #SpringBootApplication annotation forces a lot of things to fail. For instance, it tries to auto config security when I really need the existing xml security config to remain as is. I found that I can override #EnableAutoConfiguration and exclude configuration classes (.i.e. SecurityAutoConfiguration.class). But I am finding it is doing this a great deal for the items I already have on my classpath. I decided it would be better to remove #SpringBootApplication and replace it with just #Configuration, #ComponentScan and #ImportResource to load my original context xml. The class extends SpringBootServletInitializer so that I can register my custom servlets and filters.
What I have found, it now no longer knows to load the application.yml or bootstrap.yml. What triggers auto configuration of these files? Do I fall back to loading with the traditional properties placeholder configurers? I want to avoid this as the next step is to hook it up to spring cloud config to centralize the management of the application configuration.
#SpringBootApplication is a alternative for #Configuration, #EnableAutoConfiguration and #ComponentScan.
Probably you want use #Configuration + #ComponentScan. If you want load xml configuration you can use: #ImportResource annotation.
If you want use autoconfiguration, but you can disable a few auto configurations, eg:
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
Details:
http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html
http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-configuration-classes.html
http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html

Spring #ComponentScan annotation

Why do we use #ComponentScan annotation in spring ? I am not being able to figure out the exact difference between context:componentscan in xml file and #ComponentScan annotations in java and how to use them in case of classes which have been annotated with #Configuration
In short, there is no difference.
#ComponentScan - Used with #Configuration classes, for example, when you have java-based configurations for Spring.
It provides support parallel with Spring XML's <context:component-scan> element. The purpose of those two are exactly the same - to scan spring components.
But there is one advantage of xml based configuration - you don't need to compile your java code. When you added some package under <context:component-scan> then you don't need to compile java source code. Place the xml configuration file in your server and restart it.

Resources