1. 程式人生 > >Spring中通過實現FactoryBean介面獲取bean例項

Spring中通過實現FactoryBean介面獲取bean例項

1:Car

package com.bean.factorybean;


public class Car {
private String brand;
  private int price;
public Car() {
System.out.println("Car's Constructor...");

}

public Car(String brand, int price) {
this.brand = brand;
this.price = price;
}


public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}


@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
  
}

2:CarFactoryBean

package com.bean.factorybean;


import org.springframework.beans.factory.FactoryBean;
//自定義的FactoryBean,需要實現FactoryBean介面
public class CarFactoryBean implements FactoryBean<Car> {
 private String brand;
 
public void setBrand(String brand) {
this.brand = brand;
}
public Car getObject() throws Exception {
return new Car(brand,5000);
}
/**
 * 返回bean型別
 */
public Class<?> getObjectType() {
return Car.class;
}


public boolean isSingleton() {
return true;
}


}

3:xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 <!-- 通過FactoryBean來配置bean的例項 
   class:指向FactoryBean的全類名
    property: 配置FactoryBean的屬性
    但實際返回的例項卻是FactoryBean的getObject()方法返回的例項
 -->
 <bean id="car" class="com.bean.factorybean.CarFactoryBean">
 <property name="brand" value="BMN"></property>
 </bean>
  </beans>

4:Main

package com.bean.factorybean;


import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.bean.factorybean.Car;


public class Main {
public static void main(String[] args) {
//ApplicationContext子介面是ClassPathXmlApplication(有close方法)
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans-beanfactory.xml");
 //2:從IOC容器中獲取Bean例項
Car car=(Car)ctx.getBean("car");
System.out.println(car);
//關閉IOC容器
ctx.close();
}
}

相關推薦

Spring通過實現FactoryBean介面獲取bean例項

1:Car package com.bean.factorybean; public class Car {private String brand;  private int price;public Car() {System.out.println("Car's Co

spring----通過實現FactoryBean介面來配置Bean----筆記

Bean配置檔案 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="htt

Spring-實現FactoryBean介面的方式配置Bean

•Spring 中有兩種型別的Bean,一種是普通Bean,另一種是工廠Bean,即FactoryBean. •工廠 Bean 跟普通Bean不同,其返回的物件不是指定類的一個例項,其返回的是該工廠B

SpringFactoryBean介面實現FactoryBean介面Spring在初始化bean時有何不同

問題描述: 最近想要再次熟悉一下阿里中介軟體HSF的用法,在消費HSF時需要在Spring的配置檔案中進行如下配置: <bean id="myClassB" class="com.taobao.hsf.app.spring.util.HSFSpri

通過實現ApplicationContextAware介面動態獲取bean

有些場景下我們需要在程式碼中需要動態獲取其它bean,這裡介紹一種簡單實現方式。 實現ApplicationContextAware介面 package org.ricky.spring; imp

spring通過ApplicationContext獲取bean通過bean工廠獲取bean的區別

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

spring 通過ApplicationContext getBean獲取注入物件

用SpringContextUtil實現ApplicationContextAwarepackage util; import java.util.Locale; import org.springf

Spring學習之使用factorybean獲取bean例項

學一個工廠(要實現FactoryBean介面) public class CarFactoryBean implements FactoryBean<Car>{ private String name;@Overridepublic Car getObject(

Spring通過註解來配置bean以及自動注入

今天看到一篇好文章,寫的很是詳細。再加上自己的理解和補充,成了這一篇文章。文後會獻上原文連結。 使用Spring經常性的需要: 通過註解配置bean   基於註解配置bean   基於註解來配置bean的屬性    ----------------------

Spring通過FactoryBean建立複雜物件

public class ConnFactoryBean implements FactoryBean<Connection>{     private String driver;     private String url;     private String username;     

Spring通過變數和import標籤來控制載入哪些bean

  需求:根據設定變數,來載入某個spring的bean的配置檔案,這個配置檔案中,有某些使用的bean。在一些情況下,不希望這些bean被初始化和載入進context中,也不需要被外面訪問到。   在spring中,我們通過placeholder類可以讀取配置檔案,裡面可以設定引數,而在配置檔案或容器中使

spring通過註解配置bean出錯:

推酷網上的這一篇講解的很好:http://www.tuicool.com/articles/riQJzuJ 本人在自己跟著尚矽谷上的spring4.0的視訊課程學習的時候,在13課的時候跟著視訊一行

spring通過配置檔案方式實現定時任務

Spring3.0以後自帶有定時任務的實現功能: 一、修改spring配置檔案的內容:在檔案頭新增名稱空間和描述 <?xmlversion="1.0"encoding="UTF-8"? > <beansxmlns="http://www.springf

通過實現TextWatcher介面獲取RecyclerView item的EditText的值

我們在實際開發中,可能會遇到要在列表裡面放入一個EditText,但是我們在列表所在的介面獲取不到這個EditText的值,所以,這裡我們在Adapter裡面為EditText新增一個addTextChangedListener,並實現TextWatcher介面來進行監聽,

Spring通過構造自動裝配--constructor

getname ted val lan 如果 create 構造方法 detect pub 在Spring中,可以使用“通過構造自動裝配”,實際上是按構造函數的參數類型自動裝配。 這意味著,如果一個bean的數據類型與其他bean的構造器參數的數據類型是相同的,那麽將自動裝

Spring Cloud Zuul 實現路由,獲取參數

esp 參數 實現 bsp pos 舉例 nbsp 傳遞 重寫 Spring Cloud Zuul作為網關,可實現請求驗證過濾,路由到具體服務的作用。 舉例: 比如請求訂單接口,報文包括驗證信息及具體請求參數。 通過配置路由到具體接口,通過參數調用接口,返回具體報文。 參數

Spring AOP:實現Request資訊獲取

關鍵程式碼 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); Http

請問C#通過什麼方法可以獲取到訊息佇列總數

// 指數 -- 當對陣列按各位進行排序時,exp=1;按十位進行排序時,exp=10;... int exp; // 陣列a中的最大值 int max = getMax(arr); // 從個位開始,對陣列a按"指數"進行排序 for (exp = 1;

spring註解版 使用FactoryBean(工廠Bean)註冊元件

一般在容器中註冊元件都使用@Bean或者之前講的@Import,當然還有包掃描+元件標註註解的方法。今天學了一個工廠Bean的方式註冊元件,正好也在學設計模式,研究研究 玩FactoryBean需要搞一個類去實現它,老規矩,類名MyFactoryBean import o

通過QQ音樂介面獲取資料+播放源

//songList.js export function getDiscList() {     const url = '/api/getDiscList'     const data = Object.assign({}, commonParams, {