Himai Minh

Ranch Hand
+ Follow
since Jul 29, 2012
Cows and Likes
Cows
Total received
12
In last 30 days
0
Total given
0
Likes
Total received
68
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Himai Minh

Hi,
From the Pivotal's study guide of Spring 5 professional certification exam, I don't see it mention Spring WebFlux or reactive programming which are new to Spring 5.
Are they on the exam?
Just want to confirm with some of you who took the exam.
Hi, Dominik,
I just bought your tutorial module 1 and I am studying on it  now
I can't wait to see module 2 - 8 available. Would you mind posting them soon?
Thanks.
Hi, I came across two examples from Spring in Action and Getting Started with Spring Framework.
One example uses new ClassPathXmlApplicationContext ("META-INFO/spring/knight.xml") and the other one uses new ClassPathXmlApplication("classpath:META-INFO/spring/applicationContext.xml")
Both knight.xml and applicationContext.xml are in resources/META-INFO/spring.
Both example works
I assume classpath prefix is always needed as the xml config file is under resources/META-INFO folder.
But why the classpath is needed in one example, but not the other?
The examples are here:
https://github.com/habuma/spring-in-action-4-samples/blob/master/Chapter_01/knight/src/main/java/sia/knights/KnightMain.java
https://github.com/getting-started-with-spring/4thEdition/blob/master/chapter%206/ch06-bean-profiles/src/main/java/sample/spring/chapter06/bankapp/BankAppWithProfile.java

Thanks in advance.
1 month ago
Hi, I find a fix to my code, but I don't think my fix is perfect. If anyone has a better solution, please let me know.
In the applicationContext.xml, I add the viewResolver:


This is my bankapp-servlet.xml


I committed my code there :
https://github.com/aCodeRancher/4thEdition/tree/my-bank-app/chapter%2012/ch12-bankapp
2 months ago
Hi, I am trying to replace web.xml in ch12-bankapp example of Getting Started With SpringFramework 4th edition.
This is the web.xml provided in the book:


I converted it into Java code:



This is the Tomcat 9 log :


I cannot get any result from the browser.
If anyone knows, please help. Thanks.
This is my detailed code in GitHub:
https://github.com/aCodeRancher/4thEdition/tree/master/chapter%2012/ch12-bankapp
2 months ago
I don't know why I need to use AbstractAnnotationConfigDispatcherServletInitializer and a subclass of a deprecated class, WebMvcConfigurerAdapter to make it work.
But I don't want to use the deprecated class.
My code is here:
https://github.com/aCodeRancher/4thEdition/tree/myfeature_1/chapter%2012/ch12-bankapp/src/main/java/sample/spring/chapter12/config
2 months ago
Hi, I am trying to replace the web.xml by WebApplicationInitializer in chapter 12's bankapp of Getting Started with Spring Framework 5 , 4th edition.
But I got an error from Tomcat 9:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.



What I added are:





The detailed code is here :
https://github.com/aCodeRancher/4thEdition/tree/myfeature/chapter%2012/ch12-bankapp


If anyone has suggestion to fix it, please help. Thanks in advanced.
2 months ago
Hi, I tried it.
I have two overloading methods annotated with @Bean. They have different @Profile. To my surprise, the second bean in the file is autowired, not the first one.
2 months ago
Hi,
According to the Spring 5 documentation section 1.13.1


With @Profile on @Bean methods, a special scenario may apply: In the case of overloaded @Bean methods of the same Java method name (analogous to constructor overloading), an @Profile condition needs to be consistently declared on all overloaded methods.
If the conditions are inconsistent, only the condition on the first declaration among the overloaded methods will matter.


If methods are overloaded, that means the methods return beans of the same type. The container does not know which bean to inject or autowire in this situation even if the active profile is set.
I think using @Primary on one of the bean methods can tell the container which bean to inject. The order of methods' declaration does not matter as the document describes.

Reference:
https://docs.spring.io/spring/docs/5.0.3.RELEASE/spring-framework-reference/core.html#beans-definition-profiles
2 months ago
I add one more test case with two beans with @Profile ("english"). And in this case, I have to set one of them as @Primary; otherwise, the container does not know which one to inject.

It seems like in any case when the container cannot determine which bean to inject, we have to use @Primary.
The source code is committed here https://github.com/aCodeRancher/diexample/tree/advanced-autowire/src

3 months ago
Hi, Jain. Thanks for your suggestion.
It seems to me that we can draw these conclusion:
1. With beans of different name, same type , use @Primary and @Profile
2. With beans of the same name, same type, omit @Primary, @Profile is good enough.
3 months ago
Hi everyone,
I have a gut's feeling that @Profile is only used for the method/class when a particular profile is set to be active. It has nothing to do with the bean.
For example,

When the active profile is english, then the first method is chosen.

But since the method registers a bean, that is a different story. The container needs to choose one of them. In this particular case, both beans have the same priority. The container will choose either one depends on the active profile.


Any other comments ?  Correct me if I am wrong.
Thanks.
3 months ago
Hi, Jain,
My toy code is https://github.com/aCodeRancher/diexample/tree/advanced-autowire









So, in this case, the english profile is active. The container can simply choose the @Profile("english") bean. But it does not work because there are two helloWorldService beans. I have to set at least one of them @Primary.
My question is if the bean has been annotated with @Profile ("english"), why the container still does not know which bean to choose?
3 months ago
Hi, I configure two beans in a class annotated with @Configuration:
   
   @Bean
   @Profile("english")
   
   public HelloWorldService helloWorldEnglish(){
       ....
   }

   @Bean
   @Profile("spanish")
   @Primary
   public HelloWorldService helloWorldServiceSpanish(){
     ...
   }
My application.properties define spring.profiles.active = english
But it still complains that more than one bean is found.
I have to put @Primary to the helloWorldEnglish bean to make it work.
My question is why should I put @Primary to autowire the right bean even though my profile has been set to english ?
3 months ago
I used Spring boot to demo my example. I guess it is actually the Spring boot does not let me declare two constructors with @Autowired.
But according to the specification about Autowired:


...If multiple non-required constructors declare the annotation, they will be considered as candidates for autowiring.



But still, if two constructors are annotated with @Autowired, the container does not really know which constructor should use.

Reference:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html
3 months ago