Spring框架(一)Spring入門
阿新 • • 發佈:2021-06-15
spring快速入門
1、匯入spring依賴
maven倉庫地址:https://mvnrepository.com/
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.8</version> </dependency>
2、在com.study.pojo下編寫實體類程式碼
package com.study.pojo; 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 ); } }
3、在resources目錄下編寫spring配置檔案【applicationContext.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"> <!--使用spring來建立物件,在spring中這些都成為bean Hello hello = new Hello bean = 物件 new Hello() =========================== id = bean的名字 class = new的物件 property 相當於給物件中的屬性設定一個值! --> <bean id="hello" class="com.study.pojo.Hello"> <!--name="屬性名/變數名" value="屬性/變數的值"--> <property name="str" value="Spring"/> </bean> </beans>
4、在test/java目錄下編寫測試程式碼:
import com.study.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
//獲取Spring的上下文物件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//我們的物件都在spring中管理,我們要使用直接去裡面取出來!
//Hello hello = context.getBean("hello",Hello.class);
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
5、測試結果
6、總結
控制:誰來控制物件的建立,傳統應用程式的物件是由程式本身控制建立的,使用Spring後,物件是由Spring來建立的
反轉:程式本身不建立物件有bean去建立管理, 而變成被動的接收物件
依賴注入:就是利用set方法來進行注入的
IOC是一種程式設計思想,由主動的程式設計變成被動的接收
IOC建立物件方式
一、通過無參構造方法來建立
1、編寫實體類
package com.study.pojo;
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.study.pojo.User">
<constructor-arg name="name" value="張三"/>
</bean>
</beans>
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//在執行getBean的時候, user已經建立好了 , 通過無參構造
User user = (User) context.getBean("user");
//呼叫物件的方法 .
user.show();
}
二、通過有參構造方法來建立
package com.study.pojo;
public class UserT {
private String name;
public UserT() {
System.out.println("UserT被建立了");
}
public UserT(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
編寫beans.xml三種方式
(1)根據index引數下標設定
<!-- 第一種根據index引數下標設定 -->
<bean id="userT" class="com.kuang.pojo.UserT">
<!-- index指構造方法 , 下標從0開始 -->
<constructor-arg index="0" value="kuangshen2"/>
</bean>
(2)根據引數名字設定
<!-- 第二種根據引數名字設定 -->
<bean id="userT" class="com.kuang.pojo.UserT">
<!-- name指引數名 -->
<constructor-arg name="name" value="kuangshen2"/>
</bean>
(3)根據引數型別設定
<!-- 第三種根據引數型別設定 -->
<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配置
1、別名
<!--設定別名:在獲取Bean的時候可以使用別名獲取-->
<!--name="已存在的bean的名字" alias="別名"-->
<alias name="userT" alias="t"/>
2、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>
2、import
一個專案有多個配置檔案的時候,可以用import來匯入
<import resource="beans.xml"/>