1. 程式人生 > >【Spring】Spring 3.x企業應用開發實戰(6)----BeanFactory

【Spring】Spring 3.x企業應用開發實戰(6)----BeanFactory

BeanFactory是一個類工廠,是類的通用工廠。不多說,直接進入BeanFactory的使用:

首先建一個Bean類,Car.java

package com.techman.reflect;

public class Car 
{
	private String brand;
	private String color;
	private int maxSpeed;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public int getMaxSpeed() {
		return maxSpeed;
	}
	public void setMaxSpeed(int maxSpeed) {
		this.maxSpeed = maxSpeed;
	}
	
	public Car(){
		
	}
	
	public Car(String brand,String color,int maxSpeed){
		this.brand=brand;
		this.color=color;
		this.maxSpeed=maxSpeed;		
	}
	
	public void introduce()
	{
		System.out.println("Brand is "+brand+" Color is "+color+" Max Speed is "+maxSpeed);
	}
}

配置一個beans.xml檔案來指明bean的位置。

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean id="car1" class="com.techman.reflect.Car" p:brand="寶馬X7" p:color="白色" p:maxSpeed="350"/>						
</beans>


然後在類中實現Bean的初始化和呼叫。

BeanFactoryTest.java

package com.techman.resource;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.techman.reflect.Car;

public class BeanFactoryTest 
{
	public static void main(String []args)throws Throwable
	{
		ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
		Resource res=resolver.getResource("classpath:com/techman/resource/beans.xml");
		BeanFactory bf=new XmlBeanFactory(res);
		System.out.println("Init BeanFactory.");
		
		Car car=bf.getBean("car1",Car.class);
		
		car.introduce();
		
		System.out.println("Car Bean is ready for use!");
	}
}


結果為:

Init BeanFactory.
Brand is 寶馬X7 Color is 白色 Max Speed is 350
Car Bean is ready for use!

 

相關推薦

SpringSpring 3.x企業應用開發實戰(6)----BeanFactory

BeanFactory是一個類工廠,是類的通用工廠。不多說,直接進入BeanFactory的使用: 首先建一個Bean類,Car.java package com.techman.reflect

Spring 3 x 企業應用開發實戰 含CD光碟1張

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

學習《spring 3.x企業應用開發實戰》之使用OXM進行物件XML對映

1、認識XML解析技術 1.1、XML相關概念 (1)DTD:XML語法規則,是XML檔案的驗證機制,可以通過比較XML文件和DTD檔案看文件是否符合規範,元素和標籤是否使用正確。 (2)XML是SOA的基礎。 1.2、XML處理技術 (1)為了使用XML,我們需要通過X

學習《spring 3.x企業應用開發實戰》之Spring的事務管理

1、資料庫事務基礎知識 1.1、資料庫事務的概念 (1)資料庫事物的4個特性(ACID): 原子性(Atomic):表示組成一個事務的多個數據庫操作是一個不可分割的原子單元,所有的操作要麼全部成功,要麼全部失敗。一致性(Consistency):事務操作成功之後,資料庫所

《精通Spring+4.x++企業應用開發實戰》讀後感

來講 ring 如果 項目 企業 xml配置 登錄 了解 能夠 引言 還記得大三時上培訓班的是時候,當時的培訓老師說自己是本地講解spring最好的講師,但是後來等我實習了看了《Spring 3.x 企業應用開發實戰》以及後續版本《精通Spring+4.x++企業應用開發實

Spring 入門例項 簡易登入系統(精通Spring+4.x++企業應用開發實戰 學習筆記一)

論壇登入模組 在持久層有兩個DAO類,分別是UserDao和LoginLogDao,在業務層對應一個業務類UserService,在展現層擁有一個LoginController類和兩個JSP頁面,分別是登入頁面login.jsp和登入成功頁面main.js

《精通Spring4.x企業應用開發實戰》第三章

  這一章節主要介紹SpringBoot的使用,也是學習的重點內容,之後就打算用SpringBoot來寫後臺,所以提前看一下還是很有必要的。   3.SpringBoot概況 3.1.1SpringBoot發展背景   Spring Boot的出現。Spring Boot可讓開發人員不再需要編寫複雜的XML配

