目錄
1. Spring定義:
(1). IOC( Inversion of Control):
(2). AOP (Aspect Oriented Programming):
(3)一站式:
2. spring搭建:
(1). 創建一個Maven項目
(2). 導入核心 jar包
(3). 編寫 spring 配置文件
(4). 編寫實體類,并生成set方法
(5). 在resource中加入spring核心依賴?
(6)獲取對象,測試spring
3.?xml配置方式屬性賦值
4.?注解方式實現屬性賦值
(1). 開啟注解掃描
(2). 注解自己所創建的對象
5. spring注解標簽分類
(1). 生成對象的注解
(2). 屬性注入的注解
1. Spring定義:
? ? Spring是一個 輕量級 (核心功能的jar比較小,運行占的資源少),IOC?和?AOP?的 一站式的Java開發框架
官網地址:?Spring | Home
(1). IOC( Inversion of Control):
控制反轉,是一種編程思想,將對象的管理(創建 初始化 存儲 銷毀 添加額外的功能...)統一交給spring框架
正控:若要使用某個對象,需要自己去負責對象的創建
反控:若要使用某個對象,只需要從Spring框架中獲取需要使用的對象,不關心對象的創建過程,而是把創建對象的控制權反轉給了 Spring 框架.
? ? ? ?以前開發時,在哪需要對象,我們就在哪里new一個對象 ; 而現在,由于IOC容器(spring框架)負責對象的實例化、對象的初始化,對象和對象之間依賴關系、 對象的銷毀、對外提供對象的查找等操作,即對象的整個生命周期都是由容器來控制. 我們就把創建對象都交給spring框架完成,自己不new對象,需要使用對象時,直接從spring框架中獲取 .
(2). AOP (Aspect Oriented Programming):
? ? ? 面向切面編程 ,是一種編程思想(是面向對象的補充)? ?可以使用代理對象,在不修改源代碼的情況下,為目標方法添加額外的功能? ? ? ? ?
(3)一站式:
? ? ? ?涉及數據訪問層,web層層,可以對項目中的對象進行管理,還提供AOP功能,便于功能的擴展,對后端的項目統一管理
spring項目體系結構:

