Spring注解@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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot注解之@EnableAutoConfiguration詳解
這篇文章主要介紹了Springboot注解之@EnableAutoConfiguration詳解,@EnableAutoConfiguration是一個加載Starter目錄包之外的需要Spring自動生成bean對象,本文對其進行總結,需要的朋友可以參考下2023-08-08Security中的WebSecurityConfigurerAdapter詳解
這篇文章主要介紹了Security中的WebSecurityConfigurerAdapter詳解,今天我們要進一步的的學習如何自定義配置Spring?Security,本文結合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07