通過實(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ù)來(lái)初始化一下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通過PropertyDescriptor反射調(diào)用set和get方法
這篇文章主要為大家詳細(xì)介紹了Java通過PropertyDescriptor反射調(diào)用set和get方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03SpringAop實(shí)現(xiàn)原理及代理模式詳解
Spring的AOP就是通過動(dòng)態(tài)代理實(shí)現(xiàn)的,使用了兩個(gè)動(dòng)態(tài)代理,分別是JDK的動(dòng)態(tài)代理和CGLIB動(dòng)態(tài)代理,本文重點(diǎn)給大家介紹下SpringAop實(shí)現(xiàn)原理及代理模式,感興趣的朋友一起看看吧2022-04-04Java利用廣度優(yōu)先搜索實(shí)現(xiàn)抓牛問題
廣度優(yōu)先搜索是最簡(jiǎn)便的圖的搜索算法之一,這一算法也是很多重要的圖的算法的原型。本文將利用廣度優(yōu)先搜索實(shí)現(xiàn)抓牛問題,感興趣的可以了解下2022-06-06SpringBoot整合log4j2日志的實(shí)現(xiàn)
在項(xiàng)目推進(jìn)中,如果說(shuō)第一件事是搭Spring框架的話,那么第二件事情就是在Sring基礎(chǔ)上搭建日志框架,大家都知道日志對(duì)于一個(gè)項(xiàng)目的重要性,尤其是線上Web項(xiàng)目,因?yàn)槿罩究赡苁俏覀兞私鈶?yīng)用如何執(zhí)行的唯一方式。此篇文章是博主在實(shí)踐中用Springboot整合log4j2日志的總結(jié)2021-06-06SpringMVC Mock測(cè)試實(shí)現(xiàn)原理及實(shí)現(xiàn)過程詳解
這篇文章主要介紹了SpringMVC Mock測(cè)試實(shí)現(xiàn)原理及實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10SpringMVC異步處理操作(Callable和DeferredResult)
這篇文章主要介紹了SpringMVC異步處理操作(Callable和DeferredResult),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-01-01