基於注解的配置: @Configuration, @Bean
前述
之前的注解文章僅僅只是敘述文章中心的注解, 仍舊基於XML配置而非注解掃描. (@Senyag)
Java:
1.8Maven:
3SpringFramework版本以及各組件成員:
5.1.1.RELEASE
- spring-context
- spring-core
- spring-beans
以下是官方文檔說明:
Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus, you can define beans external to your application classes by using Java rather than XML files. To use these new features, see the @Configuration, @Bean, @Import, and @DependsOn annotations.
從Spring 3.0開始,Spring JavaConfig項目提供的許多功能都成為核心Spring Framework(Core)的一部分。因此,您可以使用Java而不是XML文件在應用程序類外部定義bean。要使用這些新功能,請參閱
@Configuration,@Bean,@Import和@DependsOn注解。
各注解說明
-
@Configuration- 表示這個類可以使用 Spring IoC 容器作為 bean 定義的來源。 -
@Bean- 表示方法生成由Spring容器管理的bean。 -
@Import- 表示要導入的一個或多個@Configuration類。 -
@DependsOn- 表明有依賴條件的一個類時, 那些被它依賴的Bean類將先被初始化.
(換句話說哪個Bean被注解了這個, 那么它所依賴的Bean都會先初始化)"Spring容器載入bean順序是不確定的,Spring框架沒有約定特定順序邏輯規范。但spring保證如果A依賴B(如beanA中有@Autowired B的變量),那么B將先於A被加載。但如果beanA不直接依賴B,我們如何讓B仍先加載呢?"
---- 引用自: https://blog.csdn.net/neweastsun/article/details/78775371
這里先了解
@Configuration和@Bean.@DependsOn和@Import后面再談...
示例(@Configuration&@Bean的簡單使用)
本示例參考: https://www.cnblogs.com/microcat/p/7074720.html
作用: 簡單演示功能 -- 僅僅獲取一個Bean並調用其中方法, 並不存在依賴關系.
將了解:
@Bean用於注冊一個SpringBean.@Configuration表明這是個與Spring相關的類, 可以作為Bean定義的來源.
Bean - HelloWorld.java
非JavaBean規范
package yag;
public class HelloWorld {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Bean配置文件 - HelloConfig.java
package yag;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloConfig {
// 將HelloWrold類注冊為一個Bean
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
相當於以下XML配置:
<beans> <bean class="yag.HelloWorld"/> </beans>
注解掃描 - spring-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="yag"/>
</beans>
執行者 - Runner.java
package yag;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Runner {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(HelloConfig.class);
// 獲取這個bean
HelloWorld helloWorld = context.getBean(HelloWorld.class);
// 調用方法
helloWorld.setMessage("Hello World");
System.out.println(helloWorld.getMessage());
}
}
執行結果
Hello World
Process finished with exit code 0
示例(使用@Autowired進行Setter方法注入)
在之前的文章中已經介紹了一些通過注解進行注入的示例了. 但那些文章目的僅僅是了解那些注解如何使用, 仍舊是基於XML配置環境的. 以下將了解在注解配置中的使用方式.
背景:
HelloWorld是一個bean. 至於Bean使用者就是HelloWorldUser, 它將調用HelloWorld中的sayHello()方法(這需要一個HelloWorld實例).
Bean - HelloWorld.java
非JavaBean規范
package yag;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloWorld {
public void sayHello(){
System.out.println("Hello World");
}
}
Bean的使用者 - HelloWorldUser.java
package yag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloWorldUser {
private HelloWorld helloWorld;
@Autowired // 在setter方法上, 以實現byName裝配(將匹配id="helloWorld"的Bean)
public void setHelloWorld(HelloWorld helloWorld) {
this.helloWorld = helloWorld;
}
public void useHelloWorld(){
helloWorld.sayHello();
}
}
Bean配置文件 - HelloConfig.java
package yag;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloConfig {
@Bean(name = "helloWorld") // id="helloWorld"
public HelloWorld helloWorld(){
return new HelloWorld();
}
@Bean
public HelloWorldUser helloWorldUser(){
return new HelloWorldUser();
}
}
執行者 - Runner.java
package yag;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Runner {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(HelloConfig.class);
HelloWorldUser helloWorldUser = context.getBean(HelloWorldUser.class);
helloWorldUser.useHelloWorld();
}
}
注解掃描配置 - spring-beans.xml
為了縮短篇幅, 就羅列這些, 具體的dtd可以從上面的示例找.
<context:annotation-config/>
<context:component-scan base-package="yag"/>
執行結果
Hello World
Process finished with exit code 0
關於@Bean的隨記
值得一提的是, @Bean提供了很多參數.

