Sping the actual combat through Java code Assembly bean

Source: Internet
Author: User

Although it is more recommended to implement spring's automated configuration in many scenarios through component scanning and automated assembly, sometimes the automation configuration scenario does not work, so you need to explicitly configure spring. For example, if you want to assemble components from a third-party library into your application, in which case there is no way to add @component and @autowired annotations on its classes, so you cannot use automated assembly scenarios.

In this case, you have to adopt an explicit assembly method. There are two alternative scenarios for explicit configuration: Java and XML. In this section, we'll learn how to use Java configuration, and the next section will continue to learn about spring's XML configuration

As I said before, Javaconfig is a better solution for explicit configuration because it is more powerful, type-safe, and friendly to refactoring. Because it's Java code, just like any other Java code in the application. At the same time, Javaconfig differs from other Java code in that it is conceptually different from the business logic and domain code in the application. Although it is expressed in the same language as other components, Javaconfig is the configuration code. This means that it should not contain any business logic and that javaconfig should not intrude into the business logic code. Although not required, it is common to place javaconfig in a separate package, separating it from other application logic so that its intentions are not confused.

  

1.1 Using Javaconfig for Injection

We need to declare Cdplayerbean, which relies on compactdisc. In Javaconfig, how do you assemble them together?

Here's a practical way to declare cdplayer

The CDPlayer () method, like the Sgtpeppers () method, also uses the @bean annotation, which indicates that this method creates a bean instance and registers it in the spring application context. The bean ID created is CDPlayer, which is the same as the name of the method.

It seems that Compactdisc is obtained by calling Sgtpeppers (), but this is not entirely the case. Because the @bean annotation is added to the Sgtpeppers () method, spring intercepts all calls to it and ensures that the method is returned directly to the

Bean instead of making a real call to it every time.

  

For example, suppose you introduce an additional cdplayerbean, which is exactly the same as the previous bean:

If the call to Sgtpeppers () is like any other Java method call, then each CDPlayer instance will have its own unique instance of Sgtpeppers. It makes sense if we're talking about actual CD players and CD CDs. If you have two CD players, there is no physical way to put the same CD disc in two CD players. However, in the field of software, we can inject the same sgtpeppers instance into any number of other beans completely. By default, the beans in spring are singleton, and we don't need to create exactly the same Sgtpeppers instance for the second CDPlayer Bean. Therefore, spring intercepts the call to Sgtpeppers () and ensures that the bean created by spring is returned, which is the Compactdiscbean created by spring itself when it calls Sgtpeppers (). As a result, two cdplayer beans will get the same sgtpeppers instance.

As you can see, invoking a method to reference a bean is a bit confusing. In fact, there is a more simple way to understand:

Here, the CDPlayer () method requests a compactdisc as a parameter. When spring calls CDPlayer () to create a Cdplayerbean, it automatically assembles a compactdisc into the configuration method. The method body can then use it in the appropriate way. With this technique, the CDPlayer () method can also inject compactdisc into the CDPlayer constructor without explicitly referencing the @bean method of Compactdisc

  

Referencing other beans in this way is often the best choice because it does not require that Compactdisc be declared into the same configuration class. There is not even a requirement for Compactdisc to be declared in Javaconfig, which can actually be configured either automatically by component scanning or through XML. You can spread the configuration across multiple configuration classes, XML files, and automatic scanning and assembly beans, as long as the functionality is complete . Regardless of how Compactdisc is created, Spring will pass it into the configuration method and be used to create the CDPlayer bean.

again, the method with @bean annotations can take any necessary Java functionality to produce a bean instance. The constructor and setter methods are just two simple examples of the @bean method . The possibilities that exist here are limited only by the Java language.

Sping the actual combat through Java code Assembly bean

Related Article

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.