Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 241 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring - usage of alias vs names

#1
I am confused on the usage of alias. I do understand what alias is and how it is being used but i don't see how it can be any different than using names on a bean definition.

<bean id="xyx" name="abc,def" .. />

<alias name="xyx" alias="pqr"/>

Why the alias when i can use abc or def?
Reply

#2
In my mind bean aliasing can be helpful in large system, where you can not manipulate bean names. You have option to create your own name (alias) specific for your part of the system...

from Spring documentation (3.0.x)

[To see links please register here]

> ...it is sometimes desirable to give a single bean multiple names,
> otherwise known as bean aliasing...

therefore creating multiple names or/and aliasing are the same thing.
Reply

#3
Alias has a specific using scenario which multiple names don't have:

Imagine multiple config xml files in your project, most of which are authored by your colleagues, and you need to add your own config.xml file. Using <alias> you'll be able to refer to a bean defined in another config file with a different name that's maybe more meaningful to your config, without having to touch your colleagues' config files.
Reply

#4
I recently found another use case where alias easily solved a problem.

When auto configuration is active, Spring Boot provides the bean `serverProperties` which can be used to access information about the server currently running the web app.

In integration tests (i.e. when `@SpringBootTest` annotation is present) the same bean is available under the name `org.springframework.boot.autoconfigure.web.ServerProperties`.

Of course it is possible to use a different profile for integration testing, but that would require manual change of configuration at multiple places. However, simply by adding

<alias name="serverProperties" alias="org.springframework.boot.autoconfigure.web.ServerProperties"/>

the same configuration files can be used for integration tests and in production.

This might be a bug in Spring Boot, however alias easily solve the problem without waiting for a new release. And most certainly I have no possibility to alter the Boot configuration myself.
Reply

#5
An aliased bean will always have higher priority over a non-aliased one, and in case of having different beans with the same alias then the last one declared will have the priority. In other words, the aliased bean will override the non-aliased beans.
This can be particularly useful when creating big projects or when you are building extensions to your project and don't want to touch the original bean definition.
Reply

#6
A use case maybe when you want to customize some beans that are already defined somewhere in a modular application (each module is a spring project for example), the bean maybe defined by a third-party framework/API or even your team. In that case you want that only inside your spring project call the customized version without altering other modules (projects), to do that just add the alias in your spring configuration which is indeed a powerful feature:

<alias alias="globalBeanService" name="customizedBeanService" />

Hence, whenever spring find a call to the globalBeanService, it will inject customizedBeanService for you inside your specific module.
Without this feature, you should go through all classes and modify the bean manually!!
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through