带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。
    @Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean
    案例代码:
    HelloWorld.java

    1. package com.baklib.hello;
    2. public class HelloWorld {
    3. private String message;
    4. public void getMessage() {
    5. System.out.println("Your Message : "+message);
    6. }
    7. public void setMessage(String message) {
    8. this.message = message;
    9. }
    10. }

    HelloWorldConfig.java

    1. package com.baklib.hello;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. @Configuration
    5. public class HelloWorldConfig {
    6. @Bean
    7. public HelloWorld helloWorld(){
    8. return new HelloWorld();
    9. }
    10. }

    MainApp.java

    1. package com.baklib.hello;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    4. public class MainApp {
    5. public static void main(String[] args) {
    6. ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
    7. HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
    8. helloWorld.setMessage("Hello World!");
    9. helloWorld.getMessage();
    10. }
    11. }

    编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息

    1. Your Message : Hello World!

    注入 Bean 的依赖
    案例代码:
    SpellChecker.java

    1. package com.baklib.hellobean;
    2. public class SpellChecker {
    3. public SpellChecker() {
    4. System.out.println("Inside SpellChecker constructor.");
    5. }
    6. public void checkSpelling() {
    7. System.out.println("Inside checkSpelling.");
    8. }
    9. }

    TextEditor.java

    1. package com.baklib.hellobean;
    2. public class TextEditor {
    3. private SpellChecker spellChecker;
    4. public TextEditor(SpellChecker spellChecker) {
    5. System.out.println("Inside TextEditor constructor.");
    6. this.spellChecker = spellChecker;
    7. }
    8. public void spellCheck(){
    9. spellChecker.checkSpelling();
    10. }
    11. }

    TextEditorConfig.java

    1. package com.baklib.hellobean;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. @Configuration
    5. public class TextEditorConfig {
    6. @Bean
    7. public TextEditor textEditor() {
    8. return new TextEditor(spellChecker());
    9. }
    10. @Bean
    11. public SpellChecker spellChecker(){
    12. return new SpellChecker();
    13. }
    14. }

    MainApp.java

    1. package com.baklib.hellobean;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    4. public class MainApp {
    5. public static void main(String[] args) {
    6. ApplicationContext context = new AnnotationConfigApplicationContext(TextEditorConfig.class);
    7. TextEditor te = context.getBean(TextEditor.class);
    8. te.spellCheck();
    9. }
    10. }

    编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息

    1. Inside SpellChecker constructor.
    2. Inside TextEditor constructor.
    3. Inside checkSpelling.

    @import 注解允许从另一个配置类中加载 @Bean 定义
    案例代码:
    新建 A.java 和 B.java
    ConfigA.java

    1. package com.baklib.helloImport;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. @Configuration
    5. public class ConfigA {
    6. @Bean
    7. public A a() {
    8. return new A();
    9. }
    10. }

    ConfigB.java

    1. package com.baklib.helloImport;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.context.annotation.Import;
    5. @Configuration
    6. @Import(ConfigA.class)
    7. public class ConfigB {
    8. @Bean
    9. public B b(){
    10. return new B();
    11. }
    12. }

    MainApp.java

    1. package com.baklib.helloImport;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    4. public class MainApp {
    5. public static void main(String[] args) {
    6. ApplicationContext context = new AnnotationConfigApplicationContext(ConfigB.class);
    7. A a = context.getBean(A.class);
    8. B b = context.getBean(B.class);
    9. }
    10. }

    @Bean(initMethod = “init”, destroyMethod = “cleanup” ) 指定任意的初始化和销毁的回调方法
    @Scope 注解指定 Bean 的范围