最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring注解@Configuration與@Bean注冊組件的使用詳解

 更新時間:2022年06月14日 11:15:52   作者:爪哇斗羅  
這篇文章主要介紹了SpringBoot中的注解@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多線程編程之使用thread類創(chuàng)建線程

    在Java中創(chuàng)建線程有兩種方法:使用Thread類和使用Runnable接口。在使用Runnable接口時需要建立一個Thread實例
    2014-01-01
  • Java如何實現(xiàn)保證線程安全

    Java如何實現(xiàn)保證線程安全

    這篇文章主要介紹了Java如何實現(xiàn)保證線程安全問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • springboot+jwt+springSecurity微信小程序授權(quán)登錄問題

    springboot+jwt+springSecurity微信小程序授權(quán)登錄問題

    這篇文章主要介紹了springboot+jwt+springSecurity微信小程序授權(quán)登錄問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 解決Mybatis-Plus更新方法不更新NULL字段的問題

    解決Mybatis-Plus更新方法不更新NULL字段的問題

    這篇文章主要介紹了解決Mybatis-Plus更新方法不更新NULL字段的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java?AQS?線程安全同步隊列的實現(xiàn)

    Java?AQS?線程安全同步隊列的實現(xiàn)

    AQS 同步隊列是很多的 Java 線程安全對象的實現(xiàn),例如 ReentrantLock, Semaphore, CountDownLatch, ReentrantReadWriteLock 等等,本文就介紹了Java?AQS?線程安全同步隊列的實現(xiàn),感興趣的可以了解一下
    2023-08-08
  • Java經(jīng)典算法匯總之順序查找(Sequential Search)

    Java經(jīng)典算法匯總之順序查找(Sequential Search)

    Java查找算法之順序查找說明:順序查找適合于存儲結(jié)構(gòu)為順序存儲或鏈接存儲的線性表。 下面我們來詳細(xì)說明下
    2016-04-04
  • 詳解java 中的CAS與ABA

    詳解java 中的CAS與ABA

    這篇文章主要介紹了java 中的CAS與ABA的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-05-05
  • List集合多線程并發(fā)條件下不安全如何解決

    List集合多線程并發(fā)條件下不安全如何解決

    List是我們常用的集合,但是在多線程并發(fā)的條件下,會出現(xiàn)安全問題嗎?下面我們就來測試一下,如果出現(xiàn)安全問題,該如何解決,感興趣的可以了解一下
    2021-12-12
  • SpringBoot處理JSON數(shù)據(jù)方法詳解

    SpringBoot處理JSON數(shù)據(jù)方法詳解

    這篇文章主要介紹了SpringBoot整合Web開發(fā)中Json數(shù)據(jù)處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-10-10
  • Maven之遠(yuǎn)程倉庫的配置詳解

    Maven之遠(yuǎn)程倉庫的配置詳解

    這篇文章主要介紹了Maven之遠(yuǎn)程倉庫的配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評論

鹿泉市| 崇信县| 南丰县| 修文县| 涿州市| 佛教| 新宁县| 黄平县| 南郑县| 广宗县| 青川县| 五峰| 丁青县| 长垣县| 大冶市| 台北县| 合作市| 滨州市| 昭平县| 长春市| 卢氏县| 天等县| 泸州市| 海林市| 绥江县| 罗源县| 邻水| 宜良县| 湄潭县| 南澳县| 麻城市| 江都市| 通化县| 米脂县| 米易县| 宜宾市| 安图县| 体育| 乌拉特中旗| 鄂伦春自治旗| 科尔|