Spring中基于Java的配置@Configuration和@Bean用法

前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。

Spring中為了減少xml中配置,可以聲明一個配置類(例如SpringConfig)來對bean進行配置。

一、首先,需要xml中進行少量的配置來啟動Java配置:

?

[java]?view plain?copy
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:aop="http://www.springframework.org/schema/aop"??
  4. ????xmlns:tx="http://www.springframework.org/schema/tx"?xmlns:p="http://www.springframework.org/schema/p"??
  5. ????xmlns:context="http://www.springframework.org/schema/context"??
  6. ????xsi:schemaLocation="??
  7. ????????????http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.2.xsd??
  8. ????????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-3.2.xsd??
  9. ????????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-3.2.xsd??
  10. ????????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsd">??
  11. ???<context:component-scan?base-package="SpringStudy.Model">??
  12. ????</context:component-scan>??
  13. </beans>??


二、定義一個配置類

?

用@Configuration注解該類,等價 與XML中配置beans;用@Bean標注方法等價于XML中配置bean。

代碼如下:

?

[java]?view plain?copy
  1. package?SpringStudy;??
  2. import?org.springframework.context.annotation.Bean;??
  3. import?org.springframework.context.annotation.Configuration;??
  4. import?SpringStudy.Model.Counter;??
  5. import?SpringStudy.Model.Piano;??
  6. ??
  7. @Configuration??
  8. public?class?SpringConfig?{??
  9. ??
  10. ????@Bean??
  11. ????public?Piano?piano(){??
  12. ????????return?new?Piano();??
  13. ????}??
  14. ????@Bean(name?=?"counter")???
  15. ????public?Counter?counter(){??
  16. ????????return??new?Counter(12,"Shake?it?Off",piano());??
  17. ????}??
  18. }??

三、基礎類代碼

?

Counter:

?

[java]?view plain?copy
  1. package?SpringStudy.Model;??
  2. ??
  3. public?class?Counter?{??
  4. ????public??Counter()?{??
  5. ????}??
  6. ??
  7. ????public??Counter(double?multiplier,?String?song,Instrument?instrument)?{??
  8. ????????this.multiplier?=?multiplier;??
  9. ????????this.song?=?song;??
  10. ????????this.instrument=instrument;??
  11. ????}??
  12. ??
  13. ????private?double?multiplier;??
  14. ??
  15. ????private?String?song;??
  16. ??
  17. ????@Resource??
  18. ????private?Instrument?instrument;??
  19. ??
  20. ????public?double?getMultiplier()?{??
  21. ????????return?multiplier;??
  22. ????}??
  23. ??
  24. ????public?void?setMultiplier(double?multiplier)?{??
  25. ????????this.multiplier?=?multiplier;??
  26. ????}??
  27. ??
  28. ??
  29. ????public?String?getSong()?{??
  30. ????????return?song;??
  31. ????}??
  32. ??
  33. ????public?void?setSong(String?song)?{??
  34. ????????this.song?=?song;??
  35. ????}??
  36. ??
  37. ????public?Instrument?getInstrument()?{??
  38. ????????return?instrument;??
  39. ????}??
  40. ??
  41. ????public?void?setInstrument(Instrument?instrument)?{??
  42. ????????this.instrument?=?instrument;??
  43. ????}??
  44. ??
  45. }??

Piano類

?

[java]?view plain?copy
  1. package?SpringStudy.Model;??
  2. ??
  3. ??
  4. public?class?Piano?{??
  5. ????private?String?name="Piano";??
  6. ????private?String?sound;??
  7. ??
  8. ????public?String?getName()?{??
  9. ????????return?name;??
  10. ????}??
  11. ??
  12. ????public?void?setName(String?name)?{??
  13. ????????this.name?=?name;??
  14. ????}??
  15. ??
  16. ????public?String?getSound()?{??
  17. ????????return?sound;??
  18. ????}??
  19. ??
  20. ????public?void?setSound(String?sound)?{??
  21. ????????this.sound?=?sound;??
  22. ????}??
  23. ??
  24. }??


四、調用測試類

?

?

