1. 程式人生 > >java建立bean並註冊到spring中

java建立bean並註冊到spring中

從Spring 3.0開始,增加了一種新的途徑來配置Bean Definition,這就是通過Java Code配置Bean Definition。

       與XML和Annotation兩種配置方式不同點在於:

    前兩種方式XML和Annotation的配置方式為預定義方式,即開發人員通過XML檔案或者Annotation預定義配置Bean的各種屬性後,啟動Spring容器,Spring容器會首先解析這些配置屬性,生成對應的Bean Definition,裝入到DefaultListtableBeanFactory物件的屬性容器中,以此同時,Spring框架也會定義內部使用的Bean定義,如Bean名為:org.springframework.context.annotation.internalConfigurationAnnotationProcessor”的 ConfigurationClassPostProcessor 定義。而後此刻不會做任何Bean Definition的解析動作,Spring框架會根據前兩種配置,過濾出BeanDefinitionRegistryPostProcessor 型別的Bean定義,並通過Spring框架生成對應的Bean物件(如 ConfigurationClassPostProcessor 例項)。。結合 Spring 上下文原始碼可知這個物件是一個 processor 型別工具類,Spring 容器會在例項化開發人員所定義的 Bean 前先呼叫該 processor 的 postProcessBeanDefinitionRegistry(…) 方法。此處實現基於 Java Code 配置Bean Definition的處理。

     基於 Java Code 的配置方式,其執行原理不同於前兩種。它是在 Spring 框架已經解析了基於 XML 和 Annotation 配置後,通過加入 BeanDefinitionRegistryPostProcessor 型別的 processor 來處理配置資訊,讓開發人員通過 Java 程式設計方式定義一個 Java 物件。其優點在於可以將配置資訊集中在一定數量的 Java 物件中,同時通過 Java 程式設計方式,比基於 Annotation 方式具有更高的靈活性。並且該配置方式給開發人員提供了一種非常好的範例來增加使用者自定義的解析工具類。其主要缺點在於與 Java 程式碼結合緊密,配置資訊的改變需要重新編譯 Java 程式碼,另外這是一種新引入的解析方式,需要一定的學習成本。

提及一點的就是,Spring框架有3個主要的Hook類,分別是:

org.springframework.context.ApplicationContextAware 

它的setApplicationContext 方法將在Spring啟動之前第一個被呼叫。我們用來同時啟動Jdon框架。

org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor 

它的postProcessBeanDefinitionRegistry 和 postProcessBeanFactory 方法是第二和第三被呼叫,它們在Bean初始化建立之前啟動,如果Spring的bean需要的其他第三方中的元件,我們在這裡將其注入給Spring。

org.springframework.context.ApplicationListener 

用於在初始化完成後做一些事情,當Spring所有XML或元註解的Bean都啟動被建立成功了,這時會呼叫它的唯一方法onApplicationEvent。

下面我們來完成一個,自己通過java程式碼建立bean,並註冊為Spring管理。 

<!--[if !supportLineBreakNewLine]-->

<!--[endif]-->

本例中,我們建立一個介面,然後建立該介面的2個實現類,分別命名不同的名字,然後在需要注入的地方使用@Qualifier 指定注入對應的例項。

介面com.kfit.demo.Shanhy.java

1 2 3 4 5 6 7 package com.kfit.demo; publicinterface Shanhy { publicvoid dispaly(); }

實現類com.kfit.demo.ShanhyA.java

1 2 3 4 5 6 7 8 9 package com.kfit.demo; publicclass ShanhyA implements Shanhy{ @Override publicvoid dispaly() { System.out.println("ShanhyA.dispaly()"); } }

實現類com.kfit.ShanhyB.java

1 2 3 4 5 6 7 8 9 10 package com.kfit.demo; publicclass ShanhyB implements Shanhy { @Override publicvoid dispaly() { System.out.println("ShanhyB.dispaly()"); } }

定義介面BeanDefinitionRegistryPostProcessor的實現:

