This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!

Himai Minh

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

Recent posts by Himai Minh

Hi, Labzar,
Is there any Spring Practice Exam desktop version?
The one you shared with us is on Android.
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?
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:

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 :
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:
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:
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 :

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
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.

3 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

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.
4 months ago
Hi, Jain,
My toy code is

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?
4 months ago
Hi, I configure two beans in a class annotated with @Configuration:
   public HelloWorldService helloWorldEnglish(){

   public HelloWorldService helloWorldServiceSpanish(){
My define = 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 ?
4 months ago