Win a copy of Head First Go this week in the Go forum!

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,
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
17 hours 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

2 weeks 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 weeks 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 weeks 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 weeks 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 ?
1 month 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
1 month ago
Hi, Tim.
That is true. I tried to create a @Service bean with two constructors. I annotated both with @Autowired.
IntelliJ won't compile it because only one constructor can have @Autowired annotation.

The Spring document does not say we cannot annotate more than one constructor with @Autowired.
1 month ago
Hi,
If there are two constructors annotated with @Autowired, which one the container will use to inject beans to the constructor?
According to the document:


As of Spring Framework 4.3, an @Autowired annotation on such a constructor is no longer necessary if the target bean only defines one constructor to begin with. However, if several constructors are available, at least one must be annotated to teach the container which one to use.



The document does not say which constructor to use in order to inject beans by constructor.

Thanks in advanced.

Reference: https://docs.spring.io/spring/docs/4.3.22.RELEASE/spring-framework-reference/htmlsingle/#beans-autowired-annotation
1 month ago
Hi,
I am running cloudera vm quickstarts 5.13.
In Hue browser in my cloudera virtual machine, I created a table with a column family, cf and then used bulk upload to upload a csv file.

But I got an error "API error: TSocket reads 0 bytes'.

I have already used "sudo service hbase-thrift restart " and sudo jps shows that ThriftServer is running.

The hue log file says:


8/Feb/2019 19:34:15 -0800] thrift_util  INFO     Thrift exception; retrying: TSocket read 0 bytes
[18/Feb/2019 19:34:16 -0800] thrift_util  INFO     Thrift exception; retrying: TSocket read 0 bytes
[18/Feb/2019 19:34:16 -0800] thrift_util  WARNING  Out of retries for thrift call: mutateRows
[18/Feb/2019 19:34:16 -0800] thrift_util  INFO     Thrift saw a transport exception: TSocket read 0 bytes
[18/Feb/2019 19:34:16 -0800] exceptions_renderable ERROR    Potential trace: [('/usr/lib/hue/apps/hbase/src/hbase/api.py', 46, 'query', 'return getattr(self, action)(*args)'), ('/usr/lib/hue/apps/hbase/src/hbase/api.py', 253, 'bulkUpload', 'client.mutateRows(tableName, batches, None, doas=self.user.username)'), ('/usr/lib/hue/desktop/core/src/desktop/lib/thrift_util.py', 389, 'wrapper', 'raise StructuredThriftTransportException(e, error_code=502)')]
[18/Feb/2019 19:34:16 -0800] middleware   INFO     Processing exception: Api Error: TSocket read 0 bytes: Traceback (most recent call last):
 File "/usr/lib/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/core/handlers/base.py", line 112, in get_response
   response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "/usr/lib/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/transaction.py", line 371, in inner
   return func(*args, **kwargs)
 File "/usr/lib/hue/apps/hbase/src/hbase/views.py", line 79, in api_router
   return api_dump(HbaseApi(request.user).query(*url_params))
 File "/usr/lib/hue/apps/hbase/src/hbase/api.py", line 54, in query
   raise PopupException(_("Api Error: %s") % error_msg)
PopupException: Api Error: TSocket read 0 bytes



Please advice.
1 month ago
Hi, Bora.
What is the "exam period" you are refering to? I am still looking for some official mock exams for this Spring framework exam.
By the way, I think you should also read Spring in Action 4th edition's core spring framework. I heard Spring framework's core does not change much from Spring 4 to Spring 5.
You may also  need to search for more information about Spring unit test.

I highly recommended Getting Started with Spring Framework 4th edition. It is a very good starting point.
I guess I cannot access to the spring boot's embedded HSQL database directly through HySQL database engine.
I can only view the data from this embedded database through the Thymleaf pages in the spring boot project in the same virtual machine.

In a similar situation , I use H2 embedded DB, I can only access it through localhost:<port number>/h2-console. I cannot even access it through the DB connection in IntelliJ.

1 month ago
Hi Robotas,
Congratulations. I am working on this spring 5 exam too.

For Bora,
I first quickly went through a book called Getting started with Spring Framework: cover Spring 5 4th edition. But this book has too much technical information and less theories.
But this book is a very good start.

I am reading Spring in Action 5th edition now.  But this edition covers a lot less Spring core concepts. I just ordered Spring in Action 4th edition to learn more about Spring Core and AOP.  
Spring in Action explains everything well.

I am also talking the Spring Frame guru course from Udemy now.

Also, Mr. Ivan Krizan will complete his study guide and you can buy it at https://leanpub.com/corespring5certificationindetail.

Hi, I am running  the spring-petclinic code here at https://github.com/spring-projects/spring-petclinic
This application retrieves pets, owner, vets ... data from an embedded HSQL database.
There is an application.properties file with these config without specifying any username or password:


database = hsqldb
spring.datasource.schema=classpath*:db/${database}/schema.sql
spring.datasource.data=classpath*:db/${database}/data.sql
...



The pom.xml :


<!-- Databases - Uses HSQL by default -->
   <dependency>
     <groupId>org.hsqldb</groupId>
     <artifactId>hsqldb</artifactId>
     <scope>runtime</scope>
   </dependency>
   <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <scope>runtime</scope>
</dependency>
....



I am sure the HSQL database is embedded in this Spring boot application. But I don't know how to access it.
The author said I am not supposed to ask questions from Github. That is why I ask this question here.
1 month ago
Hi,
I tried this select count (*) as SQL_CALC_FOUND_ROWS from ..... ,
but I got this :


check the manual that corresponds to your MYSQL server version for the right syntax to use near SQL_CALC_FOUND_ROWS...."



If I use select SQL_CALC_FOUND_ROWS * from ... , it works in MySQL.

Reference:
https://cnedelcu.blogspot.com/2008/11/mysql-using-sqlcalcfoundrows-and.html