Spring-02-快速上手
阿新 • • 發佈:2020-12-07
Spring-02-快速上手
注 : spring 需要匯入commons-logging進行日誌記錄 . 我們利用maven , 他會自動下載對應的依賴項 .
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
1. 編寫程式碼
1、編寫一個Hello實體類
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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--bean就是java物件 , 由Spring建立和管理--> <bean id="hello" class="com.kuang.pojo.Hello"> <property name="name" value="Spring"/> </bean> </beans>
3、我們可以去進行測試了 .
@Test
public void test(){
//解析beans.xml檔案 , 生成管理相應的Bean物件
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//getBean : 引數即為spring配置檔案中bean的id .
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
2. 思考
- Hello 物件是誰建立的 ? 【hello 物件是由Spring建立的
- Hello 物件的屬性是怎麼設定的 ? hello 物件的屬性是由Spring容器設定的
這個過程就叫控制反轉 :
- 控制 : 誰來控制物件的建立 , 傳統應用程式的物件是由程式本身控制建立的 , 使用Spring後 , 物件是由Spring來建立的
- 反轉 : 程式本身不建立物件 , 而變成被動的接收物件 .
依賴注入 : 就是利用set方法來進行注入的.
IOC是一種程式設計思想,由主動的程式設計變成被動的接收
可以通過newClassPathXmlApplicationContext去瀏覽一下底層原始碼 .
修改案例一
我們在案例一中, 新增一個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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="MysqlImpl" class="com.kuang.dao.impl.UserDaoMySqlImpl"/>
<bean id="OracleImpl" class="com.kuang.dao.impl.UserDaoOracleImpl"/>
<bean id="ServiceImpl" class="com.kuang.service.impl.UserServiceImpl">
<!--注意: 這裡的name並不是屬性 , 而是set方法後面的那部分 , 首字母小寫-->
<!--引用另外一個bean , 不是用value 而是用 ref-->
<property name="userDao" ref="OracleImpl"/>
</bean>
</beans>
測試!
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("ServiceImpl");
serviceImpl.getUser();
}
OK , 到了現在 , 我們徹底不用再程式中去改動了 , 要實現不同的操作 , 只需要在xml配置檔案中進行修改 , 所謂的IoC,一句話搞定 : 物件由Spring 來建立 , 管理 , 裝配 !
IOC建立物件方式
通過無參構造方法來建立
1、User.java
public class User {
private String name;
public User() {
System.out.println("user無參構造方法");
}
public void setName(String name) {
this.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.kuang.pojo.User">
<property name="name" value="kuangshen"/>
</bean>
</beans>
3、測試類
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//在執行getBean的時候, user已經建立好了 , 通過無參構造
User user = (User) context.getBean("user");
//呼叫物件的方法 .
user.show();
}
結果可以發現,在呼叫show方法之前,User物件已經通過無參構造初始化了!
通過有參構造方法來建立
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.kuang.pojo.UserT">
<!-- index指構造方法 , 下標從0開始 -->
<constructor-arg index="0" value="kuangshen2"/>
</bean>
<!-- 第二種根據引數名字設定 -->
<bean id="userT" class="com.kuang.pojo.UserT">
<!-- name指引數名 -->
<constructor-arg name="name" value="kuangshen2"/>
</bean>
<!-- 第三種根據引數型別設定 -->
<bean id="userT" class="com.kuang.pojo.UserT">
<constructor-arg type="java.lang.String" value="kuangshen2"/>
</bean>
3、測試
@Test
public void testT(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserT user = (UserT) context.getBean("userT");
user.show();
}
結論:在配置檔案載入的時候。其中管理的物件都已經初始化了!
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.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
import
團隊的合作通過import來實現 .
<import resource="{path}/beans.xml"/>