?作者簡介:正在學(xué)習(xí)java全棧,有興趣的可以關(guān)注我一起學(xué)習(xí)
??個人主頁: ConderX(摸魚)的主頁
??系列專欄: Spring專欄
??如果覺得博主的文章還不錯的話,請??三連支持一下博主哦??
前言
我也是剛開始接觸Spring,有興趣的同學(xué)可以和我一起零基礎(chǔ)(當(dāng)然要會Java基礎(chǔ)和XML文檔)慢慢來,我是跟著和我一起自學(xué)的同學(xué)的學(xué)習(xí)步驟來的,學(xué)習(xí)Spring之前先看一下我之前發(fā)的Maven教程,因為環(huán)境是通過Maven依賴導(dǎo)入的。
有興趣和我一起學(xué)習(xí)的可以訂閱我的Spring專欄
1.Spring環(huán)境搭建
頻繁導(dǎo)入包很麻煩所以直接用Maven倉庫,下面是我的編程環(huán)境(如果學(xué)習(xí)當(dāng)中出現(xiàn)代碼正確卻運行不起來的情況,多半是版本不對應(yīng),我同學(xué)就在一直換版本)
-
jdk 11.0.8
https://blog.csdn.net/qq_45925787/article/details/123641828
-
idea 2020.1(包含激活插件)
鏈接:https://pan.baidu.com/s/1KzwGq2azLBf_2o7tQiMwSg?pwd=csdn
提取碼:csdn -
maven 3.8.4
2.第一個Spring程序
-
新建項目
-
配置項目名稱、位置和GAV(group、ArtifactId、Version)
-
選擇Maven倉庫位置
可以配置一下默認(rèn)Maven倉庫地址,不用每次新建項目都重新配置了,具體步驟如下:
-
file–>new projects setting–>setting for new projects
-
Builds,Execution,Deployment–>Maven
-
-
成功文件結(jié)構(gòu)如圖:
如果找不到文件目錄并報了如下警告:No archetype found in remote catalog. Defaulting to internal catalog
參考:https://blog.csdn.net/qq_45925787/article/details/123721599
-
修改配置參數(shù):
-
工程新建的時候選的jdk 11
-
模塊jdk也要改為一樣的版本(電腦里只有一個版本的jdk的話一般不會出錯):選中項目名+f4
-
修改pom.xml文件:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> //這兩項文本值修改為jdk版本,不然后面可能會出錯 <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties>
-
-
通過Maven引入spring依賴:
Maven規(guī)定世界上任何一個構(gòu)件都可以使用Maven坐標(biāo)并作為一位標(biāo)識,所以大部分依賴都可以在https://mvnrepository.com/中找到
- 在網(wǎng)站首頁搜索要添加的依賴
- 在詳情頁最下面復(fù)制坐標(biāo)
- 粘貼到項目的pom.xml中指定位置
- 引用完之后是爆紅的,點擊右上角m圖標(biāo),第一次引入比較慢因為他要下載到本地倉庫
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
//我準(zhǔn)備用同一個版本號,所以我定一個一個spring版本號,下面直接引用這個版本號
<spring.version>5.3.15</spring.version>
</properties>
<dependencies>
<!--日志-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- java ee -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 單元測試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 實現(xiàn)slf4j接口并整合 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<!-- 數(shù)據(jù)庫 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
<!-- 數(shù)據(jù)庫連接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSON解析 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
<!-- JSTL標(biāo)簽庫 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 數(shù)據(jù)效驗 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.1.0.CR2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- 文件上傳組件 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
-
新建資源文件夾(使用Maven會約定好,存放bean的位置)
右鍵src–>new–>diretory–>main\resources
-
在main/java/組id的包里新建java文件:
package org.example; public class HelloWorld { private String message; public void setMessage(String message) { this.message = message; } public void getMessage() { System.out.println("message : " + message); } }
-
在資源文件夾中新建名為beans的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-3.0.xsd"> <bean id="helloworld1" class="org.example.HelloWorld"> <property name="message" value="Hello World1!" /> </bean> <bean id="helloworld2" class="org.example.HelloWorld"> <property name="message" value="Hello World2!" /> </bean> </beans>
-
在APP測試:文章來源:http://www.zghlxwxcb.cn/news/detail-471946.html
package org.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Hello world! * */ public class App { public static void main( String[] args ) { //創(chuàng)建上下文對象,讀取配置文件,bean工廠 ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml"); //創(chuàng)建Bean HelloWorld bean1= (HelloWorld) context.getBean("hello1"); HelloWorld bean2=context.getBean("hello2",HelloWorld.class); bean1.getMessage(); bean2.getMessage(); } }
輸出結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-471946.html
message:Hello World1! message:Hello World2!
到了這里,關(guān)于【Spring入門 】1.環(huán)境搭建并運行第一個Spring程序(Maven依賴)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!