cocos2dx 3.x C++搭建protobuf環境

person ccf binding csdn bind taf protoc -cp strlen http://blog.csdn.net/ganpengjin1/article/details/50964961 Cocos2dx 裏面在網絡遊戲通信這一塊一般我們都會采

配置SwooleDistributed 3.X框架中利用Nginx對HTTP、Websocket協議進行反向代理

前言 SwooleDistributed 3.X可以同時支援HTTP、Websocket和TCP協議,在配置防火牆規則的情況下可以通過IP:PORT 的方式進行HTTP和Websocket的訪問,但是這樣在生產環境中是不安全的,而且相對來說IP:PORT 的訪

JavaEE經典JAVA EE企業應用實戰-讀書筆記1

今天開始閱讀李剛的書《經典JAVA EE企業應用實戰 基於WebLogic JBoss的JSF+EJB3+JPA整合開發》這本書, 並記錄下讀書筆記,以方便以後參考。 Java EE應用大致可分為如下幾層: 1,Domain Object(領域物件)層:此層由系列的POJ

AndroidGreenDao 3.X 結合Volley以及Gson、ImageLoader實現資料儲存

關於GreenDao的優點已經不用再說了,關於第三方資料庫框架有很多,相對於Android系統本身的SQLite以及其它第三方而言,我感覺GreenDao使用更方便,體積小、速度更快。網上大多是關於GreenDao之前版本的介紹,關於3.0+新版的介紹不多。Gr

分享虹軟人臉識別應用開發過程

趁晚上的時間向大家分享一波乾貨。 虹軟的人臉識別是應用與離線開發的,因為不需要網路,所以它的識別速度較快。好了,廢話不多說,接下來

西安用Leangoo做Scrum敏捷開發實戰課(免費)

帶來 互聯網產品 簽到 class -s 認證 名企 gin light Leangoo誠邀您參加 2017《用leangoo做Scrum敏捷開發》實戰課!在此實戰課上,您不僅可以聽到一線資深敏捷顧問帶來的敏捷落地實踐經驗,還可以和眾多企業同仁共同探討敏捷實踐過程中的酸甜

slighttpd基於lighttpd架構的Server專案實戰(6)—預備知識之Http

接下來,我們開始http部分的開發。在此之前,有必要先學習一下HTTP協議~ 簡介 超文字傳輸協議(Hypertext Transfer Protocol,簡稱HTTP)是應用層協議,是一種請求/響應式的協議,即一個客戶端與伺服器建立連線後,向伺服器傳送

004-spring-data-elasticsearch 3.0.0.0使用-spring-data之定義方法、創建repository實例、從聚合根發布事件

-- ble sch current 4.3 ... reference tex manager 續上文 1.4、定義方法   存儲庫代理有兩種方法可以從方法名稱派生特定於存儲的查詢。它可以直接從方法名稱派生查詢,或者使用手動定義的查詢。可用選項取決於實際store。但

原創Spring Data Redis <=2.0.3反序列化漏洞

反序列化 默認 用戶 字節碼 href http strong comm ons Spring Data Redis隸屬於Spring Data家族, 提供簡單易用的方式來訪問Redis緩存。 Spring Data Redis在往Redis裏面寫數據的時候,默認會先對數據

筆記 Spring Boot [ 3 ] 之命令列啟動方式和啟用不同的配置檔案

命令列啟動方式 在springboot專案的根目錄下執行 mvn spring-boot:run 或 mvn install cd target java -jar xxx.jar

SpringSpring MVC原理及配置詳解

進行 return sub sca scrip uil 線程安全 松耦合 必須 1.Spring MVC概述: Spring MVC是Spring提供的一個強大而靈活的web框架。借助於註解,Spring MVC提供了幾乎是POJO的開發模式,使得控制器的開發和測試更加簡

Spring boot 打成jar包問題總結

alt date function get .post stack 問題 chan property http://www.cnblogs.com/xingzc/p/5972488.html 1、Unable to find a single main class

redisspring boot利用redis的Keyspace Notifications實現消息通知

客戶 無效 handler mage extend width psu 消息通知 queue 前言 需求:當redis中的某個key失效的時候,把失效時的value寫入數據庫。 github: https://github.com/vergilyn/RedisSampl