日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring boot: spring Aware的目的是为了让Bean获得Spring容器的服务

發布時間:2023/11/29 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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();}}

  

?

總結

以上是生活随笔為你收集整理的spring boot: spring Aware的目的是为了让Bean获得Spring容器的服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。