Spring Boot Configuration Not Working - spring

I'm using Spring boot.
I have an application.yml in src/main/resources. I then have a Configuration class that I am trying to get to load the application.yml. However, when I try to use the configuration class in another bean, the values are null. See the ApiHelper.java as to where the values are null.
I'm attempting to run the jar as so:
java -jar build/libs/app.jar
Am I doing something wrong? I've also tried using a properties file instead. When I unzip the jar file the configuration files are in the root.
src/main/resources/application.yml
spring:
profiles.active: default
---
spring:
profiles: default
api:
path: http://some-path
---
spring:
profiles: qa
api:
path: http://some-path2
src/main/java/AppConfig.java
#Configuration
#EnableConfigurationProperties(ApiConfig.class)
public class AppConfig {
#Autowired
private ApiConfig apiConfig;
#ConfigurationProperties(value = "api", exceptionIfInvalid=true)
public static class ApiConfig {
private String path;
public ApiConfig() {
System.out.println("Am I getting called?"); // yes it is
}
public String getPath() {
return path;
}
}
#Bean
public ApiHelper getApiHelper() {
return new ApiHelper();
}
}
src/main/java/ApiHelper.java
public class ApiHelper {
#Autowired
private ApiConfig apiConfig;
#PostConstruct
private void init() {
System.out.println(apiConfig); // prints ApiConfig#168498d6
System.out.println(apiConfig.getPath()); // prints null
}
}

It turns out that you need a setter to make it work:
#ConfigurationProperties(value = "api", exceptionIfInvalid=true)
public static class ApiConfig {
...
public void setPath(String path) {
this.path = path;
}
}

Related

how can application yaml value inject at runtime in spring boot?

I want to change the value of application.yaml at loading time.
ex) application.yaml
user.name: ${name}
Here, I want to put this value by calling an external API such as a vault, rather than a program argument when the jar is executed with the name value.
First of all, I think I need to write code that implements EnvironmentPostProcessor and calls external API, but I don't know how to inject that value. can I get help?
public class EnvironmentConfig implements EnvironmentPostProcessor {
#Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
// API CAll
// how can inject yaml value??
}
}
I don't know which way to orient myself.
OPTION 1: doing it via EnvironmentPostProcessor:
assuming you have registered you EnvironmentPostProcessor in /resources/META-INF/spring.factories file:
org.springframework.boot.env.EnvironmentPostProcessor=package.to.environment.config.EnvironmentConfig
all you need is to add your custom PropertySource:
public class EnvironmentConfig implements EnvironmentPostProcessor {
#Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
environment.getPropertySources()
.addFirst(new CustomPropertySource("customPropertySource"));
}
}
public class CustomPropertySource extends PropertySource<String> {
public CustomPropertySource(String name) {
super(name);
}
#Override
public Object getProperty(String name) {
if (name.equals("name")) {
return "MY CUSTOM RUNTIME VALUE";
}
return null;
}
}
OPTION 2: doing it via PropertySourcesPlaceholderConfigurer:
A class that is responsible for resolving these palceholders is a BeanPostProcessor called PropertySourcesPlaceholderConfigurer (see here).
So you could override it and provide you custom PropertySource that would resolve your needed property like so:
#Component
public class CustomConfigurer extends PropertySourcesPlaceholderConfigurer {
#Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, ConfigurablePropertyResolver propertyResolver) throws BeansException {
((ConfigurableEnvironment) beanFactoryToProcess.getBean("environment"))
.getPropertySources()
.addFirst(new CustomPropertySource("customPropertySource"));
super.processProperties(beanFactoryToProcess, propertyResolver);
}
}
use ConfigurationProperties for your properties and change it via an api like this:
#Component
#ConfigurationProperties(prefix = "user")
public class AppProperties {
private String name;
//getter and setter
}
#RestController
public class AppPropertiesController {
#Autowire
AppProperties prop;
#PostMapping("/changeProp/{name}")
public void change(#PathVariable String name){
prop.setName(name);
}
}

spring boot can not get configuration from application.yml

