国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

4、spring使用@Autowired注解實現自動裝配

2019-11-08 02:54:24
字體:
來源:轉載
供稿:網友

在上一篇sPRing的五種自動裝配方式 教程中,我們了解到spring利用xml方式進行操作的五種自動裝配方式,而在這篇文章中我們將學習利用@Autowired注解的方式進行自動裝配

@Autowired注解自動裝配的三種方式

setter方式構造函數方式通過字段自動裝配方式

例子

第一步:創建bean

Customer.java

package com.main.autowrite.autowired.annotation;public class Customer { //即將自動裝配的屬性 private Person person; private int type; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; }}

Person.java

package com.main.autowrite.autowired.annotation;public class Person { private String name; private int 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; }}

第二步:配置beans配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean></beans>

第三步:注冊AutowiredAnnotationBeanPostProcessor

主要有以下兩種方式進行注冊

在beans配置文件中添加spring上下文和“context:annotation-config”標簽

包含 AutowiredAnnotationBeanPostProcessorbean

第一種:添加 Spring 上下文和context:annotation-config在beans配置文件中。 更改你的beans配置文件如下

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <context:annotation-config /> <bean id="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean></beans>

第二種:包含AutowiredAnnotationBeanPostProcessor 更改你的beans配置文件如下

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean></beans>

第四步:使用@Autowired注解進行自動裝配

當你已經完成上述步驟后,此時你就可以使用@Autowired注解進行自動裝配了。 主要有以下三種方式:

通過setter方法通過constructor構造方法

直接在字段屬性聲明時使用@Autowired注解進行自動裝配

第一種:通過setter方法 修改你的Customer類如下:

package com.main.autowrite.autowired.annotation;import org.springframework.beans.factory.annotation.Autowired;public class Customer { //即將自動裝配的屬性 private Person person; private int type; public Person getPerson() { return person; } @Autowired public void setPerson(Person person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; }}

第二種:通過constructor構造方法 修改你的Customer類如下

package com.main.autowrite.autowired.annotation;import org.springframework.beans.factory.annotation.Autowired;public class Customer { //即將自動裝配的屬性 private Person person; private String type; @Autowired public Customer(Person person) { this.person = person; } //getter and setter methods //toString methods}

第三種: 修改你的Customer類如下

package com.main.autowrite.autowired.annotation;import org.springframework.beans.factory.annotation.Autowired;public class Customer { //即將自動裝配的屬性 @Autowired private Person person; private String type; //getter and setter methods //toString methods}

第五步:測試(上述第四步的三種裝配方式均可使用以下方式進行測試)

@Test public void test(){ applicationContext context = new ClassPathXmlApplicationContext("com/main/autowrite/autowired/annotation/beans.xml"); Customer customer = (Customer)context.getBean("customer"); System.out.print(customer.toString()); }

測試結果:

這里寫圖片描述

結論:使用@Autowired注解實現自動裝配非常靈活,而且功能強大。 在這里可以思考一下,如果存在多個person bean,那么customer將會自動裝配哪一個??可以參考下一篇關于@Qualifier注解的使用


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 张家口市| 安福县| 西充县| 象山县| 惠州市| 游戏| 青浦区| 禹州市| 巴中市| 平潭县| 华蓥市| 蚌埠市| 阳原县| 顺昌县| 吉首市| 襄垣县| 鹤岗市| 潢川县| 田林县| 靖州| 义乌市| 福安市| 曲周县| 五台县| 库尔勒市| 丹凤县| 水城县| 孟州市| 闽清县| 连南| 桂林市| 南京市| 巴南区| 温泉县| 巨野县| 康保县| 牙克石市| 乐东| 临潭县| 河东区| 阿拉善右旗|