Spring簡介
Spring是春天的意思---> 給軟體行業帶來了春天
2002,首次推出了Spring框架的雛形:interface21框架!
2004年3月24日,Spring框架以interface21框架為基礎,經過重新設計,發布了1.0正式版本,
Rod Johnson,Spring Framework創始人
spring理念
是現有的技術更加容易使用,本身是一個大雜燴,整合了現有的技術框架!
SSH:Struct2 +Spring + Hibernate
SSM: SpringMvc +Spring +Mybatis
官網:Spring Framework
官方下載地址:repo.spring.io
GitHub: GitHub - spring-projects/spring-framework: Spring Framework
導包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
jdbc
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
優點
Spring是一個開源的免費得框架(容器)!
Spring是一個輕量級得、非入侵式得框架!
控制反轉(lOC),面向切面編程(AOP)!
支持事務的處理,對框架整合的支持!
總結一句話
Spring就是一個輕易級的控制反轉(loc)和面向切面編程(AOP)的框架!
Spring組成和擴展
1.組成

2.擴展
在Spring官網有這個介紹:現代化的java開發!說白就是基于spring開發

Spring Boot
一個快速開發的腳手架,
基于Spring Boot可以快速的開發單個微服務,
約定大于配置!
Spring Cloud
Spring Cloud是基于SpringBoot實作的,
為什么要使用他們?
因為現在大多公司都在使用Spring Boot進行快速開發,學習Spring Boot的前提,需要完全掌握Spring以及SpringMVC!承上啟下的作用!
弊端:發展了太久之后,違背了原來的理念!配置十分繁瑣,人稱:"配置地獄!"
loc理論推導
UserDao介面
UserDaolmpl實作類
UserService業務介面
UserServicelmpl業務實作類
在我們之前的業務中,用戶的需求可能會影響我們原來的代碼,我們需要根據 用戶的需求去修改原代碼!
如果程式代碼量十分大,修改一次的成本代價十分昂貴!

使用一個set 介面實作
//利用set進行動態實作值的注入!
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
之前,程式員是主動創建物件! 控制權在程式員手上!
使用了set注入后,程式員不再具有主動性,而是變成了被動的接受物件!
這種思想從本質上解決了問題,我們程式員不用再去管理物件的創建了.系統的耦合性大大降低,可以更加專注在業務的實作上!這是loc的原型!

loc本質
控制反轉loC(Inversion of Control),是-種設計思想,DI(依賴注入)是實作loC的- -種方法,也有人認為DI只是loC的另-種說法,沒有IoC的程式中,我們使用面向物件編程,物件的創建與物件間的依賴關系完全硬編碼在程式中,物件的創建由程式自己控制,控制反轉后將物件的創建轉移給第三方,個人認為所謂控制反轉就是:獲得依賴物件的方式反轉了,
采用XML方式配置Bean的時候,Bean的定義資訊是和實作分離的,而采用注解的方式可以把兩者合為一體,Bean的定義資訊直接以注解的形式定義在實作類中,從而達到了零配置的目的,
控制反轉是-種通過描述(XML 或注解)并通過第三方去生產或獲取特定物件的方式,在Spring中實作控制反
轉的是loC容器,其實作方法是依賴注入(Dependency Injection,DI),
Spring測驗
1.導包

2.新建一個物體類

3.新建一個beans.xml組態檔

4.在test檔案下進行測驗

5.輸出結果,值就被取過來了

