通過實(shí)例解析Spring Ioc項(xiàng)目實(shí)現(xiàn)過程
0. Ioc
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html
主要是實(shí)現(xiàn)一個(gè)控制反轉(zhuǎn),耦合性大大降低。
1. 建maven項(xiàng)目
建立一個(gè)空的maven項(xiàng)目,然后pom.xml添加spring-context的依賴:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
2. 創(chuàng)建pojo java對(duì)象
package com.aca;
public class Hello {
private String str;
public void setStr(String str) {
this.str = str;
}
public String getStr() {
return str;
}
public Hello(String str){
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
3. 創(chuàng)建bean xml配置元數(shù)據(jù)
配置文件放在resources下。
這里以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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Hello" class="com.aca.Hello">
<constructor-arg type="java.lang.String" value="fffff"/>
</bean>
</beans>
如果有多個(gè)resource或者目錄不一致,就需要import一下:
<beans> <import resource="services.xml"/> <import resource="resources/messageSource.xml"/> <import resource="/resources/themeSource.xml"/> <bean id="bean1" class="..."/> <bean id="bean2" class="..."/> </beans>
里面可以調(diào)用構(gòu)造函數(shù)來初始化一下bean。
4.創(chuàng)建spring 上下文
這里用ClassPathXmlApplicationContext 方法。
ApplicationContext context = new ClassPathXmlApplicationContext("hbean.xml");
// retrieve configured instance
Hello hello = context.getBean("Hello", Hello.class);
// hello.setStr("abc");
System.out.println(hello);
直接可以用這個(gè)bean,由xml注入。
5. Error:java: 錯(cuò)誤: 不支持發(fā)行版本 5
將file- project structure 中的jdk版本選成跟本地一直,比如我這個(gè)jdk14

將build -> java complier中的兩個(gè)版本選擇成跟本地一致,這里是14

這兩步做好以后不會(huì)報(bào)錯(cuò),maven里面不需要選擇版本。
6. 如果報(bào)xml的問題
xml declaration should precede all document
那是因?yàn)閤ml 第一行是空格了,必須<?xml 做為第一行。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)將數(shù)字日期翻譯成英文單詞的工具類實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)將數(shù)字日期翻譯成英文單詞的工具類,結(jié)合完整實(shí)例形式分析了Java日期轉(zhuǎn)換與字符串操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
聊聊@Autowired注解注入,寫接口名字還是實(shí)現(xiàn)類的名字
這篇文章主要介紹了聊聊@Autowired注解注入,寫接口名字還是實(shí)現(xiàn)類的名字,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java中final,finally,finalize三個(gè)關(guān)鍵字的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章給大家收集整理了有關(guān)java中final,finally,finalize三個(gè)關(guān)鍵字的區(qū)別介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-04-04
Java 超詳細(xì)講解IO操作字節(jié)流與字符流
本章具體介紹了字節(jié)流、字符流的基本使用方法,圖解穿插代碼實(shí)現(xiàn)。 JAVA從基礎(chǔ)開始講,后續(xù)會(huì)講到JAVA高級(jí),中間會(huì)穿插面試題和項(xiàng)目實(shí)戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03
Java?C++分別實(shí)現(xiàn)滑動(dòng)窗口的最大值
這篇文章主要介紹了分別通過Java和C++實(shí)現(xiàn)滑動(dòng)窗口最大值,即給定一個(gè)數(shù)組?nums?和滑動(dòng)窗口的大小?k,請(qǐng)找出所有滑動(dòng)窗口里的最大值。感興趣的可以了解一下2021-12-12
MyBatis關(guān)聯(lián)查詢的實(shí)現(xiàn)
MyBatis可以通過定義多個(gè)表的關(guān)聯(lián)關(guān)系,實(shí)現(xiàn)多表查詢,本文主要介紹了MyBatis關(guān)聯(lián)查詢的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11最新評(píng)論

