spring boot: spring Aware的目的是為了讓Bean獲得Spring容器的服務

Spring?Aware的目的是為了讓Bean獲得Spring容器的服務

?

//獲取容器中的bean名稱
import org.springframework.beans.factory.BeanNameAware;
//獲得資源加載器,可以獲得額外的資源
import org.springframework.context.ResourceLoaderAware;

?

package ch2.aware;
import java.io.IOException;import org.apache.commons.io.IOUtils;
//獲取容器中的bean名稱
import org.springframework.beans.factory.BeanNameAware;
//獲得資源加載器,可以獲得額外的資源
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;//AwareService是spring的一個組件
@Service
//實現BeanNameAware,ResourceLoaderAware資源的接口,獲得名稱和資源加載的服務
public class AwareService implements BeanNameAware,ResourceLoaderAware {private String beanName;private ResourceLoader loader;//實現ResourceLoaderAware需要重寫setResourceLoader@Overridepublic void setResourceLoader(ResourceLoader resourceLoader) {// TODO Auto-generated method stubthis.loader = resourceLoader;}//實現BeanNameAware需要重寫setBeanName@Overridepublic void setBeanName(String name) {// TODO Auto-generated method stubthis.beanName = name;		}public void outputResult(){System.out.println("bean的名字為:"+ beanName);Resource resource = loader.getResource("classpath:ch2/aware/test.txt");try {System.out.println("ResourceLoader加載的內容為:" + IOUtils.toString(resource.getInputStream()));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

 配置:

package ch2.aware;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;//聲明本類是一個配置類
@Configuration
//自動加載ch2.aware包下面的內容
@ComponentScan("ch2.aware")
public class AwareConfig {}

  運行:

package ch2.aware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main(String[] args){AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(AwareConfig.class);AwareService awareService  = context.getBean(AwareService.class);awareService.outputResult();context.close();}}

  

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391641.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391641.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391641.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

10張圖帶你深入理解Docker容器和鏡像