com.kfit.config.MyBeanDefinitionRegistryPostProcessor:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 package com.kfit.config; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.context.annotation.AnnotationBeanNameGenerator; import org.springframework.context.annotation.Configuration; import com.kfit.demo.ShanhyA; import com.kfit.demo.ShanhyB; /** * 實現自己例項化bean並註冊為Spring管理 * @version v.0.1 */ @Configuration publicclass MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { //bean 的名稱生成器. private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator(); @Override publicvoid postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
java:讀取修改word的內容儲存。

1.讀取word內容 2.修改要修改的內容 3.儲存word public Map readwriteWord(Map<String, String> map,String gcid,String deptid) { //查詢word模板

C#建立檔案往檔案寫入資訊

string filepath = Server.MapPath("~/Upfile") + "\\";   //要上傳的資料夾的路徑             if (!Directory.Exists(filepath))  //不存在資料夾,建立             {                

【Git、GitHub、GitLab】三 Git基本命令之建立倉庫向倉庫新增檔案

前兩篇文章已經學會了Git的基本命令與建立倉庫的命令,點選連結檢視上一篇文章:【Git、GitHub、GitLab】二 Git基本命令之建立Git倉庫,本篇文章就來建立一個有模有樣的倉庫。該倉庫中的程式碼是一個顯示靜態頁面的小工程程式碼。 文章目錄

定時器的實現、java定時器介紹與Spring定時器的配置

1定時器的作用 在實際的開發中,如果專案中需要定時執行或者需要重複執行一定的工作,定時器顯現的尤為重要。 當然如果我們不瞭解定時器就會用執行緒去實現,例如: package org.lzstone.action public class FinanceAction exte

資料結構:java建立實現增刪改查功能

前言:java實現串的過程中遇到的問題 1、當自己建立seqstring物件然後輸出s時,會出現如下結果 public class seqstring {     private char[]strvalue;     private int curlen;     pu

Spring bean 註冊的原始碼解析

前言 所謂 bean 的註冊,就是把在配置檔案中配置的 <bean> 節點載入配記憶體中,供後續使用。 bean的註冊主要分為兩個階段,一個是準備階段,就是對配置檔案進行解析,把配置檔案載入到記憶體中,以 Document 的形式存放;第二個階段是對 Document 進行操作,獲取其中的節

實現ApplicationContextAware介面,java(new或者java反射獲取的物件)獲取spring容器的bean

本文參考了https://blog.csdn.net/bailinbbc/article/details/76446594,其實是拷貝了很多內容: 在Web應用中,Spring容器通常採用宣告式方式配置產生:開發者只要在web.xml中配置一個Listener,該Listener將會負責初始化S

eclispe建立maven專案使用springjava.lang.ClassNotFoundException: org.springframework.web.filter.Charact

報錯如下: 資訊: Starting Servlet Engine: Apache Tomcat/7.0.57 九月 24, 2018 6:44:04 下午 org.apache.catalina.util.SessionIdGenerator createSecureRa

Java Web】SpringBean的使用

Bean的定義 被稱作 bean 的物件是構成應用程式的支柱,其也由 Spring IoC 容器管理的。bean 是一個被例項化、組裝、並通過 Spring IoC 容器所管理的物件。這些 bean 是由用容器提供的配置元資料建立的,例如,已經在先前章節看到的,在 XML 的表單中的 定

Java如何獲取Spring配置的bean

Spring中的ApplicationContexts可以被限制在不同的作用域。在web框架中,每個DispatcherServlet有它自己的WebApplicationContext,它包含了DispatcherServlet配置所需要的bean。DispatcherServlet 使用的預設BeanF

java陣列練習2建立二維陣列,將陣列的內容輸出到控制檯

@author HP-Developer * 8-31號的陣列練習 * 實驗任務 建立二維陣列,並將陣列中的內容輸出到控制檯上 實驗要求 建立二維陣列進行學生姓名、學號、Java基礎成績三個資訊的儲存 學生姓

spring獲取spring容器建立bean方式

場景:在spring中獲取spring容器建立的bean方式 public static Object getBean(String beanName) { return ContextLoader.getCurrentWebApplicationContext().getB

通過註解的方式在spring註冊bean

1.首先你得讓spring容器知道你編寫的類是使用了註解方式 在sessionFactory的屬性packageToScan中寫上你的使用了註解類的包名  注意:如果你是使用的list,在value之間一定不要有空格,否者容器不能掃描到類。 2.幾種有必要知道的註解含義 @

spring建立bean物件時多例和單例的區別

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w