[Translate]17-spring Java code-based configuration metadata

Source: Internet
Author: User
Tags tutorialspoint

Spring also supports Java code-based configuration metadata. But this is a less common approach, but there are some people who use it. So it is necessary to introduce.

Spring is based on Java code Configuration metadata, which can be declared as a configuration class by @configuration annotations, and a new one through the @bean annotations

The classes that are created are managed by the spring container. In this configuration, we can assemble the beans manually, or we can assemble the beans automatically. I feel this way.

Use manual assembly in the same way, especially when there are multiple configuration classes.

Let's look at an example:

1. Create a new package Com.tutorialspoint.javacode and create a new Texteditor.java, Spellchecker.java, Highlighter.java in the package

//Texteditor.java PackageCom.tutorialspoint.javacode;ImportJavax.annotation.Resource; Public classTextEditor {Privatespellchecker spellchecker; Privatehighlighter highlighter; @Resource Public voidsethighlighter (highlighter highlighter) { This. Highlighter =highlighter; } @Resource Public voidSetspellchecker (spellchecker spellchecker) { This. Spellchecker =spellchecker; }         PublicTextEditor () {} PublicTextEditor (spellchecker spellchecker) { This. spellchecker=spellchecker; }     PublicTextEditor (highlighter highlighter) { This. highlighter=highlighter; }         Public voidinit () {System.out.println ("Init method invoked ..."); }     Public voiddestroy () {System.out.println ("Destroy method invoked ..."); }         Public voidprint () {System.out.println ("Spellchecker:" +spellchecker); System.out.println ("Highlighter:" +highlighter); }    }//Spellchecker.java PackageCom.tutorialspoint.javacode; Public classSpellchecker { Public voidCheckspell () {System.out.println ("Checking ..."); }}//Highlighter.java PackageCom.tutorialspoint.javacode; Public classHighlighter { Public voidHighlight () {System.out.println ("Highlighting ..."); }}

2. Create the following three configuration classes in package Com.tutorialspoint.javacode:

//Appconfig.java PackageCom.tutorialspoint.javacode;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.annotation.Import;/*** Use @Configuration annotations to indicate that a class belongs to a configuration class * You can introduce additional configuration classes through @import annotations. * */@Configuration @import (value={spellcheckerconfig.class, Highlighterconfig.class}) Public classAppConfig {/*** You can specify the bean's name in the spring container by using the Name property of the @bean annotation * Use the Initmethod property to specify the bean's initialization method * Use the Destroymethod property to specify the Bean's Destruction method */@Bean (Name= "TextEditor", initmethod= "Init", destroymethod= "destroy")     Publictexteditor TextEditor () {return NewTextEditor (); }    }//Highlighterconfig.java PackageCom.tutorialspoint.javacode;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration; @Configuration Public classhighlighterconfig {@Bean (name= "Highlighter")     PublicHighlighter highlighter () {return Newhighlighter (); }}//Spellcheckerconfig.java PackageCom.tutorialspoint.javacode;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration; @Configuration Public classspellcheckerconfig {@Bean (name= "Spellchecker")     Publicspellchecker spellchecker () {return Newspellchecker (); }        }

3. Create a new Mainapp.java in the package Com.tutorialspoint.javacode. The contents are as follows:

 PackageCom.tutorialspoint.javacode;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;ImportOrg.springframework.context.support.AbstractApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {//Java code-based implementation class for a container to use AnnotationconfigapplicationcontextAbstractapplicationcontext CTX =NewAnnotationconfigapplicationcontext (AppConfig.class); TextEditor te= (TextEditor) Ctx.getbean (texteditor.class);                Te.print (); //registers the JVM's exit event in order for the Bean's Destroy method to workCtx.registershutdownhook (); }}

4. Run the program and check the results:

However, Java code-based configuration metadata does not support automatic dependency injection of constructor parameters, and constructor parameters must be assembled manually.

[Translate]17-spring Java code-based configuration metadata

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.