【編者的話】本文用圖文并茂的方式介紹了容器、鏡像的區別和Docker每個命令后面的技術細節,能夠很好的幫助讀者深入理解Docker。這篇文章希望能夠幫助讀者深入理解Docker的命令,還有容器(container)和鏡像(image&#…

matlab界area_Matlab的數據科學界

matlab界area意見 (Opinion) My personal interest in Data Science spans back to 2011. I was learning more about Economies and wanted to experiment with some of the ‘classic’ theories and whilst many of them held ground, at a micro level, many were also pur…

javascript異步_JavaScript異步并在循環中等待

javascript異步Basic async and await is simple. Things get a bit more complicated when you try to use await in loops.基本的async和await很簡單。 當您嘗試在循環中使用await時,事情會變得更加復雜。 In this article, I want to share some gotchas to wat…

白盒測試目錄導航

白盒測試目錄導航(更新中) 2017-12-29 [1] 白盒測試:為什么要做白盒測試 [2] 白盒測試:理論基礎 [3] 白盒測試實戰(上) [4] 白盒測試實戰(中) [5] 白盒測試實戰(下&#…

hdf5文件和csv的區別_使用HDF5文件并創建CSV文件

hdf5文件和csv的區別In my last article, I discussed the steps to download NASA data from GES DISC. The data files downloaded are in the HDF5 format. HDF5 is a file format, a technology, that enables the management of very large data collections. Thus, it is…

CSS仿藝龍首頁鼠標移入圖片放大

CSS仿藝龍首頁鼠標移入圖片放大&#xff0c;效果似乎沒有js好。。。。。。 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>圖片放大</title><style>*{padding:0;margin:0;}body{padding-…

leetcode 224. 基本計算器(棧)

給你一個字符串表達式 s &#xff0c;請你實現一個基本計算器來計算并返回它的值。 示例 1&#xff1a; 輸入&#xff1a;s “1 1” 輸出&#xff1a;2 示例 2&#xff1a; 輸入&#xff1a;s " 2-1 2 " 輸出&#xff1a;3 示例 3&#xff1a; 輸入&#xff…

機械制圖國家標準的繪圖模板_如何使用p5js構建繪圖應用

機械制圖國家標準的繪圖模板The theme for week #5 of the Weekly Coding Challenge is:每周編碼挑戰第5周的主題是&#xff1a; 創建繪圖應用程序 (Creating a Drawing Application) This is the first application that we are building in the #weeklyCodingChallenge prog…

機器學習常用模型:決策樹_fairmodels:讓我們與有偏見的機器學習模型作斗爭

機器學習常用模型:決策樹TL; DR (TL;DR) The R Package fairmodels facilitates bias detection through model visualizations. It implements a few mitigation strategies that could reduce bias. It enables easy to use checks for fairness metrics and comparison betw…

高德地圖如何將比例尺放大到10米?

2019獨角獸企業重金招聘Python工程師標準>>> var map new AMap.Map(container, {resizeEnable: true,expandZoomRange:true,zoom:20,zooms:[3,20],center: [116.397428, 39.90923] }); alert(map.getZoom());http://lbs.amap.com/faq/web/javascript-api/expand-zo…

Android 手把手帶你玩轉自己定義相機

本文已授權微信公眾號《鴻洋》原創首發&#xff0c;轉載請務必注明出處。概述 相機差點兒是每一個APP都要用到的功能&#xff0c;萬一老板讓你定制相機方不方&#xff1f;反正我是有點方。關于相機的兩天奮斗總結免費送給你。Intent intent new Intent(); intent.setAction(M…

如何在JavaScript中克隆數組

JavaScript has many ways to do anything. I’ve written on 10 Ways to Write pipe/compose in JavaScript, and now we’re doing arrays.JavaScript有許多方法可以執行任何操作。 我已經寫了10種用JavaScript編寫管道/組合的方法 &#xff0c;現在我們正在做數組。 1.傳播…

leetcode 227. 基本計算器 II(棧)

給你一個字符串表達式 s &#xff0c;請你實現一個基本計算器來計算并返回它的值。 整數除法僅保留整數部分。 示例 1&#xff1a; 輸入&#xff1a;s “32*2” 輸出&#xff1a;7 解題思路 利用兩個棧&#xff0c;一個記錄操作數&#xff0c;一個記錄操作符&#xff0c;…

100米隊伍,從隊伍后到前_我們的隊伍

100米隊伍,從隊伍后到前The last twelve months have brought us a presidential impeachment trial, the coronavirus pandemic, sweeping racial justice protests triggered by the death of George Floyd, and a critical presidential election. News coverage of these e…

idea使用 git 撤銷commit

2019獨角獸企業重金招聘Python工程師標準>>> 填寫commit的id 就可以取消這一次的commit 轉載于:https://my.oschina.net/u/3559695/blog/1596669

ES6標準入門(第二版)pdf

下載地址&#xff1a;網盤下載 內容簡介 ES6&#xff08;又名 ES2105&#xff09;是 JavaScript 語言的新標準&#xff0c;2015 年 6 月正式發布后&#xff0c;得到了迅速推廣&#xff0c;是目前業界超級活躍的計算機語言。《ES6標準入門&#xff08;第2版&#xff09;》…

hexo博客添加暗色模式_我如何向網站添加暗模式

hexo博客添加暗色模式同一個網站&#xff0c;兩種不同的配色方案 (Same website, two different color schemes) Last year I made it a point to redesign my website from scratch. I wanted something simple and minimalist looking that clearly stated what this was — …

leetcode 331. 驗證二叉樹的前序序列化

序列化二叉樹的一種方法是使用前序遍歷。當我們遇到一個非空節點時&#xff0c;我們可以記錄下這個節點的值。如果它是一個空節點&#xff0c;我們可以使用一個標記值記錄&#xff0c;例如 #。_9_/ \3 2/ \ / \4 1 # 6 / \ / \ / \ # # # # # # 例如&#xff0…

mongodb數據可視化_使用MongoDB實時可視化開放數據

mongodb數據可視化Using Python to connect to Taiwan Government PM2.5 open data API, and schedule to update data in real time to MongoDB — Part 2使用Python連接到臺灣政府PM2.5開放數據API&#xff0c;并計劃將數據實時更新到MongoDB —第2部分 目標 (Goal) This ti…

4.kafka的安裝部署

為了安裝過程對一些參數的理解&#xff0c;我先在這里提一下kafka一些重點概念,topic,broker,producer,consumer,message,partition,依賴于zookeeper, kafka是一種消息隊列,他的服務端是由若干個broker組成的&#xff0c;broker會向zookeeper&#xff0c;producer生成者對應一個…