Spring注解@Configuration與@Bean注冊(cè)組件的使用詳解
原始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ā)之前,我們通常會(huì)通過一個(gè)xml配置文件(bean.xml)去將我們需要使用的對(duì)象通過Bean的方式去注入到Spring容器中。
下面就是將Person作為對(duì)象注入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
使用一個(gè)PersonTest.java測(cè)試類測(cè)試:
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對(duì)象,并輸出。 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容器的對(duì)象ID // 括號(hào)內(nèi)指定唯一ID,不指定則是默認(rèn)以方法名為唯一ID,相當(dāng)于:<bean>標(biāo)簽中的ID值。 @Bean("person01") public Person person() { return new Person("李四",21); } }
測(cè)試使用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文件中所配置的各個(gè)Bean對(duì)象,現(xiàn)聲明在方法上,默認(rèn)以方法名作為注入的Bean的id。
到此這篇關(guān)于Spring注解@Configuration與@Bean注冊(cè)組件的使用詳解的文章就介紹到這了,更多相關(guān)Spring @Configuration內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java判斷數(shù)字位數(shù)的方法總結(jié)
本文給大家整理了Java判斷數(shù)字位數(shù)的兩種常用方法,對(duì)此有興趣的可以跟著小編一起學(xué)習(xí)下。2018-02-02Java深入了解數(shù)據(jù)結(jié)構(gòu)之哈希表篇
哈希表是一種根據(jù)關(guān)鍵碼去尋找值的數(shù)據(jù)映射結(jié)構(gòu),該結(jié)構(gòu)通過把關(guān)鍵碼映射的位置去尋找存放值的地方,說起來可能感覺有點(diǎn)復(fù)雜,我想我舉個(gè)例子你就會(huì)明白了,最典型的的例子就是字典2022-01-01SpringCloud之微服務(wù)容錯(cuò)的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloud之微服務(wù)容錯(cuò)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05詳解如何在SpringBoot項(xiàng)目中使用全局異常處理
在完整的項(xiàng)目開發(fā)中,異常的出現(xiàn)幾乎是無法避免的;如果凡是有可能出現(xiàn)異常的地方,我們都手動(dòng)的使用try-catch將其捕獲的話,會(huì)使得代碼顯得十分臃腫并且后期不好維護(hù)。本文介紹了pringBoot項(xiàng)目中使用全局異常處理的方法,需要的可以參考一下2022-10-10Springboot注解之@EnableAutoConfiguration詳解
這篇文章主要介紹了Springboot注解之@EnableAutoConfiguration詳解,@EnableAutoConfiguration是一個(gè)加載Starter目錄包之外的需要Spring自動(dòng)生成bean對(duì)象,本文對(duì)其進(jìn)行總結(jié),需要的朋友可以參考下2023-08-08Security中的WebSecurityConfigurerAdapter詳解
這篇文章主要介紹了Security中的WebSecurityConfigurerAdapter詳解,今天我們要進(jìn)一步的的學(xué)習(xí)如何自定義配置Spring?Security,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07