[java]?view plain?copy
  1. package?webMyBatis;??
  2. ??
  3. import?org.springframework.context.ApplicationContext;??
  4. import?org.springframework.context.annotation.AnnotationConfigApplicationContext;??
  5. import?SpringStudy.Model.Counter;??
  6. ??
  7. public?class?SpringTest?{??
  8. ????public?static?void?main(String[]?args)?{??
  9. ????????//ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext("spring/bean.xml");//?讀取bean.xml中的內容??
  10. ????????ApplicationContext?annotationContext?=?new?AnnotationConfigApplicationContext("SpringStudy");??
  11. ????????Counter?c?=?annotationContext.getBean("counter",?Counter.class);//?創建bean的引用對象??
  12. ????????System.out.println(c.getMultiplier());??
  13. ????????System.out.println(c.isEquals());??
  14. ????????System.out.println(c.getSong());??
  15. ????????????System.out.println(c.getInstrument().getName());??
  16. ????}??
  17. }??

注意:如果是在xml中配置beans和bean的話,或者使用自動掃描調用的話,代碼為

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 讀取bean.xml中的內容
Counter c = ctx.getBean("counter", Counter.class);// 創建bean的引用對象

五、運行結果

12.0
false
Shake it Off
Piano

?

?

見: http://blog.csdn.net/vvhesj/article/details/47661001

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

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

相關文章

【實數二分/前綴和維護】Best Cow Fences

Poj 2018 Best Cow Fences 實數二分前綴和維護 調了一晚上&#xff0c; 但發現沒什么注意事項orz 無輸出只因eps定義成了int型QAQ哭唧唧 #include<cstdio> #include<iostream> using namespace std; const int sz 100010; double eps 1e-5; int n, f; double a[s…

json回顯

第一種&#xff1a;用json的oData塞值 案例顯示&#xff1a; list頁面url帶值 //活動基本信息修改 function updateProject() {   var selectRow $$.getSingleSelectRow(listId, "請選擇你要操作的項目進行修改&#xff01;");   if(selectRow.activity_status&…

NoSuchElementException

在之前下項目的時候遇到這個.NoSuchElementException異常,當時我寫到一個大類的結尾。但是編譯器。從未報錯。然而在運行的時候出現了這樣的異常&#xff0c;非常頭疼 &#xff0c;一到運行時候就報異常&#xff0c;我就上網搜索了一下&#xff0c;才明白&#xff0c;這是我的剛…

東方程序員怎么看西方程序員

摘要&#xff1a;東方程序員與西方程序員&#xff0c;彼此心中是什么樣子呢&#xff1f;本文收集了東西方程序員對彼此的看法與各種印象&#xff0c;對于西方/東方程序員&#xff0c;你留有什么印象呢&#xff1f; 本文是作者根據StackExchange上的一個討論貼&#xff1a;東方程…

Android開發 - 掌握ConstraintLayout(一)傳統布局的問題

在傳統的Android開發中&#xff0c;頁面布局占用了我們很多的開發時間&#xff0c;而且面對復雜頁面的時候&#xff0c;傳統的一些布局會顯得非常復雜&#xff0c;每種布局都有特定的應用場景&#xff0c;我們通常需要各種布局結合起來使用來實現復雜的頁面。隨著ConstraintLay…

輸入流與輸出流的區別

stream結尾都是字節流&#xff0c;reader和writer結尾都是字符流兩者的區別就是讀寫的時候一個是按字節讀寫&#xff0c;一個是按字符。實際使用通常差不多。在讀寫文件需要對內容按行處理&#xff0c;比如比較特定字符&#xff0c;處理某一行數據的時候一般會選擇字符流。只是…

【Spring】Spring高級話題-@Enable***注解的工作原理

EnableAspectJAutoProxy 前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 EnableAspectJAutoProxy注解 激活Aspect自動代理 <aop:aspectj-autoproxy/> 1 開啟對AspectJ自動代理的支…

IDEA項目找不到瀏覽器報錯的情況

調tomcat的時候&#xff0c;它會調用瀏覽器&#xff0c;瀏覽器關聯如果有問題&#xff0c;肯定是會報錯的 要是測試的時候&#xff0c;就是瀏覽器的問題&#xff0c;重新把瀏覽器裝一遍讓他自己重新關聯一下應該就行了轉載于:https://www.cnblogs.com/Koma-vv/p/10156478.html

