1. 程式人生 > >spring構建bean的三種方法

spring構建bean的三種方法

1.通過類的構造方法構建物件
2.通過靜態工廠方法構建物件
3.通過動態工廠方法構建物件

Spring中的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"
>
<!-- 直接通過類的構建方法構建物件 class為包名+類名--> <bean id="date" class="java.util.Date"/> <!-- 通過類的靜態工廠方法建立物件 --> <!--用於此類不能被直接例項化時,一般為抽象類--> <!--factory-method中是此類的靜態方法,通過此靜態方法返回的物件獲得一個物件--> <bean id="calendar" class="java.util.Calendar"
factory-method="getInstance" />
<!-- 通過Calendar物件的getTime方法獲取date物件 動態工廠方法 動態 --> <bean id="time" factory-bean="calendar" factory-method="getTime"/> <!--注意:動態工廠方法比靜態工廠方法多個factory-bean標籤,這個標籤指的是需要的那個bean物件的id值,用這個bean建立按另一個bean的物件--> </beans>

相關推薦

spring 注入的方法

  自己總結下  比較淺的 理解 適合 初學者:   1.構造方法 注入          直接上程式碼   <!-- 構造方法注入 --> <bean id="testservice" class="com.javen.service.im

spring構建bean方法

1.通過類的構造方法構建物件 2.通過靜態工廠方法構建物件 3.通過動態工廠方法構建物件 Spring中的配置 <?xml version="1.0" encoding="

SpringBean初始化的方法

常用的設定方式有以下三種: 通過實現 InitializingBean/DisposableBean 介面來定製初始化之後/銷燬之前的操作方法; 通過 <bean> 元素的 init-method/destroy-method屬性指定初始化之後 /銷燬之前呼叫的

spring屬性的註入方法

his ring prop pos string 名稱 BE per 構造方法 (1)使用set方法: public class Book { private String bookname;public void setBookname(String bookname)

spring與mybatis整合方法

代碼 pos location etc select標簽 ns-3 tool 一個 tro 1、采用MapperScannerConfigurer,它將會查找類路徑下的映射器並自動將它們創建成MapperFactoryBean。spring-mybatis.xml: &

Spring Boot():RestTemplate提交表單資料的方法

在REST介面的設計中,利用RestTemplate進行介面測試是種常見的方法,但在使用過程中,由於其方法引數眾多,很多同學又混淆了表單提交與Payload提交方式的差別,而且介面設計與傳統的瀏覽器使用的提交方式又有差異,經常出現各種各樣的錯誤,如405錯誤,或

JNDI+Spring獲取配置資料來源的方法

一 環境:XP+Myeclipse6.6+Tomcat7+JDK1.6 二 具體步驟如下: bean.xml中配置:  Java程式碼   <bean id="dataSource" class="org.springframework.jndi.JndiO

Spring建立物件的方法之一建構函式建立(原始碼)

Spring建立物件有三種方法,分別是: 1、建構函式建立 2、靜態工廠方法 3、例項工廠方法 這裡說下第一種方法,採用建構函式來建立,我這裡直接給原始碼,湊合著看看,能用就可以了,如果想要更深入的瞭解,那麼只有自己去找資料了。 第一個類:D1.java pa

Spring接收前臺表單資料的方法

一、 提交方式表單可以通過get/post介面提交,在RequestMapping中不指定method那麼get/post都可以訪問到,指定method=RequestMethod.POST則只能通過post方式訪問。二、Controller層獲取表單資料的三種方式1. 在方

spring注入靜態變數的方法及其注意事項

Spring靜態注入的三種方式:(說明:MongoFileOperationUtil是自己封裝的一個Mongodb檔案讀寫工具類,裡面需要依賴AdvancedDatastore物件例項,dsForRW用來獲取Mongodb資料來源)在springframework裡,我們不能

Spring Boot(二):從Session中獲取域物件的方法

1. 利用@SessionAttribute進行獲取 這是最簡單直接的方法,可以直接在方法的引數中直接進行注入,如下: // 可以直接從Session中抓取域物件 public Account login(@SessionAttribute Accou

Spring: RestTemplate提交表單資料的方法

1. 用exchange方法提交 exchange既可以執行POST方法,還可以執行GET,所以應用最為廣泛,使用方法如下: String url = "http://localhost/mirana-ee/app/login"; RestTemplate client = new RestTe

JMeter中引入外部的JAR包給bean shell 使用(提供方法

在JMeter中在bean shell中可能要引用外部的JAR包,這裡給出三種方法。 方法一:在測試計劃中,通過瀏覽按鈕,將需要匯入的jar包引入 方法二:直接將需要引用的jar包放置到JMete

spring框架入門--方式配置Bean

spring框架的核心在於“Ioc控制反轉”“DI依賴注入”以及“AOP面向切面程式設計”,所以在實現這些核心是最基礎的一步即為在ioc容器中配置bean標籤,因為我們獲取物件是通過獲取bean 來獲得的,所以配置好bean,要用物件時我們才能想用就拿.今天查看

bean例項化的方法

這篇文章總結了bean例項化方法Spring中bean例項方法有三種使用構造方法例項化bean使用靜態工廠例項化bean使用例項工廠例項化bean下面分別介紹這三種例項化bean方法具體操作。在介紹在三種建立bean方法前,我們先建立一個User類,我們通過建立User物件來

獲取Spring上下文(ApplicationContext)的方法

 以前在專案中經常用到Spring上下文(ApplicationContext),每次使用都是百度一下,使用過就忘了。今天良心發現,寫一篇部落格,讓這個知識真正屬於我,也希望我寫的博文,可以幫助需要的人。       Spring上下文(ApplicationContext)

應對加密js的方法

-a logging ember 瀏覽器 ebp gecko afa 方法 trace 經常遇到網頁在登錄後會對用戶輸入的帳號和密碼通過js進行加密,導致模擬登錄這類網站時受到阻礙 這裏小記一下當前解決該問題的三種方法 1.利用python實現js同等加密。 2.利用sel

c# pictureBox1.Image的獲得圖片路徑的方法 winform

相對 body req () ebr art www ref clas 代碼如下:c# pictureBox1.Image的獲得圖片路徑的三種方法 winform 1.絕對路徑:this.pictureBox2.Image=Image.FromFile("D:\\00

前端下載excel文件功能的方法

light nbsp html github 支持 返回 但是 inpu brush 1 從後端接收json數據,前端處理生成excel下載 JsonExportExcel的github地址:https://github.com/cuikangjie/JsonExpor

從U盤運行Linux操作系統的方法

attach 損壞 seo 系統 alt ubunt 沒有 linux操作 ash 摘要: 從U盤運行Linux操作系統的三種方法 usb_linux_0 你或許聽說過在U盤上運行live Linux操作系統,但你知不知道可以永久的保存運行時的數據,或者直接將Linux安裝