Spring實戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作示例
本文實例講述了Spring實戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="chinese" class="org.crazyit.app.service.impl.Chinese">
<!-- Spring只要檢測到lookup-method元素,
Spring會自動為該元素的name屬性所指定的方法提供實現(xiàn)體。-->
<lookup-method name="getDog" bean="gunDog"/>
</bean>
<!-- 指定gunDog Bean的作用域為prototype,
希望程序每次使用該Bean時用到的總是不同的實例 -->
<bean id="gunDog" class="org.crazyit.app.service.impl.GunDog"
scope="prototype">
<property name="name" value="旺財"/>
</bean>
</beans>
二 接口
1 Dog
package org.crazyit.app.service;
public interface Dog
{
public String run();
}
2 Person
package org.crazyit.app.service;
public interface Person
{
public void hunt();
}
三 實現(xiàn)類
GunDog
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class GunDog implements Dog
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public String run()
{
return "我是一只叫" + getName()
+ "的獵犬,奔跑迅速..." ;
}
}
Chinese
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public abstract class Chinese implements Person
{
private Dog dog;
// 定義抽象方法,該方法用于獲取被依賴Bean
public abstract Dog getDog();
public void hunt()
{
System.out.println("我?guī)е? + getDog() + "出去打獵");
System.out.println(getDog().run());
}
}
四 測試類
package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class SpringTest
{
public static void main(String[] args)
{
// 以類加載路徑下的beans.xml作為配置文件,創(chuàng)建Spring容器
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
Person p1 = ctx.getBean("chinese" , Person.class);
Person p2 = ctx.getBean("chinese" , Person.class);
// 由于chinese Bean是singleton行為,
// 因此程序兩次獲取的chinese Bean是同一個實例。
System.out.println(p1 == p2);
p1.hunt();
p2.hunt();
}
}
五 測試結(jié)果
true
我?guī)е簅rg.crazyit.app.service.impl.GunDog@69a3d1d出去打獵
我是一只叫旺財?shù)墨C犬,奔跑迅速...
我?guī)е簅rg.crazyit.app.service.impl.GunDog@86be70a出去打獵
我是一只叫旺財?shù)墨C犬,奔跑迅速...
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
- Spring IOC原理補(bǔ)充說明(循環(huán)依賴、Bean作用域等)
- SPRING FRAMEWORK BEAN作用域和生命周期原理解析
- 簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別
- Spring實戰(zhàn)之Bean的作用域request用法分析
- Spring實戰(zhàn)之Bean的作用域singleton和prototype用法分析
- 深入了解Spring中Bean的作用域和生命周期
- 淺談Spring中Bean的作用域、生命周期
- spring ioc的簡單實例及bean的作用域?qū)傩越馕?/a>
- 淺談spring中scope作用域
- 淺談Spring學(xué)習(xí)之request,session與globalSession作用域
- JSP 中Spring Bean 的作用域詳解
- 詳解Spring中Bean的生命周期和作用域及實現(xiàn)方式
- 最全總結(jié)SpringBean的作用域管理
相關(guān)文章
Spring Boot利用@Async異步調(diào)用:ThreadPoolTaskScheduler線程池的優(yōu)雅關(guān)閉詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async異步調(diào)用:ThreadPoolTaskScheduler線程池的優(yōu)雅關(guān)閉的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
idea根據(jù)實體類生成數(shù)據(jù)庫表的流程步驟
在開發(fā)的時候,經(jīng)常會遇到數(shù)據(jù)庫表結(jié)構(gòu)設(shè)計無法滿足業(yè)務(wù)的需求,需要去改動數(shù)據(jù)庫表,我們就需要去設(shè)計數(shù)據(jù)庫的字段,然后又回來增加實體類里的字段,這樣很麻煩,所以本文給大家介紹了idea根據(jù)實體類生成數(shù)據(jù)庫表的流程步驟,需要的朋友可以參考下2024-12-12
SpringMVC日期類型參數(shù)傳遞實現(xiàn)步驟講解
這篇文章主要介紹了SpringMVC日期類型參數(shù)傳遞實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
Java從單體架構(gòu)升級到微服務(wù)要注意的一些問題
這篇文章主要介紹了Java從單體架構(gòu)升級到微服務(wù)要注意的一些問題,對架構(gòu)感興趣的同學(xué),可以參考下2021-04-04

