導語
前些天,以爲剛接觸Spring的朋友,問我她代碼中的@Bean("XXX")中的這個“XXX”怎麼和bean 的名字怎麼不一樣,然後我就想着是不是該寫一下這類博客。其實說到這裏我要說一下她當時寫的代碼是在一個類中使用了@Configuration註解標註的,然後方法上標註了@Bean註解,此時如果對Spring比較熟悉的應該就知道我所說的是什麼了。
是的,她所做的就是再寫一個配置類,那麼配置類是什麼呢?它的前身又是什麼呢?其實@Configuration註解在Spring 3.0版本的時候就已經提供功能了。今天這裏暫且不說這些Spring的歷史,筆者簡單的以Spring的XML配置文件和註解的方式來對比一下。在開始之前先放一張圖(圖片是哪位朋友問問題時的截圖)和一段代碼(代碼是@Configuration註解的簡單代碼),如下:
/**
* Indicates that a class declares one or more {@link Bean @Bean} methods and
* @author Rod Johnson
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.0
* @see Bean
* @see Profile
* @see Import
* @see ImportResource
* @see ComponentScan
* @see Lazy
* @see PropertySource
* @see AnnotationConfigApplicationContext
* @see ConfigurationClassPostProcessor
* @see org.springframework.core.env.Environment
* @see org.springframework.test.context.ContextConfiguration
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(annotation = Component.class)
String value() default "";
........
}
一、XML配置文件的實現方式
在以前的XML配置文件中,我們所實現的方式就是使用<bean/>標籤的方式來實現的,不多說直接上代碼:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.pat.bean.Person">
<property name="id" value="1"/>
<property name="name" value="不才人"/>
<property name="age" value="26"/>
</bean>
</beans>
public class Person {
private Integer id;
private String name;
private int age;
// 此處省略一萬行代碼
}
從上面的代碼可以看出,在實體類中所定義的屬性在XML配置文件中是以<property/>標籤來體現的。繼續看下測試代碼:
public class MainTest {
public static void main(String[] args) {
// 配置文件測試方式
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");
Person person = applicationContext.getBean(Person.class);
System.out.println(person);
}
}
這段代碼就是先衝類路徑下獲取spring-bean.xml配置文件,然後獲取Person對象,最後打印,這貌似沒什麼可說的,那就來說下本次的重點@Configuration註解吧。
二、@Configuration註解
在當前這個Spring Boot橫行的時代,@Configuration註解的使用已經是家常便飯的事情了,一些一開始沒接觸過Spring的朋友,可能對一些註解和使用方式可能一臉懵逼,因爲Spring Boot的的基礎是Spring,那麼就先說下@Configuration註解的前身?不急,先來看下代碼:
@Configuration
public class PersonConfig {
@Bean
public Person person(){
return new Person(2, "浪隨客", 27);
}
}
這代碼是不是和前面一開始的圖片中的代碼使用很相似?是的,這就是一個demo,我們先看下這裏和XML中的是不是感覺又那麼一絲絲相似,是的,其實最後都會打印出Person實體對象的信息,也就是說標註了@Configuration註解的類就是一個配置類,也就和XML中的<beans/> 標籤的作用是一樣的,在這個裏面PersonConfig 類中,我們也可以使用多個@Bean註解來標註出多個實體對象。就像<beans/>標籤中可以有多個<bean/>標籤一樣。
其實在上面的XML的方式中所說到的<bean/>標籤中的id屬性就是用來聲明這個對象的名稱的,不信可以自己試試(代碼路徑最後會貼出來),說到這裏就要說到配置類中的對象名稱是什麼了,在@Bean註解沒標註任何信息的時候,方法名稱就是當前返回對象的名稱,如果@Bean("pesron123")是這樣寫的,那麼這裏面的“person123”就是類名稱了。不信你也可以試試。最後還是特一下這個使用方式的測試方法,代碼如下:
public class MainTest {
public static void main(String[] args) {
// // 配置文件測試方式
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");
// Person person = applicationContext.getBean(Person.class);
// @Configuration配置類測試方式
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
Person person = annotationConfigApplicationContext.getBean(Person.class);
System.out.println(person);
}
}
總結
本次文章,主要說了一下幾點:
1.列舉了XML配置文件的使用方式和@Configuration註解的使用方式
2.對兩者進行了一下對比
3.說了一下@Bean的使用
代碼:https://github.com/zfy-zhang/spring-annotation