日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring---基于Spring IOC的小程序

發布時間:2023/12/13 javascript 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring---基于Spring IOC的小程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現的功能以及各文件間的關系

IHelloMessage:一個接口,用于定義輸出問候信息。

HelloWorld、HelloChina:接口的實現類。在這里表示人在不同的地方

Person:一個人物類,調用IHelloMessage接口,向用戶輸出問候信息

Main:程序的入口類,用于加載配置文件以及啟動IOC容器,調用人物類,向用戶輸出問候信息

IHelloMessage

public interface IhelloMessage {public String sayHello();public String say();}

?

HelloWorld、HelloChina

public class HelloWord implements IhelloMessage {@Overridepublic String sayHello() {// TODO Auto-generated method stubreturn "Hello World";}@Overridepublic String say() {// TODO Auto-generated method stubreturn "ceshi";}}

?

Person

public class Person {private IhelloMessage helloMessage;public void setHelloMessage(IhelloMessage helloMessage) {this.helloMessage = helloMessage;}public IhelloMessage getHelloMessage() {return helloMessage;}public String sayHello() {return helloMessage.say();}}

?

Main

public class Main {public static void main(String[] args) {// 讀取配置文件Resource resource = new FileSystemResource("helloMessage.xml");// 加載并啟動IOC容器BeanFactory factory = new XmlBeanFactory(resource);// 從容器中獲取實例Person person = (Person) factory.getBean("person");String str = person.sayHello();System.out.println(str);}}

?

spring3.1以上的版本中XmlBeanFactory已過時,有兩種解決方案

1.將XmlBeanFactory替換成DefaultListableBeanFactory和XmlBeanDefinitionReader,代碼如下Main類所寫

public class Main {public static void main(String[] args) {// 讀取配置文件Resource resource = new FileSystemResource("helloMessage.xml");// 構造工廠DefaultListableBeanFactory factory= new DefaultListableBeanFactory ();// 通過構造工廠得到加載并啟動IOC容器XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);// 將讀取到的配置文件加載到容器中 reader.loadBeanDefinitions(resource);// 從容器中獲取實例Person person = (Person) factory.getBean("person");String str = person.sayHello();System.out.println(str);}}

?2.使用其它幾種方式加載配置文件。詳見Spring---加載配置文件的幾種方法(org.springframework.beans.factory.BeanDefinitionStoreException)

配置文件xml

Java bean

每個bean對應一個java文件,當這個類文件配置到spring容器當中后,就變成了一個一個小豆子,而每個小豆子都擁有自己獨立的功能。在運行spring工程之前,我們需要做的有兩件事情。第一,將這些java類以java bean的形式注冊到spring容器中。第二,通過配置文件來配置java bean之間的依賴關系。各個單獨的類(組件)只需要關心自己的核心業務邏輯,而它們之間的依賴關系,則交由spring容器來配置和管理,這就是控制反轉,而相對于類(組件),我們則可以稱為依賴注入。以下是配置文件的示例代碼

<?xml version="1.0" encoding="UTF-8"?> <!-- 定義使用哪種規范進行解析配置文件 --> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans><!-- bean:定義java bean --><bean id = "helloWorld" class = "com.hudongwu.spring.chapter01.HelloWord"></bean><bean id = "helloChina" class = "com.hudongwu.spring.chapter01.HelloChina"></bean><bean id = "person" class = "com.hudongwu.spring.chapter01.Person"><!-- 與其它java bean建立依賴關系 --><property name="helloMessage" ref="helloWorld"></property></bean> </beans>

?

轉載于:https://www.cnblogs.com/xiaobaizhiqian/p/7746340.html

總結

以上是生活随笔為你收集整理的Spring---基于Spring IOC的小程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。