loc物件創建方式
1.使用無參構造,默認!
2.假設我們要使用有參構造創建物件,
1. 下標賦值
<!--第一種下標賦值-->
<bean id="user" class="com.bubbles.pojo.User">
<constructor-arg index="0" value="彬哥 "/>
</bean>
2.型別,不建議使用
<!--第二種方式通過型別創建,不建議使用!-->
<bean id="user" class="com.bubbles.pojo.User">
<constructor-arg type="java.lang.String" value="彬哥"/>
</bean>
3.引數名,建議使用
<!--第三種,直接通過引數名來設定 -->
<bean id="user" class="com.bubbles.pojo.User">
<constructor-arg name="name" value="彬哥"/>
</bean>
總結
在組態檔加載的時候,容器中管理的物件就已經初始化了!
Spring配置說明
別名
<!-- 別名,如果添加了別名,我們也可以使用別名獲取到這個物件-->
<alias name="user" alias="springyyds"/>
Bean的配置
<!--配置 id:bean 的唯一識別符號,也就是相當于我們學的物件名 class :bean 物件所對應的全限定名:包名 +型別 name 也是別名,而且name 可以同時取多個別名 -->
<bean id="user1" class="com.bubbles.pojo.User1" name="user12,u2,u3;u4">
<property name="name" value="ksyyds"/>
</bean>
import
這個import,一般用于團隊開發使用,他可以將多個組態檔,匯入合并為一個
假設,現在專案中有多個人開發,這三個人復制不同的類開發,不同的類需要注冊在不同的bean中,我們可以利用import將所有人的beans.xml合并為一個總的!
張三
李四
王五
applicationContext.xml
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
使用的時候,直接使用總的配置就可以了,
Dl依賴注入環境
依賴注入
構造器注入
前面有
2.set方式注入【重點】
依賴注入:Set注入!
依賴
bean物件的創建依賴于容器!
注入
bean物件中的所有屬性,由容器來注入
環境搭建
1.復雜型別
package com.bubbles.pojo;
public class Address {
//參考物件
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.真實測驗物件
package com.bubbles.pojo; import java.util.*;
//學生類
public class Student {
private String name;
private Address address;
private String [] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
后面的Ait+lnsert自己生成 }
3.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 id="student" class="com.bubbles.pojo.Student">
<!--第一種,普通值注入,value -->
<property name="name" value="彬哥"/>
</bean>
</beans>
4.測驗
import com.bubbles.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext mapper = new ClassPathXmlApplicationContext ("beans.xml");
Student student = (Student)mapper.getBean ("student");
System.out.println (student.getName ());
}
}
完善注入資訊
<?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 id="address" class="com.bubbles.pojo.Address">
<property name="address" value="西安" />
</bean>
<bean id="student" class="com.bubbles.pojo.Student">
<!--第一種,普通值注入,value -->
<property name="name" value="彬哥"/>
<!--第二種,Bean注入,ref -->
<property name="address" ref="address"/>
<!-- 陣列注入-->
<property name="books" >
<array>
<value>紅樓夢</value>
<value>西游記</value>
<value>熊出沒</value>
<value>海綿寶寶</value>
</array>
</property>
<!-- List -->
<property name="hobbys">
<list>
<value>編程</value>
<value>學習</value>
<value>打代碼</value>
</list>
</property>
<!--Map -->
<property name="card">
<map>
<entry key="身份證" value="6666666666666666"/>
<entry key="銀行卡" value="123456789"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>和平精英</value>
<value>我的世界</value>
<value>植物大戰僵尸</value>
</set>
</property>
<!--null -->
<property name="wife">
<null/>
</property>
<!--Properties
key=value
key=value
key=value
-->
<property name="info">
<props>
<prop key="driver">20218031</prop>
<prop key="url">男</prop>
<prop key="username">root</prop>
<prop key="password">Root</prop>
</props>
</property>
</bean>
</beans>
常用注入

擴展方式注入
官方解釋

使用
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd ">
<!--p命名空間注入,可以直接注入屬性的值:property -->
<bean id="user" class="com.bubbles.pojo.User" p:name="彬哥" p:age="17"/>
<!--c命名空間注入,通過構造器注入:constructs-args -->
<bean id="user2" class="com.bubbles.pojo.User" c:age="17" c:name="bubbles" />
</beans>
測驗
@Test public void test(){
ApplicationContext mapper = new ClassPathXmlApplicationContext ("userbeans.xml");
User user = mapper.getBean ("user",User.class);
System.out.println (user);
}
注意點
p命名和c命名空間不能直接使用,需要匯入xml約束!
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
bean的作用域