2. spring搭建:
(1). 創建一個Maven項目
(2). 導入核心 jar包
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
(3). 編寫 spring 配置文件
<?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.ff.spring.model.User"> </bean>
</beans>
(4). 編寫實體類 Dao接口 和 Service?
例如
package com.ffyc.spring.model;import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;public class Student {private int no;private String name ;public Student(){System.out.println("這是無參的構造方法");}public int getNo() {return no;}public void setNo(int no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
(5). 在resource中加入spring核心依賴?
<bean id="student" class="com.ffyc.spring.model.Student"></bean>
<bean id="studentDao" class="com.ffyc.spring.dao.StudentDao"></bean>
<bean id="studentService" class="com.ffyc.spring.service.StudentService">
例如:
<?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"><!--1.配置: 在spring中配置我們的類,把我們的類交給spring管理--><bean id="student" class="com.ffyc.spring.model.Student" scope="prototype"></bean><!--生成對象的作用域: spring框架默認創建一個對象(scope="singleton"),例如dao service對象.如果需要創建多個對象時,需要添加配置scope="prototype" 例如模型類student --><bean id="studentDao" class="com.ffyc.spring.dao.StudentDao"></bean><bean id="studentService" class="com.ffyc.spring.service.StudentService"><!--創建對象,并為屬性注入值--><!--方法一:用set方法為屬性賦值(推薦)--><property name="studentDao" ref="studentDao"></property><property name="student" ref="student"></property><!--方法二: 用構造方法賦值 --><!-- <constructor-arg name="studentDao" ref="studentDao"></constructor-arg>--></bean></beans>
(6)測試spring
ApplicationContext? applicationContext? = new ClassPathXmlApplicationContext("application.xml");
StudentService studentService =applicationContext.getBean("studentService",StudentService.class);
例如:
package com.ffyc.spring.test;import com.ffyc.spring.model.Student;
import com.ffyc.spring.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test1 {public static void main(String[] args) {/*之前如何使用對象: 在哪需要,就在哪new對現在如果使用對象: 把我們的類配置給spring框架,當spring框架啟動時就會為我們創建對象,需要時從spring框架里直接獲取就行*///啟動spring框架,獲取配置文件 現在獲取對象ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");Student student1 = applicationContext.getBean("student", Student.class);Student student2 = applicationContext.getBean("student", Student.class);System.out.println(student1);System.out.println(student2);StudentService studentService =applicationContext.getBean("studentService",StudentService.class);}
}
3.?xml配置方式屬性賦值
1. 定義:
? ? ? ? 指Spring 創建對象的過程中,將對象依賴屬性(簡單值 集合 對象)通過配置設置給該對象
實現 IOC 需要 DI 做支持, 也可以簡單理解為在創建對象時,為對象的屬性賦值方式有兩種:
(1). 通過set和get方法對屬性賦值
(2). 通過構造方法為屬性賦值
package com.ffyc.spring.service;import com.ffyc.spring.dao.StudentDao;
import com.ffyc.spring.model.Student;public class StudentService {StudentDao studentDao;Student student;/* //方法二: 通過構造方法為屬性賦值public StudentService(StudentDao studentDao, Student student) {this.studentDao = studentDao;this.student = student;}*/public void save(){/*StudentDao studentDao = new StudentDao();Student student = new Student();studentDao.*/studentDao.saveStudent(student);}//方法二: 通過set和get方法對屬性賦值public StudentDao getStudentDao() {return studentDao;}public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}
}
<?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"><!--1.配置: 在spring中配置我們的類,把我們的類交給spring管理--><bean id="student" class="com.ffyc.spring.model.Student" scope="prototype"></bean><!--生成對象的作用域: spring框架默認創建一個對象(scope="singleton"),例如dao service對象.如果需要創建多個對象時,需要添加配置scope="prototype" 例如模型類student --><bean id="studentDao" class="com.ffyc.spring.dao.StudentDao"></bean><bean id="studentService" class="com.ffyc.spring.service.StudentService"><!--創建對象,并為屬性注入值--><!--方法一:用set方法為屬性賦值(推薦)--><property name="studentDao" ref="studentDao"></property><property name="student" ref="student"></property><!--方法二: 用構造方法賦值 --><!-- <constructor-arg name="studentDao" ref="studentDao"></constructor-arg>--></bean></beans>
注意: spring 生成對象的作用域?scope ="singleton / prototype"?
? ? ? ? ?singleton(默認):? 在 Spring中只存在一個對象(bean) ,每次拿取都是同一個.
? ? ? ? ?prototype? ?:? ? ?? ?相當于getBean()的時候都會創建一個新的對象
4.?注解方式實現屬性賦值
(1). 開啟注解掃描
<context:component-scan base-package="包名"> </context:component-scan>
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--配置 spring對哪個包下的類進行掃描--><context:component-scan base-package="com.ffyc.spring"> </context:component-scan></beans>
(2). 注解自己所創建的對象
@Component (value=“user”)等同于 <bean id=“user” class=“”></bean>
@Service
@Repository
以上注解都可以實現創建對象功能,為了后續擴展功能,在不同的層使用不同的注解標記@Scope(value=“prototype”) 原型@Scope(value=“ singleton ”) 單例
package com.ffyc.spring.model;import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;@Component //等價于配置注解 <bean id="student" class="com.ffyc.spring.model.Student" scope="prototype"></bean>
@Scope(value = "prototype")
public class Student {private int no;private String name ;public Student(){System.out.println("這是無參的構造方法");}
}
package com.ffyc.spring.dao;import com.ffyc.spring.model.Student;
import org.springframework.stereotype.Repository;@Repository(value = "studentDao")
public class StudentDao {public void saveStudent(Student student){System.out.println("保存學生");}
}
package com.ffyc.spring.service;import com.ffyc.spring.dao.StudentDao;
import com.ffyc.spring.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;//service服務層 進行事務管理 數據訪問...
@Service
public class StudentService {@Autowired//相當于<property name="studentDao" ref="studentDao"></property>StudentDao studentDao;@AutowiredStudent student;/* //方法二: 通過構造方法為屬性賦值public StudentService(StudentDao studentDao, Student student) {this.studentDao = studentDao;this.student = student;}*/public void save(){/*StudentDao studentDao = new StudentDao();Student student = new Student();studentDao.*/studentDao.saveStudent(student);}//方法二: 通過set和get方法對屬性賦值public StudentDao getStudentDao() {return studentDao;}public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}
}
@Autowired: 是spring框架中提供的注解標簽,默認要求注入的對象必須存在,如果不存在,會報錯? 可以根據屬性 類型 或者 對象名字(value="名字")去spring框架中查找對象?
@Resource:?是Java中提供的注解標簽 同樣要求注入的對象也必須存在,如果不存在,會報錯?
?可以根據屬性 類型 或者 對象名字(value="名字")去spring框架中查找對象?
5. spring注解標簽分類
(1). 生成對象的注解
@Component
@Service
@Repository
(2). 屬性注入的注解
@Autowired
@Qualifier (value = "studao") 通過對象名查找
@Resource