My spring boot application can not get configuration parameters from application.yml file. My main class as following:
#SpringBootApplication(scanBasePackages={"com.test"})
public class Main {
#Bean
public Validator validator(){
return new org.springframework.validation.beanvalidation.CustomValidatorBean();
}
public static void main(String[] args) throws IOException {
new SpringApplicationBuilder(Main.class)
.properties("application.yml")
.build()
.run(args);
}
}
My controller class as following:
#RestController
#RequestMapping("/test_traffic")
#Component
public class AnycastTrafficController {
#Autowired
TestService testService;
#GetMapping("/test")
public Object component() {
return testService.getTraffic();
}
}
My Service class as following:
#Service
public class TestService {
#Autowired
TestDao testDao;
public Object getTraffic() {
testDao.getTraff();
}
}
My Dao class as following:
#Component
public class TestDao {
#Autowired
MyDBConfig mydbConfig;
public DB getMyDBConfig () {
DB db = new DB(mydbConfig.id, mydbConfig.key);
return db;
}
}
My Config class as following:
#Configuration
#EnableConfigurationProperties
#ConfigurationProperties(prefix = "mydb")
public class MyDBConfig {
public String id;
public String key;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
My application.yml (which located at /src/main/resources)as following:
server:
port: 8003
context-path: /
logging:
level:
ROOT: INFO
org.springframework: INFO
org.springframework.data: INFO
com.alibaba: INFO
file: log/
docserver:
accessKeyId: 1111
accessKeySecret: 2222
---
spring:
profiles: dev
application:
name: test-application
mydb:
id: 1111111
key: 2222222
But when I started the Main class and request the url, it threw exception as following:
the id should not be empty.
that mean my Configuration class didn't get the configure data from yml file, so where I did wrong please. p.s(but the server port 8003 could be found by application). Thanks!
Your application.yml contains an invalid property option.
Instead of
spring:
profiles: dev
you should use
spring:
profiles:
active: dev
After correcting this this, the configuration processor should work properly.

springboot could not found feignclient

ERROR INFO LIKE BELOW:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field helloAgent in com.example.client.controller.Hello required a bean of
type 'com.example.common.agent.HelloAgent' that could not be found.
Action:
Consider defining a bean of type 'com.example.common.agent.HelloAgent' in
your configuration.
project structure:
module: test-client as feignclient caller.
module: test-server as feignclient interface implementation.
module: test-common put all feignclient together.
test-common:
package com.example.common.agent;
#FeignClient("hello")
public interface HelloAgent {
#GetMapping("/hello")
String hello(#RequestParam String msg);
}
test-server:(works fine)
package com.example.server.controller;
#RestController
public class Hello implements HelloAgent {
#Override
public String hello(#RequestParam String msg) {
System.out.println("get " + msg);
return "Hi " + msg;
}
}
test-client:
package com.example.client.controller;
#RestController
public class Hello {
#Autowired
private HelloAgent helloAgent;
#GetMapping("/test")
public String test() {
System.out.println("go");
String ret = helloAgent.hello("client");
System.out.println("back " + ret);
return ret;
}
}
----------------------------
#EnableEurekaClient
#EnableFeignClients
#SpringBootApplication
#ComponentScan(basePackages = {"com.example.common.agent","com.example.client.controller"})
public class TestClientApplication {
public static void main(String[] args) {
SpringApplication.run(TestClientApplication.class, args);
}
}
Is there anyway to put all feignclient together so that we can manage them gracefully?
Or there is only way to use them redundancy?
THANKS!
Feign doesn't know about #ComponentScan.
Use #EnableFeignClients(basePackages = {"com.example.common.agent","com.example.client.controller"})
Solution using Configuration
In case you use Swagger Codegen, you can use a configuration to bootstrap the apis:
#Configuration
public class ParkingPlusFeignClientConfiguration {
#Autowired
private ParkingPlusProperties properties;
#Bean
public ServicoPagamentoTicket2Api ticketApi() {
ApiClient client = new ApiClient();
// https://stackoverflow.com/questions/42751269/feign-logging-not-working/59651045#59651045
client.getFeignBuilder().logLevel(properties.getClientLogLevel());
client.setBasePath(properties.getHost());
// Generated from swagger: https://demonstracao.parkingplus.com.br/servicos
return client.buildClient(ServicoPagamentoTicket2Api.class);
}
}

How to load springboot properties?

I'm trying to load properties once in my springboot application.
Actually, I have created a class to do that :
#Configuration
#PropertySource(value = { "classpath:parameters.properties", "classpath:iot.properties" })
public class PropertiesHelper {
#Autowired
protected Environment env;
private static Environment properties;
#PostConstruct
public void init() {
properties = env;
}
public static String getProperty(final String propertyName) {
return properties.getProperty(propertyName);
}
}
This class work fine but this is not clean code (sonar hate my static variable).
So, how can I load in my springboot application all my properties correctly and only once ?
#PropertySource("classpath:parameters.properties")
#PropertySource("classpath:iot.properties")
public class PropertiesHelper {
#Value( "${Value.you.need}" )
private String valueYouNeed;
}
parameters.properties
Value.you.need=12345
Make it something like this, it should be work in your scenario.
An elegant solution
If you just want to eliminate the sonar alarm(sonar hate my static variable)
// application.properties
spring.profiles.active=test1010
// IOC config bean container
#Configuration
#Data
public class PropertiesConfig{
#Value("${spring.profiles.active:preline}")
private String profiles;
}
//Use
#Autowired
private PropertiesConfig propertiesConfig;
#GetMapping("/getPropertiesConfig")
public String getPropertiesConfig(){
return propertiesConfig.getProfiles();
}
I think the above scheme is a more elegant way~,Do you have a better solution?

Injecting Configuration Properties is not working in my test

I'm stuck! If I skip tests and deploy to tomcat auto wiring the configuration properties file works. In my test, it fails! I'm not sure what I'm missing.
Here is my setup:
Spring Boot v 1.2.5.RELEASE
Application.yml
git:
localRepo: './powershell-status-scripts/'
remoteRepo: 'https://github.com/...'
RepositoryProperties this class has getters and setters for the properties
#Configuration
#ConfigurationProperties(locations = "classpath:application.yml", prefix = "git", ignoreUnknownFields = false)
public class RepositoryProperties {
private String localRepo;
private String remoteRepo;
public RepositoryProperties() {
}
public String getLocalRepo() {
return localRepo;
}
public void setLocalRepo(String localRepo) {
this.localRepo = localRepo;
}
public String getRemoteRepo() {
return remoteRepo;
}
public void setRemoteRepo(String remoteRepo) {
this.remoteRepo = remoteRepo;
}
}
Application.java
#EnableAutoConfiguration
#EnableConfigurationProperties
#ComponentScan(basePackages = "com.sendash.admin")
#EnableJpaRepositories("com.sendash.admin.dao.jpa")
#EnableSwagger
public class Application extends SpringBootServletInitializer {
private static final Class<Application> applicationClass = Application.class;
private static final Logger log = LoggerFactory.getLogger(applicationClass);
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
}
GitService - Autowiring the properties works on tomcat!
#Service
#EnableConfigurationProperties
public class GitService {
#Autowired
private RepositoryProperties repositoryProperties;
public void updateLocalRepository() {
...
}
GitServiceTest this class fails on init because of a NPE. Properties is null.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = Application.class)
#Profile("test")
#TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
public class GitServiceTest {
#Autowired
private static GitService manager;
#Autowired
private static RepositoryProperties properties;
private static final String localRepoLocation = properties.getLocalRepo();
I do realize after pasting this that #EnableConfigurationProperties is on both the Application.java and the GitService.java class. Stopping the duplication does not fix the problem.
If you want to use Spring Boot in your tests, you should configure the tests accordingly. To do that, remove the ContextConfiguration and add the following:
#SpringApplicationConfiguration(classes = Application.class, initializers = ConfigFileApplicationContextInitializer.class)
This should enable injecting the configuration properties.
I did change my ContextConfiguration as suggested, but my main problem was trying to autowire a static field. It was static for #BeforeClass test setup logic so I needed to move things around a bit, but I got it working. Thanks for the suggestion.

Resources