@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
- Public class Demoservice {
- public void DoSomething () {
- System.out.println ("OK");
- }
- }
[Java] View plain copy
- import org.springframework.context.annotation.configuration;
- IMPORT ORG.SPRINGFRAMEWORK.CONTEXT.ANNOTATION.IMPORT;  
-   
- @Configuration
- @Import (demoservice. Class) //is not unsupported spring 4.2 before
- public class democonfig {
-   
- }  
Run[Java] View plain copy
- Import Org.springframework.context.annotation.AnnotationConfigApplicationContext;
- Public class Main {
- public static void Main (string[] args) {
- Annotationconfigapplicationcontext context = new Annotationconfigapplicationcontext ("com.. Example ");
- Demoservice ds = Context.getbean (demoservice. Class);
- Ds.dosomething ();
- }
- }
Output Results
OK
@import annotations in the spring framework