spring-注解---IOC(3)

? ??spring--注解---IOC(3)

?

?

package com.zwj.bean;public class Blue {public Blue(){System.out.println("blue...constructor");}public void init(){System.out.println("blue...init...");}public void detory(){System.out.println("blue...detory...");}
}
Blue
package com.zwj.bean;import org.springframework.stereotype.Component;@Component
public class Car {public Car(){System.out.println("car constructor...");}public void init(){System.out.println("car ... init...");}public void detory(){System.out.println("car ... detory...");}}
Car
package com.zwj.bean;public class Color {private Car car;public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}@Overridepublic String toString() {return "Color [car=" + car + "]";}}
Color
package com.zwj.bean;import org.springframework.beans.factory.FactoryBean;//創建一個Spring定義的FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {//返回一個Color對象,這個對象會添加到容器中
    @Overridepublic Color getObject() throws Exception {// TODO Auto-generated method stubSystem.out.println("ColorFactoryBean...getObject...");return new Color();}@Overridepublic Class<?> getObjectType() {// TODO Auto-generated method stubreturn Color.class;}//是單例?//true:這個bean是單實例,在容器中保存一份//false:多實例,每次獲取都會創建一個新的bean;
    @Overridepublic boolean isSingleton() {// TODO Auto-generated method stubreturn false;}}
ColorFactoryBean
package com.zwj.bean;public class RainBow {}
RainBow
package com.zwj.bean;public class Yellow {}
Yellow
package com.zwj.condition;import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;import com.zwj.bean.RainBow;public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {/*** AnnotationMetadata:當前類的注解信息* BeanDefinitionRegistry:BeanDefinition注冊類;*         把所有需要添加到容器中的bean;調用*         BeanDefinitionRegistry.registerBeanDefinition手工注冊進來*/@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {boolean definition = registry.containsBeanDefinition("com.zwj.bean.Yellow");boolean definition2 = registry.containsBeanDefinition("com.zwj.bean.Blue");if(definition && definition2){//指定Bean定義信息;(Bean的類型,Bean。。。)RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);//注冊一個Bean,指定bean名registry.registerBeanDefinition("rainBow", beanDefinition);}}}
MyImportBeanDefinitionRegistrar
package com.zwj.condition;import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;//自定義邏輯返回需要導入的組件
public class MyImportSelector implements ImportSelector {//返回值,就是到導入到容器中的組件全類名//AnnotationMetadata:當前標注@Import注解的類的所有注解信息
    @Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {// TODO Auto-generated method stub//importingClassMetadata//方法不要返回null值return new String[]{"com.zwj.bean.Blue","com.zwj.bean.Yellow"};}}
MyImportSelector
package com.zwj.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;import com.zwj.bean.Car;
import com.zwj.bean.Color;
import com.zwj.bean.ColorFactoryBean;
import com.zwj.condition.MyImportBeanDefinitionRegistrar;
import com.zwj.condition.MyImportSelector;@Configuration
@Import({Car.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
//@Import導入組件,id默認是組件的全類名
public class MainConfig2 {/*** 給容器中注冊組件;* 1)、包掃描+組件標注注解(@Controller/@Service/@Repository/@Component)[自己寫的類]* 2)、@Bean[導入的第三方包里面的組件]* 3)、@Import[快速給容器中導入一個組件]*         1)、@Import(要導入到容器中的組件);容器中就會自動注冊這個組件,id默認是全類名*         2)、ImportSelector:返回需要導入的組件的全類名數組;*         3)、ImportBeanDefinitionRegistrar:手動注冊bean到容器中* 4)、使用Spring提供的 FactoryBean(工廠Bean);*         1)、默認獲取到的是工廠bean調用getObject創建的對象*         2)、要獲取工廠Bean本身,我們需要給id前面加一個&*             &colorFactoryBean*/@Beanpublic ColorFactoryBean colorFactoryBean(){return new ColorFactoryBean();}}
MainConfig2
package com.zwj.test;import static org.junit.Assert.*;import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.zwj.bean.Blue;
import com.zwj.config.MainConfig2;public class IOCTest {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);@Testpublic void testImport(){printBeans(applicationContext);Blue bean = (Blue) applicationContext.getBean(Blue.class);System.out.println(bean);//工廠Bean獲取的是調用getObject創建的對象Object bean2 = applicationContext.getBean("colorFactoryBean");Object bean3 = applicationContext.getBean("colorFactoryBean");System.out.println("bean的類型:"+bean2.getClass());System.out.println(bean2 == bean3);Object bean4 = applicationContext.getBean("&colorFactoryBean");System.out.println(bean4.getClass());}private void printBeans(AnnotationConfigApplicationContext applicationContext){String[] definitionNames = applicationContext.getBeanDefinitionNames();for (String name : definitionNames) {System.out.println(name);}}}/*mainConfig2
com.zwj.bean.Car
com.zwj.bean.Blue
com.zwj.bean.Yellow
colorFactoryBean
rainBow
com.zwj.bean.Blue@341b80b2
ColorFactoryBean...getObject...
ColorFactoryBean...getObject...
bean的類型:class com.zwj.bean.Color
false
class com.zwj.bean.ColorFactoryBean*/
IOCTest
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zwj</groupId><artifactId>spring-annotation_IOC</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.12.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies>
</project>
pom.xml

?

轉載于:https://www.cnblogs.com/ou-pc/p/9860691.html

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

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

相關文章

絕對定位的div圖片居中自適應

需求點 固定定位div中添加圖片內容&#xff0c;保證圖片垂直居中&#xff0c;并且自適應。 一般在第三方UI組件中&#xff0c;這種布局需求較為常見 解決方案一 &#xff08;親測有效&#xff09; HTML代碼&#xff1a; <div class"el-carousel__item is-active is…

英語進階系列-A06-本周總結

本周總結 目錄Content 英語進階系列-A01-再別康橋 英語進階系列-A02-英語學習的奧秘 英語進階系列-A03-英語升級練習一 英語進階系列-A04-英語升級練習二 英語進階系列-A05-英語升級練習三 古詩Poem 再別康橋 回鄉偶書 梅花 勸學 游子吟 詞匯Vocabulary be; have; give; get; t…

在div中設置文字與內部div垂直居中

要實現如圖一所示的結果&#xff1a; html代碼如下&#xff1a; <!DOCTYPE html> <html><head lang"zh"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta charset"utf-8" /><title>商…

王之泰201771010131《面向對象程序設計(java)》第九周學習總結

第一部分&#xff1a;理論知識學習部分 第7章異常、日志、斷言和調試 概念&#xff1a;異常、異常類型、異常聲明、異常拋出、 異常捕獲1.異常處理技術2.斷言的概念及使用3.基本的調試技巧 1&#xff09;異常的概念 a.Java的異常處理機制可以控制程序從錯誤產生的 位置轉移到能…

vue移動端UI框架——Vant全局引入vs局部引入

全局引入 1.在main.js中全局引入全部vant組件 優點&#xff1a;可以在所有vue文件的template中定義所需組件缺點&#xff1a;打包發布時會增加包的大小&#xff0c;Vue的SPA首屏打開時本來就有些慢&#xff0c;同時不能在js中使用類似Toast功能的組件 代碼如下&#xff1a; …

大前端完整學習路線(完整版),路線完整版

第一階段&#xff1a; HTMLCSS: HTML進階、CSS進階、divcss布局、HTMLcss整站開發、 JavaScript基礎&#xff1a; Js基礎教程、js內置對象常用方法、常見DOM樹操作大全、ECMAscript、DOM、BOM、定時器和焦點圖。 JS基本特效&#xff1a; 常見特效、例如&#xff1a;tab、…

web-8. 多框架頁面的創建

8. 多框架頁面的創建 8.1 框架概念 框架是由單個框架加上框架集構成的區域。 每個框架是指頁面中一個獨立額區&#xff0c;框架集是一個關于框架結構的頁面&#xff0c;定義本頁面的框架數、大小、布局以及框架之間的相互關系。 8.2 框架集標記 框架集文件保存了所有框架的信息…

匯編語言第二章知識梳理及思考

第二章 寄存器&#xff08;CPU工作原理&#xff09; CPU概述 CPU由運算器、控制器、寄存器等器件組成&#xff0c;這些器件靠內部總線相連。 內部總線實現CPU內部各個器件之間的聯系。 外部總線實現CPU和主板上其他器件的聯系。 寄存器概述 8086CPU有14個寄存器&#xff1a; AX…

前端面試題總結(js、html、小程序、React、ES6、Vue、算法、全棧熱門視頻資源)持續更新

Vue面試題 生命周期函數面試題 1.什么是 vue 生命周期 2.vue生命周期的作用是什么 3.第一次頁面加載會觸發哪幾個鉤子 4.簡述每個周期具體適合哪些場景 5.created和mounted的區別 6.vue獲取數據在哪個周期函數 7.請詳細說下你對vue生命周期的理解&…

Neural Networks and Deep Learning 讀書筆記

1 轉載于:https://www.cnblogs.com/jellyj/p/9867103.html

JS中的數據類型轉換:String轉換成Number的3種方法

今天有個學員問了個關于數據類型轉換的問題&#xff0c;我覺得這個是可以給大家說一下的。 JavaScript中&#xff0c;可以通過以下3種方法來將string值轉換成number&#xff1a; 1.調用Number()來對string進行值類型轉換。 2.parseInt()。 3.parseFloat()。 Number() 使用…

Java學習——使用Static修飾符

程序功能&#xff1a;通過兩個類 StaticDemo、LX4_1 說明靜態變量/方法與實例變量/方法的區別。 package Pack1;public class Try {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("靜態變量x"StaticDemo.getX());非…

JavaScript從入門到精通之入門篇(一)概念與語法

入門篇大綱 第一部分 概念與語法 1.JavaScript的歷史 2.基本概念 3.JavaScript的使用、調試和異常處理 4.基本詞法和變量 5.數據類型和類型轉換 6.運算符 算數運算符 賦值運算符 一元運算符 使用一元運算符&#xff0c;將會把所有的內容轉換為數值運算&#xff0c;不…

【小記】-005--純CSS實現的小玩意兒

效果圖奉上 代碼奉上 <!DOCTYPE html> <html lang"zh"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><meta http-equiv"X-UA-Compatible&q…

JavaScript從入門到精通之入門篇(二)函數和數組

入門篇大綱 第二部分 函數與數組 1.函數 函數的定義 普通函數 function 函數名 &#xff08;表達式1…&#xff09; { 代碼塊 }js是解釋性語言&#xff0c;在當前script標簽代碼執行的開始階段&#xff0c;就會將普通函數放入堆中&#xff0c;也只是將引用放入堆中&#xf…

leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

557. Reverse Words in a String III 最簡單的把空白之間的詞反轉 class Solution { public:string reverseWords(string s) {vector<int> blank;for(int i 0;i < s.size();i){if(s[i] )blank.push_back(i);}int start 0;int end;for(int i 0;i < blank.size(…

elementUI vue table status的狀態列顏色變化和操作列狀態顯示(停用, 啟用)

<div id"app" style"display: none">...<el-table-column prop"status" label"狀態" width"80" align"center"><template scope"scope"><span v-if"scope.row.status0"…

一道Python面試題

無意間&#xff0c;看到這么一道Python面試題&#xff1a;以下代碼將輸出什么&#xff1f; def testFun(): temp [lambda x : i*x for i in range(4)] return temp for everyLambda in testFun(): print (everyLambda(2))腦中默默一想&#xff0c;這還用說么&#xff0c;肯定是…

Windows下的ssh姐妹花 Xshell 和 Xftp

Windows下的ssh姐妹花 Xshell 和 Xftp 今天是3月8號&#xff0c;中國傳統的三八婦女節&#xff0c;是距離中國新興節日三七女生&#xff08;神&#xff09;節最近的一個全國性節日&#xff0c;今天我也是宅在家&#xff0c;研究了一下近日工作上遇到的一個純軟件技術問題。廢話…

關于數字證書理解的簡單整理以及12306站點證書簡單分析

版權聲明&#xff1a;本文為博主原創文章&#xff0c;未經博主允許不得轉載。 https://blog.csdn.net/sundacheng1989/article/details/25540601 首先簡單理解一下什么是數字證書。這里是一篇英文文檔&#xff0c;描寫敘述的非常形象。形象的描寫敘述了什么是公鑰。什么是私鑰。…