[转载]Spring Java Based Configuration

@Configuration & @Bean Annotations 

Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.  

Here is the content of HelloWorldConfig.java file:

package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {

@Bean

   public HelloWorld helloWorld(){
      return new HelloWorld();

} }

Here is the content of HelloWorld.java file:

package com.tutorialspoint;
public class HelloWorld {
   private String message;
   public void setMessage(String message){
      this.message  = message;

}

   public void getMessage(){
      System.out.println("Your Message : " + message);

} }

 

Following is the content of the MainApp.java file:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext ctx =
      new AnnotationConfigApplicationContext(HelloWorldConfig.class);
      HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
      helloWorld.setMessage("Hello World!");
      helloWorld.getMessage();
   }

}

Once you are done with creating all the source filesand adding required additional libraries, let us run the application. You should note that there is no configuration file required. If everything is fine with your application, this will print the following message:

Your Message : Hello World!

ge com.tutorialspoint;

 import org.springframework.context.annotation.*;
 @Configuration
 public class TextEditorConfig {

@Bean

    public TextEditor textEditor(){
       return new TextEditor( spellChecker() );

}

@Bean

    public SpellChecker spellChecker(){
       return new SpellChecker( );

} }

Here is the content of TextEditor.java file: package com.tutorialspoint;

 public class TextEditor {
    private SpellChecker spellChecker;
    public TextEditor(SpellChecker spellChecker){
       System.out.println("Inside TextEditor constructor." );

Injecting Bean Dependencies

When @Beans have dependencies on one another, expressing that dependency is as simple as having one bean method calling another as follow 

Here is the content of TextEditorConfig.java file: 

package com.tutorialspoint;

 import org.springframework.context.annotation.*;
 @Configuration
 public class TextEditorConfig {

@Bean

    public TextEditor textEditor(){
       return new TextEditor( spellChecker() );

}

@Bean

    public SpellChecker spellChecker(){
       return new SpellChecker( );

} }

Here is the content of TextEditor.java file: package com.tutorialspoint;

 public class TextEditor {
    private SpellChecker spellChecker;
    public TextEditor(SpellChecker spellChecker){
       System.out.println("Inside TextEditor constructor." );

 

this.spellChecker = spellChecker;
   }
   public void spellCheck(){
      spellChecker.checkSpelling();

} }

Following is the content of another dependent class file SpellChecker.java:

package com.tutorialspoint;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );

}

   public void checkSpelling(){
      System.out.println("Inside checkSpelling." );

} }

Following is the content of the MainApp.java file:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext ctx =
      new AnnotationConfigApplicationContext(TextEditorConfig.class);
      TextEditor te = ctx.getBean(TextEditor.class);
      te.spellCheck();

} }

Once you are done with creating all the source files and adding required additional libraries, let us run the application. You should note that there is no configuration file required. If everything is fine with your application, this will print the following message:

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.

The @Import Annotation

The @Import annotation allows for loading @Bean definitions from another configuration class. Consider a ConfigA class as follows:

@Configuration
public class ConfigA {
   @Bean
 public A a() {
      return new A();

} }

You can import above Bean declaration in another Bean Declaration as follows:

@Configuration
@Import(ConfigA.class)
public class ConfigB {

@Bean

   public B a() {
      return new A();

}

}

Now, rather than needing to specify both ConfigA.class and ConfigB.class when instantiating the context, only ConfigB needs to be supplied as follows:

public static void main(String[] args) {
   ApplicationContext ctx =
   new AnnotationConfigApplicationContext(ConfigB.class);
   // now both beans A and B will be available...
   A a = ctx.getBean(A.class);
   B b = ctx.getBean(B.class);

原文地址:https://www.cnblogs.com/reynold-lei/p/3545395.html