I'm using ribbon and eureka and I want add the following property:
my-service.ribbon.ServerListRefreshInterval
However, the default context doesn't see this property. I'm using ribbon as a loadBalancer. Currently I fixed it by using:
ConfigurationManager.loadPropertiesFromResources("my-service.properties");
Is this the only way to fix this?
Example of my code:
#Component
public class ServiceClientFactory
{
#Inject
public ServiceClientFactory(
private String applicationName="firstApp";
final SpanAccessor spanAccessor
final LoadBalancerClient loadBalancer,
) throws IOException {
loadBalancer.choose(applicationName);
}
}
#SpringBootApplication
#EnableEurekaClient
public class ServiceApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
new ServiceApplication()
.configure(new SpringApplicationBuilder(ServiceApplication.class))
.run(args);
}
}
Related
I want to new a project like mybatis-spring-boot-starter, I use springboot 2.3.2, but something wrong.
First of all, I define a BatisProperties.java like following:
#ConfigurationProperties(prefix = BatisProperties.MYBATIS_PREFIX)
public class BatisProperties {
public static final String MYBATIS_PREFIX = "batis";
private String MyClassName;
...
}
then, a BatisAutoConfiguration.java
#Configuration
#ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
#EnableConfigurationProperties(BatisProperties.class)
public class BatisAutoConfiguration implements InitializingBean {
private final BatisProperties properties;
#Override
public void afterPropertiesSet() throws Exception {
checkConfigFileExists();
}
private void checkConfigFileExists() {
System.out.println(this.properties.getMyClassName());//null here
if (this.properties.isCheckConfigLocation() && //code from mybatis-spring-booot-starter
StringUtils.hasText(this.properties.getConfigLocation())) {
Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
Assert.state(resource.exists(),
"Cannot find config location: " + resource + " (please add config file or check your Mybatis configuration)");
}
}
And in /resource/META_INF/spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
xxx.BatisAutoConfiguration
above are all in a starter, I compile it to a jar file and use this jar file in another project(project TWO), in project TWO, I define .properties or .yml in /resource directory, contents are:
batis.my-class-name=xxxx.xxxx
Finally, a DemoApplication.java or Test.java like following:
DemoApplication.java
#SpringBootApplication
public class DemoApplication {
#Autowired
private BatisProperties properties;
private static BatisProperties batisProperties;
#PostConstruct
public void init() {
batisProperties = properties;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println(batisProperties.toString());//xxxx.BatisProperties#436390f4
System.out.println(batisProperties.getMyClassName());//null
}
}
Test.java
#SpringBootTest
class Test {
#Autowired
private BatisProperties properties;
#Test
void contextLoads() {
System.out.println(properties);// some bean in Spring IOC
System.out.println(properties.getDatasourceConfigProviderClassName());// null
}
}
Above comments are the results: We can find BatisProperties Bean in Spring IOC, but all properties are null.
So anybody can help? I don't know whether it is caused by the version of SpringBoot
I would improve the BatisProperties class to be as:
#Configuration
#ConfigurationProperties(prefix = "batis")
public class BatisProperties {
private String MyClassName;
...
// Make sure you have getters and setter for properties!
// If you use Lombok, put #Data on this class.
...
}
There is no need to explicitly have BatisAutoConfiguration.java. Just let Spring initialize it for you. Completely remove the class BatisAutoConfiguration.java. Also do not use static fields for the property class.
Try the following:
#SpringBootApplication
#EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Or also more explicit, but not needed:
#SpringBootApplication
#ComponentScan(basePackages = {"my.app.org"}) // prefix, where BatisProperties is
#EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
or:
#SpringBootApplication
#EnableAutoConfiguration
#Import(BatisProperties.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The BatisProperties should be available anywhere in your App.
I have a small vertx application with an AppLauncher class that extend of VertxCommandLauncher and I set a appConfig.json with the typical config parameters :
public class AppLauncher extends VertxCommandLauncher implements VertxLifecycleHooks {
public static void main(String[] args) {
new AppLauncher().dispatch(args);
}
#Override
public void afterConfigParsed(JsonObject config) {
AppConfig.INSTANCE.setConfig(config);
}
To run my application in my IDE I put in edit configuration my main class (Applauncher.java) and the arguments :
run io.vertx.covid.verticle.MainVerticle -conf../vertx-application/src/main/resources/appConfig.json
This is my test class:
#BeforeAll
static void deployVerticles(Vertx vertx, VertxTestContext testContext) {
vertx.deployVerticle(BaseVerticle.class.getName(),testContext
.succeeding(id->testContext.completeNow()));
}
This is my BaseVerticle class that all my verticles extends from:
public abstract class BaseVerticle extends AbstractVerticle {
public static String CONTEXT_PATH = AppConfig.INSTANCE.getConfig().getString(Constants.CONTEXT_PATH);
}
And this is my AppConfig class :
public enum AppConfig {
INSTANCE;
private JsonObject config;
public JsonObject getConfig() {
return config;
}
public void setConfig(JsonObject config) {
this.config = config;
}
}
Everything works, but if I would like to test it in a separete way then I deploy my verticles but I have a Nullpointer in the CONTEXT_PATH (BaseVerticle class) because the config (suppose to be taken from appConfig.json) is null.
I haven't found a way to pass the arguments with my appConfig.json or should I call to the main method passing the arguments?
I like to do something that is similar to profiles in my vertx application.
If you set an environment variable with the key vertx-config-path before the vertx instance is initialized, you can control where vertx's config retriever (you might need to add vert-config to your gradle/maven dependencies) gets the configuration from.
In your launcher, you can do something like the following, which will give you the ability to add profile based config files to your resources folder conf/config-%s.json where %s is the profile name:
public class CustomLauncher extends Launcher {
public static final String ACTIVE_PROFILE_PROPERTY = "APP_ACTIVE_PROFILE";
private static final CLI cli = CLI.create("main")
.addOption(new Option()
.setShortName("p")
.setLongName("profile")
);
public static void main(String[] args) {
initDefaults(Arrays.asList(args));
new CustomLauncher().dispatch(args);
}
public static void executeCommand(String cmd, String... args) {
initDefaults(Arrays.asList(args));
new CustomLauncher().execute(cmd, args);
}
public static void initDefaults(List<String> args) {
System.setProperty(LoggerFactory.LOGGER_DELEGATE_FACTORY_CLASS_NAME, SLF4JLogDelegateFactory.class.getName());
CommandLine parse = cli.parse(args);
String profile = parse.getOptionValue("p");
if (profile != null && !profile.isEmpty()) {
System.setProperty(ACTIVE_PROFILE_PROPERTY, profile);
System.setProperty("vertx-config-path", String.format("conf/config-%s.json", profile));
}
}
}
Then in your test, instead of relaying on vertx test extension to inject vertx for you, you can initialize it by yourself and control the profile (aka which config file to load) like the following:
private static Vertx vertx;
#BeforeAll
public static void deployVerticles(VertxTestContext testContext) {
CustomLauncher.initDefaults(Arrays.asList("--profile", "test"))
vertx = Vertx.vertx();
ConfigRetriever.create(vertx).getConfig(asyncResult -> {
if (asyncResult.succeeded()) {
JsonObject config = asyncResult.result();
DeploymentOptions deploymentOptions = new DeploymentOptions()
.setConfig(config);
vertx.deployVerticle(BaseVerticle.class.getName(), deploymentOptions);
} else {
// handle failure
}
});
}
Then when you run your application, instead of providing -conf, you can use -p or --profile
I also highly recommend to get familiar with vertx-config as you can also get env variables, k8s config maps, and much more.
EDIT: I also highly recommend to move to Kotlin if possible, makes the async-code much easier to handle in an imperative way (with Coroutines). It's very hard to deal with libraries like Vert.x in Java compared to languages like Kotlin.
I solved my problem creating a verticle with the config stuffs (vertx-config documentation), here is my verticle config class:
public class ConfigVerticle extends AbstractVerticle {
protected static Logger logger = LoggerFactory.getLogger(ConfigVerticle.class);
public static JsonObject config;
#Override
public void start() throws Exception {
ConfigStoreOptions fileStore = new ConfigStoreOptions()
.setType("file")
.setOptional(true)
.setConfig(new JsonObject().put("path", "conf/appConfig.json"));
ConfigStoreOptions sysPropsStore = new ConfigStoreOptions().setType("sys");
ConfigRetrieverOptions options = new ConfigRetrieverOptions().addStore(fileStore).addStore(sysPropsStore);
ConfigRetriever retriever = ConfigRetriever.create(vertx, options);
retriever.getConfig(ar -> {
if (ar.failed()) {
logger.info("Failed to retrieve config from appConfig.json");
} else {
config = ar.result();
vertx.deployVerticle(MainVerticle.class.getName(), new DeploymentOptions().setConfig(config));
}
});
}
}
And my MainVerticle.class I pass the new configuration like this:
public class MainVerticle extends AbstractVerticle {
#Override
public void start(){
vertx.deployVerticle(BackendVerticle.class.getName(), new DeploymentOptions().setConfig(config()));
}
}
Then, my simpleTests :
#ExtendWith(VertxExtension.class)
public class BaseCovidTest {
protected WebClient webClient;
#BeforeEach
void initWebClient(Vertx vertx){
webClient = WebClient.create(vertx);
}
#BeforeAll
static void deployVerticles(Vertx vertx, VertxTestContext vertxTestContext) {
vertx.deployVerticle(ConfigVerticle.class.getName() ,vertxTestContext
.succeeding(id-> {
try {
vertxTestContext.awaitCompletion(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
vertxTestContext.completeNow();
}));
}
}
And everything works, thanks #Tom that inspired me to fix it!
I just created a really basic spring boot application using spring initializer and am trying things out. I want to load a list from a yaml configuration file, but it always returns empty.
I have a custom configuration class
#ConfigurationProperties("example-unit")
#EnableConfigurationProperties
public class ConfigurationUnit {
public List<String> confiList = new ArrayList<>();
public List<String> getConfiList() {
return this.confiList;
}
}
And my main class looks like this
#SpringBootApplication
public class DemoApplication {
static ConfigurationUnit configurationUnit = new ConfigurationUnit();
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
List<String> hello = configurationUnit.getConfiList();
System.out.print("");
}
}
I have put the application.yaml into resources folder.
example-unit:
- string1
- string2
- hello22
I searched here and online, but can't figure out what's the issue and nothing I changed helped. I know I must be doing something wrong.
This statement is wrong static ConfigurationUnit configurationUnit = new ConfigurationUnit();
you should not create the object
Spring only injects the properties into the beans that are handled by application context, and spring creates beans of classes that are annotated with # Configuration
ConfigurationUnit
#Configuration
#ConfigurationProperties("example-unit")
public class ConfigurationUnit {
public List<String> confiList;
public List<String> getConfiList() {
return this.confiList;
}
}
DemoApplication In the spring boot main get the bean from applicationcontext and from it get the list object
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
ConfigurationUnit unit = context.getBean("configurationUnit"):
System.out.print(unit. getConfiList());
}
}
Put your list under prefix.property. In your case example-unit.confi-list:. Usually provide a setter for your property: setConfiList(List<String> strings). But since you already initialized it as empty Array list this setter is obsolete says this. There is also advice to add Enable-annotation to Application class:
Application class should have #EnableConfigurationProperties annotation
Here is the reference on how Spring Bboot Configurtion Binding works.
Specifically for your question, this is an example of app that achives your goal:
application.yml
example-unit:
confiList:
- string1
- string2
- hello22
sources
#SpringBootApplication
#EnableConfigurationProperties(ConfigurationUnit.class)
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
ConfigurationUnit configurationUnit = context.getBean(ConfigurationUnit.class);
System.out.println(configurationUnit.getConfiList());
}
}
#ConfigurationProperties("example-unit")
public class ConfigurationUnit {
public List<String> confiList = new ArrayList<>();
public List<String> getConfiList() {
return this.confiList;
}
}
Here is an example :
Application.yml:
example-unit: string1,string2,hello22
ConfigurationUnit.class:
#Component
#PropertySource(value="classpath:application.yml")
public class ConfigurationUnit {
#Value("#{'${example-unit}'.split(',')}")
private List<String> confiList;
public List<String> getConfiList() {
return confiList;
}
}
DemoFileLoadApplication.class:
#SpringBootApplication
public class DemoFileLoadApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoFileLoadApplication.class, args);
ConfigurationUnit configurationUnit = context.getBean(ConfigurationUnit.class);
System.out.println(configurationUnit.getConfiList());
}
}
Output:
[string1, string2, hello22]
Before we start, yes I know there's another question out there but they are not the same issue and I couldn't find anything to solve this.
Ok, I have
package a
imports ...
#SpringBootApplication
public class LauncherApplication implements CommandLineRunner {
#Autowired
private SubListener subListener;
#Value("${proj.id}")
private String testy;
public static void main(String[] args) {
SpringApplication.run(LauncherApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
System.out.println(subListener.getProjID());
System.out.println(testy);
}
}
Then on my subListener
package a.b
imports ...
#Component
public class SubListener {
#Value("${proj.id}")
private String projID;
public String getProjID() {
return projID;
}
}
and finally inside my resources folder on application.properties
proj.id=Hello from file
Now, by all accounts this should work, the SpringBootApplication has the component scan thing, the bean is marked as #component and is a subpackage of the springbootapplication, the properties file has the default name and in the default directory. I can't find a single reason for this not to work. Also mind you that when I call the property on testy it works, it only return null when it's returning from the sublistener.
Thank you for your time
EDIT:
New launcherApplication
#SpringBootApplication
public class LauncherApplication {
public static void main(String[] args) {
SpringApplication.run(LauncherApplication.class, args);
}
#Bean
public CommandLineRunner runner(SubListener subListener){
CommandLineRunner runner = new CommandLineRunner() {
#Override
public void run(String... args) throws Exception {
System.out.println(subListener.getProjID());
}
};
return runner;
}
}
It still returns null though
My only guess would be that the #Value annotation inside your SubListener class is from the wrong package. Can you please check that you are using this import and not something else:
import org.springframework.beans.factory.annotation.Value;
I've copied your code and it's working for me. If you still can't get it working then I'd recommend trying to reproduce it in a new empty project.
i create simple spring project and i need to use annotation #Autowired but when i run project, i get exception NullPointerException.
This is my classes:
Main.java
public class Main {
#Autowired
private static InjectClass injectClass;
public static void setInjectClass(InjectClass injectClass) {
Main.injectClass = injectClass;
}
public static void main(String[] args) {
injectClass.hello(); //NullPointerException
}
}
ConfigurationBean
#Configuration
public class ConfigurationBean {
#Bean
public InjectClass injectClass(){
return new InjectClass();
}
}
InjectClass
public class InjectClass {
public void hello(){
System.out.println("Autowired success!");
}
}
You need to initiate application contex before using any bean.
You can do it by writing following code in starting of your main method.
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ConfigurationBean.class);