Spring入门学习笔记(2)——基于Java的配置
目录
基于Java的配置
@Configuration & @Bean Annotations
使用@Configuration注释类表示,Spring IoC容器可以将该类用作bean定义的源。@Bean注释告诉Spring,用@Bean注释的方法将返回一个应该在Spring应用程序上下文中注册为bean的对象。最简单的@Configuration类如下所示:
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
它和以下的XML方式定义的是等价的:
<beans>
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld" />
</beans>
带@Bean的方法名作为bean id注释,他创建并返回实际的bean。一个配置类可以拥有多个Bean的声明。一旦定义了配置类,你可以通过 AnnotationConfigApplicationContex
加载并获取
他们。
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
也可以获取加载不同的configuration
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
Example
HelloWorldConfig.java
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
HelloWorld.java
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
MainApp.java
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依赖
当@ bean相互依赖时,表示依赖关系就像让一个bean方法调用另一个bean一样简单,如下所示
@Configuration
public class AppConfig {
@Bean
public Foo foo() {
return new Foo(bar());
}
@Bean
public Bar bar() {
return new Bar();
}
}
foo bean通过构造函数注入接收到bar的引用
Example
TextEditorConfig.java
@Configuration
public class TextEditorConfig {
@Bean
public TextEditor textEditor(){
return new TextEditor( spellChecker() );
}
@Bean
public SpellChecker spellChecker(){
return new SpellChecker( );
}
}
TextEditor.java
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}
SpellChecker.java
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}
MainApp.java
public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(TextEditorConfig.class);
TextEditor te = ctx.getBean(TextEditor.class);
te.spellCheck();
}
}
输出:
Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.
@Import注解
@Import
注解允许在一个Configuration中导入另外一个配置类。
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B a() {
return new A();
}
}
这样,只需要加载ConfigB,则可以加载A,B两个配置文件,而不需要一样加载两次.
Lifecycle Callbacks(声明周期回调)
@bean注释支持指定任意的初始化和销毁回调方法,就像Spring XML的init方法和销毁方法。
public class Foo {
public void init() {
// initialization logic
}
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init", destroyMethod = "cleanup" )
public Foo foo() {
return new Foo();
}
}
指定Bean的作用域
默认作用域是singleton,可以通过以下方法重写:
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public Foo foo() {
return new Foo();
}
}