Generation policy/Mode modification of default bean name in spring

Source: Internet
Author: User

Recently the company project intends to modular, in fact, a reason is to be able to integrate the company's multiple business code, such as a resource XXX, two business has the management of this resource, although it is a resource, but is completely different definitions, completely different tables, different processing logic. So I'm going to make the class name the same, but the package name is different.

There is a problem here, that is, when using annotations, the resources in the two modules are called Xxxdao, which will be error when Spring starts. The error is as follows conflicts with existing, non-compatible bean definition of same name and class [Xxxxdao] something, meaning that two beans have the same name, so the start error.

The solution can only let us manually modify the bean name generation strategy, I am here directly with his implementation of the class's fully qualified name (Com.abc.dao.impl.xxxDaoImpl) as the name of the bean.

Annotationbeannamegenerator is the default generation strategy for spring, let's take a look at the source code, where the Builddefaultbeanname method is the implementation of the generated name, as follows:

String shortclassname = Classutils.getshortname (Definition.getbeanclassname ());
Return Introspector.decapitalize (Shortclassname);

This default build strategy people should know how to generate, I will not elaborate.

Here Definition.getbeanclassname () is to get the fully qualified name, Classutils.getshortname is the name of the class, and the following introspector.decapitalize actually turns the initials into lowercase.

We are going to set the fully qualified name here, we can write a new class, assuming that it is called XXX, and then inherit annotationbeannamegenerator after overriding the Builddefaultbeanname method, Return to Definition.getbeanclassname () so that our generation strategy is written.

The next step is to configure it in spring. Open a. applicationcontext.xml file and find our configuration Context:component-scan this block, add a parameter name-generator= the fully qualified name of our newly written class.

By this time the default name of all beans is set, but if we explicitly write the bean ID on the class, we will still use the name of our custom bean.


Source: http://irfen.me/spring-bean-name-gener-modify/

From for notes (Wiz)

Generation policy/Mode modification of default bean name in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.