带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。
@Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean
案例代码:
HelloWorld.java
package com.baklib.hello;public class HelloWorld {private String message;public void getMessage() {System.out.println("Your Message : "+message);}public void setMessage(String message) {this.message = message;}}
HelloWorldConfig.java
package com.baklib.hello;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class HelloWorldConfig {@Beanpublic HelloWorld helloWorld(){return new HelloWorld();}}
MainApp.java
package com.baklib.hello;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;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();}}
编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息
Your Message : Hello World!
注入 Bean 的依赖
案例代码:
SpellChecker.java
package com.baklib.hellobean;public class SpellChecker {public SpellChecker() {System.out.println("Inside SpellChecker constructor.");}public void checkSpelling() {System.out.println("Inside checkSpelling.");}}
TextEditor.java
package com.baklib.hellobean;public class TextEditor {private SpellChecker spellChecker;public TextEditor(SpellChecker spellChecker) {System.out.println("Inside TextEditor constructor.");this.spellChecker = spellChecker;}public void spellCheck(){spellChecker.checkSpelling();}}
TextEditorConfig.java
package com.baklib.hellobean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class TextEditorConfig {@Beanpublic TextEditor textEditor() {return new TextEditor(spellChecker());}@Beanpublic SpellChecker spellChecker(){return new SpellChecker();}}
MainApp.java
package com.baklib.hellobean;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainApp {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(TextEditorConfig.class);TextEditor te = context.getBean(TextEditor.class);te.spellCheck();}}
编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息
Inside SpellChecker constructor.Inside TextEditor constructor.Inside checkSpelling.
@import 注解允许从另一个配置类中加载 @Bean 定义
案例代码:
新建 A.java 和 B.java
ConfigA.java
package com.baklib.helloImport;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class ConfigA {@Beanpublic A a() {return new A();}}
ConfigB.java
package com.baklib.helloImport;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;@Configuration@Import(ConfigA.class)public class ConfigB {@Beanpublic B b(){return new B();}}
MainApp.java
package com.baklib.helloImport;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainApp {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(ConfigB.class);A a = context.getBean(A.class);B b = context.getBean(B.class);}}
@Bean(initMethod = “init”, destroyMethod = “cleanup” ) 指定任意的初始化和销毁的回调方法
@Scope 注解指定 Bean 的范围
