2nd-Assembly bean-Assembly of Beans through Java code

Source: Internet
Author: User

Assembling beans through Java code

? In the case of explicit assembly, there are two options: Java and XML configuration, which first describes how Java is configured.

2.3.1 Creating a Configuration class

First review the previous chapter of the configuration content:

@Configurationpublicclassimplements CompactDisc {    private"sgt";    private"Twinkle, twinkle, little start";    publicvoidplay() {        System.out.println("title:""song:" + song);    }}

? Here we remove the @componentscan annotations, leaving only the @configuration annotations, indicating that the class is a configuration class and does not contain details on how to implement the bean, and if this is the only way to run it, The test fails with a Beancreation-exception exception. The test is expected to be injected into cdplayer and compactdisc, but these beans are not created at all, because component scans will not find them. To test pass, you need to add the @ Componentscan's annotations.

2.3.2. Declaring a simple bean

To declare the bean in javaconfig, we need to add @bean annotations to the method as follows:

    @Bean    publicsgtPeppers(){        returnnewSgtPeppers();    }

By default, the Bean's ID is the same as the method name with the @bean annotation (this example is Sgtpeppers), and if you want to set it to a different name, you can rename the method to specify the bean's Name property @bean (name = "Othername" ) in a way to achieve.

2nd-Assembly bean-Assembly of Beans through Java code

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.