@import annotations in the spring framework

Source: Internet
Author: User

@import annotations in the spring framework@import annotations in the spring framework

In the previous article, the author introduced the spring Javaconfig. This is in addition to the use of traditional XML files, Spring brings new choices. The same author lists the annotations as part of the Java Config. If you're new to spring, there's also a lot of data indexes on spring and Spring 4.

In the list, @Import is used to consolidate all the bean configurations defined in the @configuration annotations. This is actually much like the case where we import multiple XML configuration files into a single file. The same functionality is achieved @Import annotations. This article describes using @import annotations to import javaconfig files in a spring project.

In the following example, I created two configuration files and then imported them into the master configuration file. Finally, the main configuration file is used to create the context.

Code
 car.javapackage Javabeat.net.basic; public interface car {public void print ();}     
 toyota.javapackage Javabeat.net.basic; import org.springframework.stereotype.Component;  @Component public  class toyota implements car{public void print () {System.out.println ( "I am Toyota "); }} 
 volkswagen.javapackage Javabeat.net.basic; import org.springframework.stereotype.Component;  @Component public  class volkswagen implements car{public void print () {System.out.println ( Span class= "hljs-string" > "I am Volkswagen"); }} 
 javaconfiga.javapackage Javabeat.net.basic; import Org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;  @Configuration public class javaconfiga { @Bean (name=  "Volkswagen") public Car getVolkswagen () {return new Volkswagen ();}}    
 javaconfigb.javapackage Javabeat.net.basic; import Org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;  @Configuration public class JAVACONFIGB { @Bean (name=  "Toyota") public Car getToyota () {return new Toyota ();}}    
 parentconfig.javapackage Javabeat.net.basic; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import;  @Configuration  @Import ({javaconfiga.class,javaconfigb.class}) Span class= "Hljs-keyword" >public class parentconfig {//any Other bean definitions}    
contextloader.javapackage javabeat.net.basic; Import Org.springframework.context.annotation.AnnotationConfigApplicationContext; public class contextloader {public static void main  ( String args[]) {Annotationconfigapplicationcontext context = new Annotationconfigapplicationcontext (Parentconfig.class); Car car = (Toyota) Context.getbean ( "Toyota"); Car.print (); car = (Volkswagen) Context.getbean (      

Program execution output
I am Toyata
I am Volkswagen

Summary

The author of this article describes the use of @import annotations. This annotation helps us to import multiple configuration files (either by function or by business) into a single master configuration to avoid writing all configurations in one configuration.

@Import Annotations
@Import annotations are import tags in the previous XML configuration that can be used to rely on the configuration and loading of beans in a third-party package
only import configuration classes are supported before 4.2
in 4.2, @Import annotations support importing plain Java classes and declaring them as a bean[Java] View plain copy
    1. Public class Demoservice {
    2. public void DoSomething () {
    3. System.out.println ("OK");
    4. }
    5. }
[Java] View plain copy
    1. import  org.springframework.context.annotation.configuration;  
    2. IMPORT ORG.SPRINGFRAMEWORK.CONTEXT.ANNOTATION.IMPORT;  
    3.   
    4. @Configuration   
    5. @Import (demoservice. Class) //is not unsupported spring 4.2 before   
    6. public class democonfig {  
    7.   
    8. }  
Run[Java] View plain copy
  1. Import Org.springframework.context.annotation.AnnotationConfigApplicationContext;
  2. Public class Main {
  3. public static void Main (string[] args) {
  4. Annotationconfigapplicationcontext context = new Annotationconfigapplicationcontext ("com..  Example ");
  5. Demoservice ds = Context.getbean (demoservice.   Class);
  6. Ds.dosomething ();
  7. }
  8. }
Output Results
OK

@import annotations in the spring framework

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.