亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2022年06月14日 11:15:52   作者:爪哇斗羅  
這篇文章主要介紹了SpringBoot中的注解@Configuration與@Bean注冊組件的使用,具有很好的參考價值,希望對大家有所幫助

原始Spring開發(fā)

Person.java

準備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文件導入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);		
	}
}

輸出結果如下:

注解Spring開發(fā)

舍棄上面的bean.xml文件,通過注解的方式將xml文件轉換成配置類,建立PersonConfig配置類:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.jektong.spring.Person;
@Configuration // 告訴spring這是配置類相當于配置文件bean.xml
public class PersonConfig {
    // 這是注入到spring容器的對象ID
    // 括號內指定唯一ID,不指定則是默認以方法名為唯一ID,相當于:<bean>標簽中的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作用總結

@Configuration

相當于spring中的XML配置文件,將此XML文件替代成配置類,聲明在類上。

@Bean

相當于XML文件中所配置的各個Bean對象,現聲明在方法上,默認以方法名作為注入的Bean的id。

到此這篇關于Spring注解@Configuration與@Bean注冊組件的使用詳解的文章就介紹到這了,更多相關Spring @Configuration內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java判斷數字位數的方法總結

    Java判斷數字位數的方法總結

    本文給大家整理了Java判斷數字位數的兩種常用方法,對此有興趣的可以跟著小編一起學習下。
    2018-02-02
  • Java深入了解數據結構之哈希表篇

    Java深入了解數據結構之哈希表篇

    哈希表是一種根據關鍵碼去尋找值的數據映射結構,該結構通過把關鍵碼映射的位置去尋找存放值的地方,說起來可能感覺有點復雜,我想我舉個例子你就會明白了,最典型的的例子就是字典
    2022-01-01
  • Java使用BouncyCastle加密

    Java使用BouncyCastle加密

    本文主要介紹了Java使用BouncyCastle加密,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • SpringCloud之微服務容錯的實現

    SpringCloud之微服務容錯的實現

    這篇文章主要介紹了SpringCloud之微服務容錯的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • 詳解如何在SpringBoot項目中使用全局異常處理

    詳解如何在SpringBoot項目中使用全局異常處理

    在完整的項目開發(fā)中,異常的出現幾乎是無法避免的;如果凡是有可能出現異常的地方,我們都手動的使用try-catch將其捕獲的話,會使得代碼顯得十分臃腫并且后期不好維護。本文介紹了pringBoot項目中使用全局異常處理的方法,需要的可以參考一下
    2022-10-10
  • Springboot注解之@EnableAutoConfiguration詳解

    Springboot注解之@EnableAutoConfiguration詳解

    這篇文章主要介紹了Springboot注解之@EnableAutoConfiguration詳解,@EnableAutoConfiguration是一個加載Starter目錄包之外的需要Spring自動生成bean對象,本文對其進行總結,需要的朋友可以參考下
    2023-08-08
  • Maven本地緩存清理小工具的實現

    Maven本地緩存清理小工具的實現

    這篇文章主要介紹了Maven本地緩存清理小工具的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • java中的AWT事件處理問題

    java中的AWT事件處理問題

    這篇文章主要介紹了java中的AWT事件處理問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Security中的WebSecurityConfigurerAdapter詳解

    Security中的WebSecurityConfigurerAdapter詳解

    這篇文章主要介紹了Security中的WebSecurityConfigurerAdapter詳解,今天我們要進一步的的學習如何自定義配置Spring?Security,本文結合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • Java應該在哪里判斷List是否為空

    Java應該在哪里判斷List是否為空

    在Java中,我們常用List來存儲數據,但是我們怎么判斷它是否成功帶來了我們需要的數據呢?下面這篇文章主要給大家介紹了關于Java應該在哪里判斷List是否為空的相關資料,需要的朋友可以參考下
    2022-02-02

最新評論