Spring 學習02:上手Spring
Spring 學習02:上手Spring
上一篇文章介紹了Spring,那接下來肯定就要上手了
Hello Spring:
匯入jar包
在 pow.xml 匯入jar包,spring 需要匯入commons-logging進行日誌記錄 。 我們利用maven , 他會自動下載對應的依賴項 .junit用於測試類
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId >
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
編寫程式碼
1.編寫一個實體類
public class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("Hello,"+ name );
}
}
2.編寫我們的spring檔案 , 這裡我們命名為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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean就是java物件,由Spring建立和管理-->
<!--型別 變數名 = new 型別()
Hello hello = new Hello()
id:變數名
class: new 的物件
property:相當於給物件中的屬性設定一個值!-->
<bean id="hello" class="com.pwx.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
</beans>
3.測試
public class MyTest {
@Test
public void test(){}{
//獲取Spring的上下文物件!
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我們的物件現在都在Spring中的管理了,我們要使用,直接去裡面取出來就可以
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
思考
在這裡,我們可以看,我們不再和之前一樣,new物件了
那麼思考幾個問題
- Hello 物件是誰創造的
- 物件相應的屬性是怎麼設定的
毫無疑問,我們都知道,物件是通過Spring建立的,畢竟我們在學這個嘛,那相應的屬性值也肯定是他設定的了,但是他怎麼實現的呢?
這就是所謂的IOC(控制反轉),結合上一篇文章,我們都知道,所謂的控制反轉,就是不再是由程式設計師來創造物件了,我們負責留出一個介面,也就是這個set,然後又呼叫者向這個介面注入他想創造的類,在這裡:
- 控制:誰來控制物件的建立 , 傳統應用程式的物件是由程式本身控制建立的 , 使用Spring後 , 物件是由Spring來建立的
- 反轉 : 程式本身不建立物件 , 而變成被動的接收物件
- 依賴注入 : 就是利用set方法來進行注入的
說白了,IOC是一種程式設計思想,由主動的程式設計變成被動的接收,那對於我們來說,你是不是就可以偷懶了,不用總是去改程式碼了
有興趣的話,可以去看看ClassPathXmlApplicationContext的原始碼
示範偷懶
1.修改一下配置檔案
<?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 id="MysqlImpl" class="com.pwx.dao.impl.UserDaoMySqlImpl"/>
<bean id="OracleImpl" class="com.pwx.dao.impl.UserDaoOracleImpl"/>
<bean id="ServiceImpl" class="com.pwx.service.impl.UserServiceImpl">
<!--注意: 這裡的name並不是屬性 , 而是set方法後面的那部分 , 首字母小寫-->
<!--引用另外一個bean , 不是用value 而是用 ref-->
<property name="userDao" ref="OracleImpl"/>
</bean>
</beans>
2.測試一下
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("OracleImpl");
serviceImpl.getUser();
}
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("ServiceImpl");
serviceImpl.getUser();
}
這時候,是不是就看出來了,我們不用去改程式碼了,只需要改改配置檔案了,那多好啊,這不就是偷懶了
總結就是一句話:所有的物件,由Spring來建立,管理,裝配
IOC建立物件方式
通過無參構造方法來創造
1.User.java
public class User {
private String name;
public User(){
System.out.println("User的無參構造");
}
public User(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
public void show(){
System.out.println("name :" + name);
}
}
2.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.xsd">
<bean id="user" class="com.pwx.pojo.User">
<property name="name" value="Spring"/>
</bean>
</beans>
3.測試
public class MyTest {
@Test
public void test(){}{
//獲取Spring的上下文物件!
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我們的物件現在都在Spring中的管理了,我們要使用,直接去裡面取出來就可以
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
通過執行結果,我們可以很清楚的看到,Spring是通過無參的構造方法創造物件的。
那能不能用有參的呢,我們來看看
通過有參的構造方法來構建
1.UserT . java
public class UserT {
private String name;
public UserT(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+ name );
}
}
2、beans.xml 有三種方式編寫
<!-- 第一種根據index引數下標設定 -->
<bean id="userT" class="com.pwx.pojo.UserT">
<!-- index指構造方法 , 下標從0開始 -->
<constructor-arg index="0" value="Spring1"/>
</bean>
<!-- 第二種根據引數名字設定 -->
<bean id="userT" class="com.pwx.pojo.UserT">
<!-- name指引數名 -->
<constructor-arg name="name" value="Spring2"/>
</bean>
<!-- 第三種根據引數型別設定 -->
<bean id="userT" class="com.pwx.pojo.UserT">
<constructor-arg type="java.lang.String" value="Spring3"/>
</bean>
這三種我們都可以在官方的文件上看到,我一般都是用第二種,比較清楚,這個你喜歡用什麼就用什麼
3.測試
@Test
public void testT(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserT user = (UserT) context.getBean("userT");
user.show();
}
可以很清楚的看到,確實都輸入來了,是通過有參構造方法來創造物件的
說到官方文件,在這裡提到一個點,很多小夥伴看到我們的配置檔案都是用beans.xml,而不是用applicationContext.xml會有一些疑惑,其實在這裡由兩個方面:
- 第一個很明顯是為了偷懶,官方的太長了
- 第二個是設計到團隊開發,到了最後合併的時候只需要到applicationContext.xml寫入:
<import resource="{path}/beans.xml"/>
就可以完成多個專案的配置檔案合併了
Spring配置
再說一些其他的Spring配置吧,Spring的配置其實很麻煩的,但是因為我們還沒到,所以就覺得還行,配置地獄可不是鬧著玩的
別名
alias 設定別名 , 為bean設定別名 , 可以設定多個別名(我覺得沒什麼用)
<!--設定別名:在獲取Bean的時候可以使用別名獲取-->
<alias name="userT" alias="userNew"/>
Bean的配置
<!--bean就是java物件,由Spring建立和管理-->
<!--
id 是bean的識別符號,要唯一,如果沒有配置id,name就是預設識別符號
如果配置id,又配置了name,那麼name是別名
name可以設定多個別名,可以用逗號,分號,空格隔開
如果不配置id和name,可以根據applicationContext.getBean(.class)獲取物件;
class是bean的全限定名=包名+類名
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.pwx.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
很明顯,這個就比上面的好用
個人覺得,最好還是就著原名用,要不然程式碼多了之後,很亂
最後
以上所有是通過在嗶哩嗶哩中遇見狂神說,狂神老師學習的,在這裡表示熱衷的感謝,又不懂的大家也可以去看看他的視訊。