Spring注解@Configuration與@Bean注冊組件的使用詳解
原始Spring開發(fā)
Person.java
準(zhǔn)備Person.java類:
package com.jektong.spring;
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}pom.xml
在pom文件導(dǎo)入Spring基本依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jektong</groupId> <artifactId>maven01</artifactId> <version>0.0.1-SNAPSHOT</version> <name>maven01</name> <description>maven01</description> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> </dependencies> </project>
bean.xml
在沒有使用Spring注解開發(fā)之前,我們通常會通過一個xml配置文件(bean.xml)去將我們需要使用的對象通過Bean的方式去注入到Spring容器中。
下面就是將Person作為對象注入Spring容器中:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.jektong.spring.Person">
<property name="name" value="zs"></property>
<property name="age" value="18"></property>
</bean>
</beans>PersonTest.java
使用一個PersonTest.java測試類測試:
package com.jektong.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonTest {
public static void main(String[] args) {
// 加載配置文件,此文件放在類路徑下。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
// 獲取bean.xml文件中注入的Person對象,并輸出。
Person bean = (Person) applicationContext.getBean("person");
System.out.println(bean);
}
}輸出結(jié)果如下:

注解Spring開發(fā)
舍棄上面的bean.xml文件,通過注解的方式將xml文件轉(zhuǎn)換成配置類,建立PersonConfig配置類:
package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.jektong.spring.Person;
@Configuration // 告訴spring這是配置類相當(dāng)于配置文件bean.xml
public class PersonConfig {
// 這是注入到spring容器的對象ID
// 括號內(nèi)指定唯一ID,不指定則是默認(rèn)以方法名為唯一ID,相當(dāng)于:<bean>標(biāo)簽中的ID值。
@Bean("person01")
public Person person() {
return new Person("李四",21);
}
}測試使用AnnotationConfigApplicationContext類讀取注解:
package com.jektong.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.jektong.config.PersonConfig;
public class PersonTest {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(PersonConfig.class);
Person bean = ac.getBean(Person.class);
System.out.println(bean);
// 查看BEAN的id
String[] beanDefinitionNames = ac.getBeanNamesForType(Person.class);
for (int i = 0; i < beanDefinitionNames.length; i++) {
System.out.println("beanid為:"+ beanDefinitionNames[i]);
}
}
}輸出如下:

@Configuration與@Bean作用總結(jié)
@Configuration
相當(dāng)于spring中的XML配置文件,將此XML文件替代成配置類,聲明在類上。
@Bean
相當(dāng)于XML文件中所配置的各個Bean對象,現(xiàn)聲明在方法上,默認(rèn)以方法名作為注入的Bean的id。
到此這篇關(guān)于Spring注解@Configuration與@Bean注冊組件的使用詳解的文章就介紹到這了,更多相關(guān)Spring @Configuration內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java多線程編程之使用thread類創(chuàng)建線程
在Java中創(chuàng)建線程有兩種方法:使用Thread類和使用Runnable接口。在使用Runnable接口時需要建立一個Thread實例2014-01-01
springboot+jwt+springSecurity微信小程序授權(quán)登錄問題
這篇文章主要介紹了springboot+jwt+springSecurity微信小程序授權(quán)登錄問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
解決Mybatis-Plus更新方法不更新NULL字段的問題
這篇文章主要介紹了解決Mybatis-Plus更新方法不更新NULL字段的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java經(jīng)典算法匯總之順序查找(Sequential Search)
Java查找算法之順序查找說明:順序查找適合于存儲結(jié)構(gòu)為順序存儲或鏈接存儲的線性表。 下面我們來詳細(xì)說明下2016-04-04
SpringBoot處理JSON數(shù)據(jù)方法詳解
這篇文章主要介紹了SpringBoot整合Web開發(fā)中Json數(shù)據(jù)處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-10-10