1.單理模式(Spring默認機制)
<bean id="user2" class="com.bubbles.pojo.User" c:age="17" c:name="bubbles" scope="singleton" />
2.原型模式:每次容器中get的時候,都會產生一個新物件!
<bean id="user2" class="com.bubbles.pojo.User" scope="singleton" />
3.其余的request、session、application、這些個只能在web開發中使用到!
Bean的自動裝配
自動裝配是Spring滿足bean依賴一種方式!
Spring會在背景關系中自動尋找,并自動給bean裝配屬性!'
在Spring中有三中裝配的方式
1.在xml中顯示的配置
2.在java中顯示配置
3.隱式的自動裝配bean【重要】
測驗
環境搭建
一個人有兩只寵物!
ByName自動裝配
<!-- autowire是自動裝配的意思-->
<!--byName:會自動在容器背景關系中查找,和自己物件set方法后面的值對應的beanid! --> <bean id="people" class="com.bubbles.pojo.People" autowire="byName">
<property name="name" value="彬哥" />
</bean>
ByType自動裝配
<bean class="com.bubbles.pojo.Cat" />
<bean class="com.bubbles.pojo.Dog" />
<!-- autowire是自動裝配的意思-->
<!--byName:會自動在容器背景關系中查找,和自己物件set方法后面的值對應的beanid! -->
<!--byType:會自動在容器背景關系中查找,和自己物件屬性型別相同的bean! -->
<bean id="people" class="com.bubbles.pojo.People" autowire="byType">
<property name="name" value="彬哥" />
</bean>
總結
byname的時候,需要保證所有bean的id唯一,并且這個bean需要和自動注入的屬性的set方法的值一致!
bytype的時候,需要保證所有bean的class唯一,并且這個bean需要和自動注入的屬性的型別一致!
使用注解實作自動裝配
jdk1.5支持的注解,Spring2.5就支持注解,
要使用注解須知
1.匯入約束: context約束
2.配置注解的支持 :<context:annotation-config /> 【重要!】
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemalocation="http://www.springframework.org/schema/beans
https://www.springf ramework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Autowired
直接在屬性上使用即可!
也可以在set方式上使用!

使用@Autowired我們可以不用撰寫Set方法了,前提是你這個自動裝配的屬性在loc(Spring)容器中存在,且符合名字byname!
科普
@Nullable 欄位標記了這個注解,說明這個欄位可以為null:
public @interface Autowired{
boolean required() default true;
}
測驗代碼
public class People {
//如果顯示定義了Autowired的required屬性false,說明這個物件可以為null,否則不允許為空
public class People {
@Autowired(required = false)
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
如果 @Autowired自動裝配的環境比較復雜,自動裝配無法通過一個注解
【@Autowired】完成的時候、我們可以使用@Qualifier(value ="xxx")去配置
@Autowired的使用,指定一個唯一的bean物件注入!
public class People {
@Autowired
@Qualifier(value ="cat11")
private Cat cat;
@Autowired
@Qualifier(value ="dog111")
private Dog dog;
private String name;
}
@Resource注解
public class People {
@Resource(name = "cat2")
private Cat cat;
@Resource
private Dog dog;
private String name;
}
總結
@Resource 和@Autowired的區別
都是用來自動裝配的,都可以放在屬性欄位上
@Autowired 通過byType的方式實作,而且必須要求這個物件存在!【常用】
@Resource 默認通過byname的方式實作,如果找不到名字,則通過 !byType實作! 如果兩個都找不到的情況下,就報錯!【常用】
執行順序不同: @Autowired 通過byType的方式實作, @Resource 默認通過byname的方式實作,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/297944.html
標籤:其他
