午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

Springboot在有參構造方法類中使用@Value注解取值

瀏覽:20日期:2023-05-08 15:55:09

我們在Springboot中經常使用@Value注解來獲取配置文件中的值,像下面這樣

@Componentclass A { @Value('${user.value}') private String configValue; public void test() { System.out.println(configValue); }}

但有時我們需要這個類擁有一個有參的構造方法,比如

@Componentclass A { @Value('${user.value}') private String configValue; private String s; public A(String s) { this.s = s; } public void test() { System.out.println(s); System.out.println(configValue); }}

要使@Value生效,必須把Bean交給Spring進行管理,而不能使用new去實例化對象,否則@Value取值為NULL。我們一般使用@Autowired都是默認注入無參的構造方法,要想注入有參的構造方法,我們需要構建Config類:

@Configurationpublic class AConfig { @Bean(name='abc') DataOpration abcA() { return new A('abc'); }}

然后創建SpringUtil類

@Componentpublic class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null) { SpringUtil.applicationContext = applicationContext; } } public static ApplicationContext getApplicationContext() { return applicationContext; } //通過name獲取 Bean. public static Object getBean(String name){ return getApplicationContext().getBean(name); }}

在調用時,只需要獲取到對應的Bean

A a = (A) SpringUtil.getBean('abc');a.test();

就可以同時獲取到配置文件中的值和傳入的參數。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 男人的天堂欧美 | 亚洲最大成人av | 欧美日韩系列 | 成人免费午夜视频 | 久久大胆 | 三年中文免费观看大全动漫 | 亚洲视频黄 | 懂色av一区二区夜夜嗨 | 丁香在线视频 | 亚洲青草视频 | 小罗莉极品一线天在线 | 国产精久久一区二区三区 | 99热在线观看 | 亚洲色图欧美在线 | www午夜| 亚洲国产123| 有色视频在线观看 | 国产999精品| 欧美精品黑人猛交高潮 | 亚洲香蕉av | 成人一区二区视频 | 黄色一级视频免费看 | 亚洲第一免费播放区 | 国产高清在线 | 麻豆av免费看 | 亚洲午夜av | 亚洲成在线 | 自拍亚洲欧美 | 麻豆国产原创 | 久久久综合| av中字在线| 国产一区二区自拍视频 | 日韩综合久久 | 玖玖玖视频 | 日韩午夜精品 | 欧洲久久久久 | 欧美成人综合 | 亚洲天堂精品在线 | 中文字幕在线看 | 久久无| 亚洲碰碰 |