版本spring
IDEA 2018.5app
Gradle 5.6.3maven
Spring 5.1.xxxide
步驟參考:函數
https://www.yuque.com/docs/share/a0ebb22b-1dbc-4ad1-b1f7-7999419d27a3?#測試
連接:https://pan.baidu.com/s/1hIHb8Y_Ixlr_vqF7LF1J0Q
提取碼:gyfi gradle
(跟第五條有關聯可先不配置)this
找到本地gradle倉庫地址,通常爲 .gradle文件夾。而後在裏面添加init.gradle文件,文件內容以下url
allprojects{ repositories { def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public' def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter' all { ArtifactRepository repo -> if(repo instanceof MavenArtifactRepository){ def url = repo.url.toString() if (url.startsWith('https://repo1.maven.org/maven2')) { project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL." remove repo } if (url.startsWith('https://jcenter.bintray.com/')) { project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL." remove repo } } } maven { url ALIYUN_REPOSITORY_URL url ALIYUN_JCENTER_URL } } }
沒有導入import project,spa
方法一、File >> close project
方法二、Settings > Appearance & Bechavior > Menus and Toolbars.
打開Main menu > File > 選中File 下邊任意一個, 點擊 右側按鈕 Add After
這裏會彈出一個界面讓你選擇添加的功能
Import Project 選項在Other目錄下, 找到import Project ,點擊OK保存設置便可
開始導入
首先找到源碼中 spring-framework-5.1.x\spring-framework-5.1.x\gradle\wrapper 文件夾,修改gradle-wrapper.properties中的屬性 該處主要是爲了防止再次下載其餘的版本gradle
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=file\:///C:Program Files/Java/gradle-5.6.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
repositories { maven { url "https://maven.aliyun.com/repository/spring-plugin" } maven{ url "https://maven.aliyun.com/nexus/content/repositories/spring-plugin"} maven { url "https://repo.spring.io/plugins-release" } maven { url "https://repo.spring.io/plugins-release" } }
repositories { maven { url "https://maven.aliyun.com/repository/central" } maven { url "https://repo.spring.io/libs-release" } mavenLocal() }
⼯程—>tasks—>compileTestJava
新建model
添加依賴(修改紅色區域)指定jdk版本號,依賴 spring-context
編寫測試
public class IocTest { @Test public void testIoC() { ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); LagouBean lagouBean = classPathXmlApplicationContext.getBean(LagouBean.class); System.out.println(lagouBean); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd "> <!--循環依賴問題--> <bean id="lagouBean" class="com.lagou.edu.LagouBean"> <property name="ItBean" ref="itBean"/> </bean> <bean id="itBean" class="com.lagou.edu.ItBean"> <property name="LagouBean" ref="lagouBean"/> </bean> <!--<bean id="myBeanFactoryPostProcessor" class="com.lagou.edu.MyBeanFactoryPostProcessor"/> <bean id="myBeanPostProcessor" class="com.lagou.edu.MyBeanPostProcessor"/>--> <!--<bean id="lagouBean" class="com.lagou.edu.LagouBean"> </bean>--> <!--aop配置--> <!--橫切邏輯--> <!--<bean id="logUtils" class="com.lagou.edu.LogUtils"> </bean> <aop:config> <aop:aspect ref="logUtils"> <aop:before method="beforeMethod" pointcut="execution(public void com.lagou.edu.LagouBean.print())"/> </aop:aspect> </aop:config>--> </beans>
建立 LagouBean
public class LagouBean implements InitializingBean, ApplicationContextAware { private ItBean itBean; public void setItBean(ItBean itBean) { this.itBean = itBean; } /** * 構造函數 */ public LagouBean(){ System.out.println("LagouBean 構造器..."); } /** * InitializingBean 接口實現 */ public void afterPropertiesSet() throws Exception { System.out.println("LagouBean afterPropertiesSet..."); } public void print() { System.out.println("print方法業務邏輯執行"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("setApplicationContext...."); } }
到這,說明 Spring源碼構建成功