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

歡迎訪問 生活随笔!

生活随笔

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

javascript

编写第一个Spring程序——IOC实现

發布時間:2025/3/8 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编写第一个Spring程序——IOC实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一個Spring程序 IOC范例

1、新建maven工程
2、在pom.xml文件中導入相關jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.6.2</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>compile</scope></dependency>
3、在Package【pojo】下新建一個【Source】類:
package pojo;public class Source { private String fruit; // 類型private String sugar; // 糖分描述private String size; // 大小杯 /* setter and getter */ }
4、在resources目錄下新建一個 applicationContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean name="source" class="pojo.Source"><property name="fruit" value="橙子"/><property name="sugar" value="多糖"/><property name="size" value="超大杯"/></bean> </beans>
5、在 Packge【test】下新建一個【TestSpring】類:
package test;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import pojo.Source;public class TestSpring {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"./applicationContext.xml"});Source source = (Source) context.getBean("source");System.out.println(source.getFruit());System.out.println(source.getSugar());System.out.println(source.getSize());} }
6、運行測試代碼,可以正常拿到 xml 配置的 bean
橙子 多糖 超大杯

總結

以上是生活随笔為你收集整理的编写第一个Spring程序——IOC实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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