第一個程序、對象創建方式、配置說明
導入Jar包
注 : spring 需要導入commons-logging進行日誌記錄 . 我們利用maven , 他會自動下載對應的依賴項 .
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
編寫代碼
-
編寫一個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 ); } }
-
編寫我們的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>
-
我們可以去進行測試了 .
@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(); }
思考
- 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"/>