简体   繁体   中英

Spring bean alias usage

I know what bean alias means in spring. But I want to know the use cases for making use of alias. Why would somebody want to refer a bean using alias name instead of its name?

Thanks in advance.

I think the reference documentation explains it very well:

In a bean definition itself, you can supply more than one name for the bean, by using a combination of up to one name specified by the id attribute, and any number of other names in the name attribute. These names can be equivalent aliases to the same bean, and are useful for some situations, such as allowing each component in an application to refer to a common dependency by using a bean name that is specific to that component itself.

Specifying all aliases where the bean is actually defined is not always adequate, however. It is sometimes desirable to introduce an alias for a bean that is defined elsewhere. This is commonly the case in large systems where configuration is split amongst each subsystem, each subsystem having its own set of object definitions. In XML-based configuration metadata, you can use the element to accomplish this.

An specific example may be where you must define an entry point for authentication in a Single Sign On module for multiple applications. You define it in a single Spring Bean definition and you alias it in your specific application to use it as an authentication entry point.

A usage I've seen is the following: you have two instances of a given interface ( SomeBean ): one for environment A, and one for environment B. So you define two beans: one named "someBeanForA", and the other one named "someBeanForB".

The beans where this SomeBean must be injected don't know which one they must use: it depends on the environment. So they use an alias:

@Autowired
@Qualifier("someBeanAlias")
private SomeBean someBean;

When deploying to the environment A, the alias in the XML file points to someBeanA. When deploying to the environment B, the alias in the XML file points to someBeanB.

There is an example in the springframework docs itself.

As a concrete example, consider the case where component A defines a DataSource bean called componentA-dataSource, in its XML fragment. Component B would however like to refer to the DataSource as componentB-dataSource in its XML fragment. And the main application, MyApp, defines its own XML fragment and assembles the final application context from all three fragments, and would like to refer to the DataSource as myApp-dataSource.

We have used alias in our project and the reason why we used this is because

For an use case, the architecture is such that the bean ids are mentioned in an database master table column. So when the flow is invoked, it reads the table and loads the bean with the same name provided in the column. The bean definition is present in an applicationContext which is part of a jar and we cant change that.

Now for some instances, we had to change the bean name in the table column(to provide better naming convention) but since we cant change the context bean definition, we used aliases to map the new name to the older bean id.

This is also helps in the scenario of multiple environments. If aliases are present we don't have to run the script to update the column value in each environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM