這篇文章主要為大家展示了如何使用Spring Aware標(biāo)記接口,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

我們提供的服務(wù)有:成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、柏鄉(xiāng)ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的柏鄉(xiāng)網(wǎng)站制作公司
Aware接口是一個標(biāo)記接口
XXXAare在Spring中表示對XXX可以感知,通俗點(diǎn)解釋就是:如果在某個類里面想要使用Spring的一些東西,就可以通過實(shí)現(xiàn)XXXAware接口告訴Spring,Spring看到后就會送過來,而接受的方式是通過實(shí)現(xiàn)接口唯一的方法setXXX.比如ApplicationContextAware
ApplicationContextAware使用
編寫SpringAware實(shí)現(xiàn)ApplicationContext接口
package com.rookie.bigdata.aware;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @author rookie
* @version 1.0
* @date 2020/3/21 22:58
*/
public class SpringAware implements ApplicationContextAware {
private ApplicationContext applicationContext;
//Spring容器會將ApplicationContext對象傳入過來
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("傳入的IOC容器applicationContext = [" + applicationContext + "]");
this.applicationContext = applicationContext;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
}User實(shí)體類
package com.rookie.bigdata.domain;
public class User {
private String username;
private String address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}spring-aware.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="user" class="com.rookie.bigdata.domain.User">
<property name="username" value="張三"></property>
<property name="address" value="北京"></property>
</bean>
<bean id="springAware" class="com.rookie.bigdata.aware.SpringAware"></bean>
</beans>測試類
@Test
public void test1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/aware/spring-aware.xml");
// User user = (User) applicationContext.getBean("user");
// System.out.println(user);
SpringAware springAware = (SpringAware) applicationContext.getBean("springAware");
ApplicationContext applicationContext1 = springAware.getApplicationContext();
User user = (User) applicationContext1.getBean("user");
System.out.println(user);
}以上就是關(guān)于如何使用Spring Aware標(biāo)記接口的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。
分享名稱:如何使用SpringAware標(biāo)記接口
文章出自:http://chinadenli.net/article26/ggpjjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、定制開發(fā)、搜索引擎優(yōu)化、企業(yè)網(wǎng)站制作、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)