c/c++經典面試試題及標準答案

下面的問題我想大部分c/c 程序員 都遇見過.如果沒有看過&#xff0c;草根IT特別推薦一下。 一、請填寫BOOL , float, 指針變量與“零值”比較的 if 語句。&#xff08;10分&#xff09;請寫出 BOOL flag 與“零值”比較的 if 語句。&#xff08;3分&#xff09;標準答案&#x…

微服務拆分

微服務拆分是做微服務架構很重要也很難的話題&#xff0c;很多時候&#xff0c;幾個服務是合還是拆在設計團隊內也很難達成共識。 當你糾結應該拆分和合并時我建議就先合并&#xff0c;等后面版本迭代需要時有必要再去做拆分。從系統發展的角度說&#xff0c;很多平臺也都是從單…

oracle數據庫學習筆記

字符函數是ORACLE中最常用的函數: Lower(char):將字符串轉化為小寫格式 Upper(char):將字符轉化為大寫的格式 Length(char):返回字符串的長度 Substr(char,m,n):取字符串的子串 Trim,Ltrim,Rtrim:去掉空格 dual 虛表 當沒有表可以用的時候 就用虛表 as 當做 可以理解為別…

并發編程-concurrent指南-線程池ExecutorService的使用

有幾種不同的方式來將任務委托給 ExecutorService 去執行&#xff1a; execute(Runnable)submit(Runnable)submit(Callable)invokeAny(…)invokeAll(…)execute(Runnable) execute(Runnable) 方法要求一個 java.lang.Runnable 對象&#xff0c;然后對它進行異步執行。以下是使用…

怎樣去理解@ComponentScan注解

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 怎么樣去理解它呢&#xff1f; 1.配置視圖控制器 [java] view plain copy package com.apress.prospringmvc.bookstore.web.config; imp…

oracle 如何創建序列squence

create sequence 序列名 start with 1 increment by 1 nomaxvalue nominvalue nocycle nocache;

如何高效、可移植申請內存代碼。

在視頻編解碼中&#xff0c;如何申請char mem_2D[1920][1080], char mem_3D[4][1920][1080], char mem_4D[6][4][1920][1080]&#xff0c;高效 又 可移植申請內存呢&#xff1f; 請看如下代碼&#xff1a; 看完后&#xff0c;如要申請的是 int &#xff0c;不是cha…

CSS中的px與物理像素、邏輯像素、1px邊框問題

一直不太清楚CSS中的1px與邏輯像素、物理像素是個什么關系&#xff08;作為一名前端感覺很慚愧 -_-&#xff01;&#xff09;&#xff0c;今天終于花時間徹底弄清楚了&#xff0c;其實弄清楚之后就覺得事情很簡單&#xff0c;但也只有在弄清楚之后&#xff0c;才會覺得簡單&…

平滑數據遷移,不影響服務

為什么80%的碼農都做不了架構師&#xff1f;>>> 轉自&#xff1a;http://www.10tiao.com/html/249/201703/2651959992/1.html 轉載于:https://my.oschina.net/jzgycq/blog/2872104

spring cache相關注解介紹 @Cacheable、@CachePut、@CacheEvict

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 Cacheable是用來聲明方法是可緩存的。將結果存儲到緩存中以便后續使用相同參數調用時不需執行實際的方法。直接從緩存中取值。最簡單的格…

layui 渲染select下拉選項 ,日期控件的用法

最近項目中用到關于layui的前端技術&#xff0c;在使用layui 渲染select option下拉復選框時出現了沒有值渲染的問題&#xff0c;還有使用layui日期的過程 &#xff0c;接下來就一起看看吧。 /** *從后臺渲染字段民族數據/<div class"layui-inline"><labe…

CF1082G Petya and Graph(最小割,最大權閉合子圖)

QWQ嚶嚶嚶 感覺是最水的一道\(G\)題了 順便記錄一下第一次在考場上做出來G qwqqq 題目大意就是說&#xff1a; 給你n個點&#xff0c;m條邊&#xff0c;讓你選出來一些邊&#xff0c;最大化邊權減點權 \(n\le 1000\) QWQ 看完這個題和數據范圍&#xff0c;第一感覺就是網絡流啊…