I am working on a Micronaut project, where I would like to see if the environment variables from the application.yml are being correctly assigned using the #Value annotation, when the app starts locally.
But every time the app is starting it shows me that the variables are not being assigned to the environment variables from the application.yml file.
That is my code:
public class Application {
private static String localTestString = "I am the local String";
#Value("${aws.secretkeyid}")
public static String applicationYmlTestString;
#Value("${aws.keyid}")
private static int keyId;
public static void main(String[] args) {
Micronaut.run(Application.class);
}
static{
log.warn("Local Test String is: " + localTestString);
log.warn("Application Yml Test String is: " + applicationYmlTestString);
log.warn("Key ID: " + keyId);
}
}
This is my application.yml
aws:
keyid: 123
secretkeyid: "abcdesdasdsddddd"
region: "europe-1"
Output:
Local Test String is: I am the local String
Application Yml Test String is: null
Key ID: 0
As we see the two variables applicationYmlTestString and keyId are not being assigned to the environment variables. Is there a way to solve this problem and to get:
Application Yml Test String is: abcdesdasdsddddd
Key ID: 123
Thank you in advance!
There are two issues with the example you have shown. Firstly, Micronaut does not inject values to static fields annotated with #Value annotation. (It's not weird, Spring does not support it as well.) Secondly, after injecting values to non-static fields, you won't be able to read their values using the class' static constructor. The whole application context must be ready to read such values, so you need to use an event listener that reacts to the application startup event.
Here is the simplest way to achieve it based on your example:
package micronaut.hello.world;
import io.micronaut.context.annotation.Value;
import io.micronaut.context.event.StartupEvent;
import io.micronaut.runtime.Micronaut;
import io.micronaut.runtime.event.annotation.EventListener;
import jakarta.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
#Singleton
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
#Value("${aws.secretkeyid}")
private String applicationYmlTestString;
#Value("${aws.keyid}")
private int keyId;
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
#EventListener
void onStartup(StartupEvent event) {
log.warn("Application Yml Test String is: " + applicationYmlTestString);
log.warn("Key ID: " + keyId);
}
public String getApplicationYmlTestString() {
return applicationYmlTestString;
}
public void setApplicationYmlTestString(String applicationYmlTestString) {
this.applicationYmlTestString = applicationYmlTestString;
}
public int getKeyId() {
return keyId;
}
public void setKeyId(int keyId) {
this.keyId = keyId;
}
}
There are three things worth mentioning:
The above example uses #EventListener annotation that makes the given method "event-aware", and this method will be triggered when the specific event is published by the application (or framework.)
We react to io.micronaut.context.event.StartupEvent - an event fired once startup is complete.
Keep in mind that to make this #EventListener annotation work, we need to annotate the application class with #Singleton to make this class a proper Micronaut bean.
Alternatively, if making an application class a singleton bean does not look good to you, you can implement the ApplicationEventListener interface and create a dedicated bean that will react to the same startup event. In this example, I use a static inner class, but that's just to make this example simple:
package micronaut.hello.world;
import io.micronaut.context.annotation.Value;
import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.context.event.StartupEvent;
import io.micronaut.runtime.Micronaut;
import jakarta.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
#Singleton
static class OnStartupEventListener implements ApplicationEventListener<StartupEvent> {
#Value("${aws.secretkeyid}")
private String applicationYmlTestString;
#Value("${aws.keyid}")
private int keyId;
#Override
public void onApplicationEvent(StartupEvent event) {
log.warn("Application Yml Test String is: " + applicationYmlTestString);
log.warn("Key ID: " + keyId);
}
public String getApplicationYmlTestString() {
return applicationYmlTestString;
}
public void setApplicationYmlTestString(String applicationYmlTestString) {
this.applicationYmlTestString = applicationYmlTestString;
}
public int getKeyId() {
return keyId;
}
public void setKeyId(int keyId) {
this.keyId = keyId;
}
}
}
But eventually, you should consider implementing a configuration class and use it instead of injecting values with the #Value annotation. However, whatever option you choose, the same thing applies - the configuration class can be injected to a non-static field and can be checked using an event listener mechanism.
And as Tim mentioned in the comment below, "Be careful logging environment variables though... They have a habit of being secrets, and logging them out tends to end up with them being in plain text in loads of different systems 😉". If you really need to log such information to double-check if the expected configuration is injected, try doing it in the controlled dev environment only. Assuming that you use the dev profile for the local env, you could use #Requires annotation to limit specific event listener to only that dev environment:
#Singleton
#Requires(env = "dev")
class OnStartupEventListener implements ApplicationEventListener<StartupEvent> {
#Value("${aws.secretkeyid}")
private String applicationYmlTestString;
#Value("${aws.keyid}")
private int keyId;
#Override
public void onApplicationEvent(StartupEvent event) {
log.warn("Application Yml Test String is: " + applicationYmlTestString);
log.warn("Key ID: " + keyId);
}
public String getApplicationYmlTestString() {
return applicationYmlTestString;
}
public void setApplicationYmlTestString(String applicationYmlTestString) {
this.applicationYmlTestString = applicationYmlTestString;
}
public int getKeyId() {
return keyId;
}
public void setKeyId(int keyId) {
this.keyId = keyId;
}
}
Related
I am using TestContainer to run RabbitMQ instance in order to use it in my integration tests.
I create a Junit 5 extension class that implement the BeforeAllCallback interface to run the container only once before my tests, to connect to the container i need to retrieve the mapped port that is exposed in my host machine, so i am wandering if there is any solution in order to access the extension class field from my integration test class.
The Extension
public class RmqExtension implements BeforeAllCallback {
private static final String DEFAULT_USER = "guest";
private static final String DEFAULT_PASS = "guest";
public static final int RABBIT_HTTP_API_PORT = 15672;
private static final String RABBIT_MQ_IMAGE_NAME = "rmqImage";
private static final String RABBIT_MQ_OVERVIEW_PATH = "/api/overview";
private static final GenericContainer rabbitMqContainer = new GenericContainer(DockerImageName.parse(RABBIT_MQ_IMAGE_NAME))
.withExposedPorts(RABBIT_HTTP_API_PORT)
.waitingFor(Wait.forHttp(RABBIT_MQ_OVERVIEW_PATH).withBasicCredentials(DEFAULT_USER, DEFAULT_PASS).forStatusCode(HttpStatus.SC_OK));
#Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
rabbitMqContainer.start();
}
}
My test Class
#ExtendWith(RmqExtension.class)
class RabbitMqIT {
private int myPort;
#Test
void myTest(){
// What i need to do
myPort = rabbitMqContainer.getMappedPort(15672);
}
}
I am unsure what is the most elegant JUnit-Jupiter-idiomatic way to do this, but if there is only 1 instance of the container per JVM process, you could either use a public static field or save it System Properties.
Also, see the Singleton Container Pattern for another example of how to do this without JUnit:
https://www.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers
This is the first time for me working with Quarkus and CDI and I think I'm getting it wrong.
In my application users can download some files. I'd like to control how many files are being downloaded simultaneously. I thought I could do this by creating a bean with #ApplicationScoped annotation, forcing instantiation at startup, and then injecting it wherever I need to know how many files are currently downloading.
This is what I managed to do:
#ApplicationScoped
public class DownloadState {
private int downloadingFiles;
void startup(#Observes StartupEvent event) {
setDownloadingFiles(0);
System.out.println("downloading files: " + downloadingFiles);
}
public int getDownloadingFiles() {
return downloadingFiles;
}
public void setDownloadingFiles(int downloadingFiles) {
this.downloadingFiles = downloadingFiles;
}
public void incrementDownloadingFiles() {
downloadingFiles++;
}
public void decrementDownloadingFiles() {
downloadingFiles--;
}
}
Doing this I can see the log at startup saying "downloading files: 0", so I know the class has been instantiated.
I try to access the number of downloading files here:
public class Downloader {
private static final Logger LOG = Logger.getLogger(Downloader.class);
#Inject
DownloadState downloadState;
private Dotenv dotenv = Dotenv.configure()
.directory("../")
.filename(".env.local")
.load();
private String fileName = dotenv.get("DOWNLOAD_PATH");
private String url;
public Downloader(String url, String fileName) {
this.url = url;
this.fileName += fileName;
}
public void downloadProduct() {
LOG.info("Downloading Files: " + downloadState.getDownloadingFiles());
//...
}
}
Whenever downloadProduct is called a NullPointerException is thrown on the line LOG.info("Downloading Files: " + downloadState.getDownloadingFiles());
Am I getting CDI totally wrong? Any help is really appreciated, thank you in advance.
I assume you're calling the Downloader constructor directly -- in which case, indeed no injection will happen and your downloadState will be null. Dependency injection is "contagious" -- you have to use it everywhere.
In your case, I'd probably just make Downloader also #ApplicationScoped, inject it everywhere you use it, and probably move the url and fileName parameters from constructor to downloadProduct. Actually at that point, the number of downloadingFiles could also be in Downloader. (Also note that it can be accessed from multiple threads -- so I'd probably use an AtomicInteger for downloadingFiles.)
All in all, something like this:
#ApplicationScoped
public class Downloader {
private static final Logger LOG = Logger.getLogger(Downloader.class);
private final AtomicInteger downloadingFiles = new AtomicInteger(0);
private final String downloadPath = Dotenv.configure()
.directory("../")
.filename(".env.local")
.load()
.get("DOWNLOAD_PATH");
public void downloadProduct(String url, String fileName) {
String path = downloadPath + fileName;
int current = downloadingFiles.incrementAndGet();
LOG.info("Downloading Files: " + current);
//...
}
}
I have a class the following class as RequestScope bean:
#RequestScope
class RequestContext {
private String requestId;
private String traceId;
private String authorisedId;
private String routeName;
// few more fields
#Inject RequestContext(SecurityContext securityContext) {
this.requestId = UUID.randomUUID().toString();
if(securityService.getAuthentication().isPresent()){
this.authorisedId = (securityService
.getAuthentication().get()).getUserId().toString();
}
}
/* to be updated in controller method interceptors */
public void updateRouteName(String name){
this.routeName = name;
}
The idea is to have an object containing the REST request level custom data accessible across the application, the scope of the this obviously should be within the current request. This can be used for say.. logging - whenever devs log anything from the application, some of the request meta data goes with it.
I am not clear what the #RequestScope bean really is:
From its definition - my assumption is it is created for every new http-request and same instance is shared for the life of that request.
when is it constructed by Micronaut ? Is it immutable ?
Across multiple requests I can see the same requestId ( expecting new UUID for every request)
Is it the right use-case for #RequestScope bean?
I was running into an issue regarding #RequestScope so I'll post an answer here for others.
I was trying to inject a #RequestScope bean into an HTTP filter, set a value in the bean, and then read it later from another bean. For example
#RequestScope
class RequestScopeBean() {
var id: Int? = null
}
#Filter
class SetRequestScopeBeanHere(
private val requestScopeBean: Provider<RequestScopeBean>
) {
override fun doFilterOnce(request: HttpRequest<*>, chain: ServerFilterChain): Publisher<MutableHttpResponse<*>> {
requestScopeBean.get().id = // id from Http Request
}
}
#Singleton
class GetRequestScopeBeanHere(
private val requestScopeBean: Provider<RequestScopeBean>
) {
fun getIdFromRequestScopeBean() {
println(requestScopeBean.get().id)
}
}
In this example before any controller is executed my filter (SetRequestScope) is called, this will set requestScopeBean.id but the key is that the request scope bean must be wrapped in a javax.inject.Provider, otherwise setting the field won't work.
Down the line, when GetRequestScopeBeanHere::getIdFromRequestScopeBean is called it'll have access to the requestScopeBean.id set earlier
This is intentional by Micronaut:
https://github.com/micronaut-projects/micronaut-core/issues/1615
when is it constructed by Micronaut ?
A #RequestScope bean is created during request processing, the first time the bean is needed.
Is it immutable ?
It could be. You get to decide if the bean is mutable or not when you write the class. As written in your example, RequestContext is mutable. If you remove the updateRouteName method, that bean would be immutable.
Is it the right use-case for #RequestScope bean?
I don't think so, but that is really an opinion based question.
EDIT: Based On Comments Added Below
See the project at https://github.com/jeffbrown/rscope.
https://github.com/jeffbrown/rscope/blob/2935a4c1fc60f350198d7d3c1dbf9a7eedd333b3/src/main/java/rscope/DemoController.java
package rscope;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
#Controller("/")
public class DemoController {
private final DemoBean demoBean;
public DemoController(DemoBean demoBean) {
this.demoBean = demoBean;
}
#Get("/doit")
public String doit() {
return String.format("Bean identity: %d", demoBean.getBeanIdentity());
}
}
https://github.com/jeffbrown/rscope/blob/2935a4c1fc60f350198d7d3c1dbf9a7eedd333b3/src/main/java/rscope/DemoBean.java
package rscope;
import io.micronaut.runtime.http.scope.RequestScope;
#RequestScope
public class DemoBean {
public DemoBean() {
}
public int getBeanIdentity() {
return System.identityHashCode(this);
}
}
https://github.com/jeffbrown/rscope/blob/2935a4c1fc60f350198d7d3c1dbf9a7eedd333b3/src/test/java/rscope/DemoControllerTest.java
package rscope;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import javax.inject.Inject;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
#MicronautTest
public class DemoControllerTest {
#Inject
#Client("/")
RxHttpClient client;
#Test
public void testIndex() throws Exception {
// these will contain the identity of the the DemoBean used to handle these requests
String firstResponse = client.toBlocking().retrieve("/doit");
String secondResponse = client.toBlocking().retrieve("/doit");
assertTrue(firstResponse.matches("^Bean identity: \\d*$"));
assertTrue(secondResponse.matches("^Bean identity: \\d*$"));
// if you modify DemoBean to be #Singleton instead of
// #RequestScope, this will fail because the same instance
// will be used for both requests
assertNotEquals(firstResponse, secondResponse);
}
}
Is there any way to get the number and some identification information of already created entities of particular Prototype-bean in Spring application?
Addition. In our project we have more then 400 prototype-beans and I would like to trace the state what beans were created during execution and the number of entities of each type.
I have found a way to see the actual picture about created prototype-beans.
I use free VisualVM memory profiler.
In the Sampler tab you can see all instances of created classes including singleton and prototype beans.
You'll see the names of your own packages and classes. In this case:
prototype is a package with my prototype-beans.
singleton is a package with my singleton-beans.
newclasses is a package with classes that I created by new operator.
Also after the garbage collector will clean up the memory you will see the result here.
you can do it by Publish and Listen Application Events.
create you own event.
when prototype bean was created send event from it.
create count ApplicationListener , and listen to income creation event.
here is example
Spring – Publish and Listen Application Events
Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, decorates and otherwise assembles a prototype object, hands it to the client and then has no further knowledge of that prototype instance.
Simple variant :
public class PrototypeCreationEvent extends ApplicationEvent {
private String beanName;
public PrototypeCreationEvent(Object source , String beanName) {
super(source);
this.beanName = beanName;
}
public String getBeanName(){
return beanName;
}
}
public class PrototypeCreationListener implements ApplicationListener<PrototypeCreationEvent> {
private ConcurrentMap<String,AtomicInteger> prototypeCreationStatistic = new ConcurrentHashMap<>();
//or from guava AtomicLongMap prototypeCreationStatistic = AtomicLongMap.create();
#Override
public void onApplicationEvent(PrototypeCreationEvent event) {
prototypeCreationStatistic.computeIfAbsent(event.getBeanName() , k->new AtomicInteger(0)).incrementAndGet();
System.out.println(event);
}
public ConcurrentMap<String,AtomicInteger> getPrototypeCreationStatistic(){
return prototypeCreationStatistic;
}
}
public abstract class PrototypeCreationPublisher implements BeanNameAware , ApplicationEventPublisherAware ,InitializingBean {
private String beanName;
private ApplicationEventPublisher applicationEventPublisher;
#Override
public void setBeanName(String name) {
this.beanName = name;
}
#Override
public void afterPropertiesSet() throws Exception {
System.out.println();
}
#Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
#PostConstruct //or use interface InitializingBean
public void sendEventAfterCreation() throws Exception {
applicationEventPublisher.publishEvent(new PrototypeCreationEvent(this , beanName));
}
}
#Component(value = BeanDefinition.SCOPE_PROTOTYPE)
public class PrototypeA extends PrototypeCreationPublisher{
}
#Component(value = BeanDefinition.SCOPE_PROTOTYPE)
public class PrototypeB extends PrototypeCreationPublisher{
}
example :
PrototypeA prototypeA1 = context.getBean(PrototypeA.class);
PrototypeA prototypeA2 = context.getBean(PrototypeA.class);
PrototypeA prototypeA3 = context.getBean(PrototypeA.class);
PrototypeB prototypeB1 = context.getBean(PrototypeB.class);
PrototypeCreationListener statistic = context.getBean(PrototypeCreationListener.class);
statistic.getPrototypeCreationStatistic().entrySet().forEach(s->{
System.out.println(s.getKey() + " count = "+s.getValue());
});
result :
PrototypeB count = 1
PrototypeA count = 3
I'm aware of that this topic might be considered as offtopic or convention/opinion based, but I have not found any other place that I could find solution for my problem.
I'm writing and Spring application, fully configured with annotations in Java. I'm loading the properties file with #PropertySource annotation:
#Configuration
#ComponentScan("myapp.app")
#PropertySource("app.properties")
public class ApplicationConfig {
#Bean
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
Let's assume, that I have app.properties file of following content:
property1=someProperty1Value
property2=someProperty2Value
I'm loading this value with following code:
#Service
public class MyServiceImpl implements MyService {
#Value("${property1}")
private String property1Value;
#Value("${property2}")
private String property2Value;
#Override
public void doStuff() {
System.out.println(property1Value);
System.out.println(property2Value);
}
}
This is working perfectly fine. On the other hand, I find it hard to maintain - if some will think that "property1" is not the best name for a property and would like to rename it, then it will be needed to find all strings "${property1}" and rename it. I tought that I could extract it to the constant class:
public final class Properties {
public static final String PROPERTY_1 = "${property1}";
public static final String PROPERTY_2 = "${property2}";
private Properties() {
}
}
This requires refactoring of the existing bindings to new constant values:
#Value(Properties.PROPERTY_1)
private String property1Value;
Looks nice, but I do not like the mess in the Properties class, I think it will be better to the constant values without bracelets:
public static final String PROPERTY_1 = "property1";
Which leads to another refactoring in the MyServiceImpl class:
#Value("${" + Properties.PROPERTY_1 + "}")
private String property1Value;
But boy, that's really ugly. I thought about extracting constant values to the Enum:
public enum Properties {
PROPERTY_1("property1"),
PROPERTY_2("property2");
private final String key;
private Properties(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public String getSpringKey() {
return "${" + getKey() + "}";
}
}
and use it like
#Value(Properties.PROPERTY_1.getSpringKey())
private String property1Value;
but then IDE reminded me, that annotation value has to be a constant.
After creating this enum, I thought that I might be over-thinking it, and it should be kept as simple as possible. Currently I came back to the solution with constants in format of
public static final String PROPERTY_1 = "${property1}";
Finally, I would like to ask you to provide another, nice-looking solution, or some reference links where I could read about some common solution.