說明:本文部分內容參考其他優秀博客后結合自己實戰例子改編如下
Spring框架是個輕量級的Java EE框架。所謂輕量級,是指不依賴於容器就能運行的。Struts、Hibernate也是輕量級的。
輕量級框架是相對於重量級框架而言的,重量級框架必須依賴特定的容器,例如EJB框架就必須運行在Glassfish、JBoss等支持EJB的容器中,而不能運行在Tomcat中。——《Java Web整合開發 王者歸來》
Spring以IoC、AOP為主要思想,其中IoC,Inversion of Control 指控制反轉或反向控制。在Spring框架中我們通過配置創建類對象,由Spring在運行階段實例化、組裝對象。
AOP,Aspect Oriented Programming,面向切面編程,其思想是在執行某些代碼前執行另外的代碼,使程序更靈活、擴展性更好,可以隨便地添加、刪除某些功能。Servlet中的Filter便是一種AOP思想的實現。
Spring的核心是控制反轉(IoC)和面向切面(AOP)。
簡單來說,Spring是一個分層的JavaSE/EE full-stack(一站式) 輕量級開源框架。
即Spring在JavaEE的三層架構[表現層(Web層)、業務邏輯層(Service層)、數據訪問層(DAO層)]中,每一層均提供了不同的解決技術。如下:
• 表現層(Web層):Spring MVC
• 業務邏輯層(Service層):Spring的IoC
• 數據訪問層(DAO層):Spring的jdbcTemplate
Spring的優點
-
方便解耦,簡化開發 (高內聚低耦合)
Spring就是一個大工廠(容器),可以將所有對象創建和依賴關系維護,交給Spring管理
spring工廠是用於生成bean
-
AOP編程的支持
Spring提供面向切面編程,可以方便的實現對程序進行權限攔截、運行監控等功能
-
聲明式事務的支持
只需要通過配置就可以完成對事務的管理,而無需手動編程
-
方便程序的測試
Spring對Junit4支持,可以通過注解方便的測試Spring程序
-
方便集成各種優秀框架
Spring不排斥各種優秀的開源框架,其內部提供了對各種優秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
-
降低JavaEE API的使用難度
Spring 對JavaEE開發中非常難用的一些API(JDBC、JavaMail、遠程調用等),都提供了封裝,使這些API應用難度大大降低
一、Spring中的IoC操作
將對象的創建交由Spring框架進行管理。
IoC操作分為:IoC配置文件方式和IoC的注解方式。
1. IoC入門案例
(1)導入Spring框架中的相關jar包,這里只導入Spring的Core模塊(Core模塊是框架的核心類庫)下的jar包(使用IoC的基本操作,並不需要導入spring-framework-4.2.0的所有jar包,
只導入spring-beans
、spring-core
、spring-context、
、spring-expression
spring-aop
這5個jar包),以及 支持日志輸出的 commons-logging 和 log4j 的jar包;
(2)創建一個普通的Java類,並在該類中創建方法,如下:
package com.wm103.ioc; /** * Created by DreamBoy on 2018/3/17. */ public class User { public void add() { System.out.println("User Add Method."); } @Override public String toString() { return "This is a user object."; } }
(3)創建Spring的配置文件,進行Bean的配置
Spring的核心配置文件名稱和位置不是固定的。但官方件建議將該核心配置文件放在src目錄下,且命名為 applicationContext.xml。
這里為了方便,將核心配置文件放在src目錄下,並命名為 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">
<!-- 配置service
<bean> 配置需要創建的對象
id :用於之后從spring容器獲得實例時使用的
class :需要創建實例的全限定類名
-->
<bean id="user" class="com.wm103.ioc.User"></bean>
</beans>
(4)編寫測試類進行測試,通過配置文件創建類對象
package com.wm103.ioc; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by DreamBoy on 2018/3/17. */ public class TestIoc { @Test public void runUser() { // 1. 加載Spring配置文件,根據創建對象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2. 得到配置創建的對象 User user = (User) context.getBean("user"); System.out.println(user); user.add(); } }
2. Spring的bean管理(配置文件)
Bean實例化的方式
在Spring中通過配置文件創建對象。
Bean實例化三種方式實現:
(1)使用類的無參數構造創建,如:
<!-- 等同於 user = new com.wm103.ioc.User(); --> <bean id="user" class="com.wm103.ioc.User"></bean>
(2)使用靜態工廠創建
如果一個Bean不能通過new直接實例化,而是通過工廠類的某個靜態方法創建的,需要把<bean>
的class
屬性配置為工廠類。如:
<!-- 等同於 user = com.wm103.ioc.UserFactory.createInstance(); --> <bean id="user" class="com.wm103.ioc.UserFactory" factory-method="createInstance"></bean>
(3)使用實例工廠創建
如果一個Bean不能通過new直接實例化,而是通過工廠類的某個實例方法創建的,需要先配置工廠的<bean>
標簽,然后在需要創建的對象的bean標簽的factory-bean
屬性配置為工廠類對象,factory-method
屬性配置為產生實例的方法。如:
<!-- 等同於 userFactory = new com.wm103.ioc.UserFactory(); --> <bean id="userFactory" class="com.wm103.ioc.UserFactory"></bean> <!-- 等同於 user = userFactory.createInstance(); --> <bean id="user" factory-bean="userFactory" factory-method="createInstance"></bean>
Bean標簽的常用屬性
(1)id屬性:用於指定配置對象的名稱,不能包含特殊符號。
(2)class屬性:創建對象所在類的全路徑。
(3)name屬性:功能同id屬性一致。但是在name屬性值中可以包含特殊符號。
(4)scope屬性
-
singleton:默認值,單例
單例模式下,在程序下只有一個實例。非單態模式下,每次請求該Bean,都會生成一個新的對象。
-
prototype:多例
-
request:創建對象后將對象存放到request域
-
session:創建對象后將對象存放到session域
-
globalSession:創建對象后將對象存放到globalSession域
3. DI的依賴注入
屬性依賴注入
-
依賴注入方式:手動裝配 和 自動裝配
-
手動裝配:一般進行配置信息都采用手動
基於xml裝配:構造方法、setter方法
-
自動裝配(基於注解裝配):
屬性注入指創建對象時,向類對象的屬性設置屬性值。
在Spring框架中支持set方法注入和有參構造函數注入,即創建對象后通過set方法設置屬性或采用有參構造函數創建對象並初始化屬性。
3.1 使用有參構造函數注入屬性
案例:Student.java 提供有單參的構造方法
package com.wm103.ioc; public class Student { private String name; public Student(String name) { this.name = name; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + '}'; } }
spring bean的配置:
<bean id="student" class="com.wm103.ioc.Student"> <constructor-arg name="name" value="DreamBoy"></constructor-arg> </bean>
提供有多參的構造方法
public class User { private Integer uid; private String username; private Integer age; public User(Integer uid, String username) { super(); this.uid = uid; this.username = username; } public User(String username, Integer age) { super(); this.username = username; this.age = age; }
spring bean的配置:
<!-- 構造方法注入 * <constructor-arg> 用於配置構造方法一個參數argument name :參數的名稱 value:設置普通數據 ref:引用數據,一般是另一個bean id值 index :參數的索引號,從0開始 。如果只有索引,匹配到了多個構造方法時,默認使用第一個。 type :確定參數類型 例如:使用名稱name <constructor-arg name="username" value="jack"></constructor-arg> <constructor-arg name="age" value="18"></constructor-arg> 例如2:【類型type 和 索引 index】 <constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg> <constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg> --> <bean id="userId" class="com.itheima.f_xml.a_constructor.User" > <constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg> <constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg> </bean>
創建Student對象進行測試:
@Test public void runStudent() { // 1. 加載Spring配置文件,根據創建對象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2. 得到配置創建的對象 Student student = (Student) context.getBean("student"); System.out.println(student); }
3.2 使用set方法注入屬性
案例:Teacher.java 提供屬性的set方法
package com.wm103.ioc; public class Teacher { private String name; public void setName(String name) { this.name = name; } @Override public String toString() { return "Teacher{" + "name='" + name + '\'' + '}'; } }
bean的配置:
<bean id="teacher" class="com.wm103.ioc.Teacher"> <property name="name" value="Teacher Wu"></property> </bean>
創建Teacher對象進行測試:
public void runTeacher() { // 1. 加載Spring配置文件,根據創建對象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2. 得到配置創建的對象 Teacher teacher = (Teacher) context.getBean("teacher"); System.out.println(teacher); }
3.3 注入對象類型屬性
以三層架構中的service層和dao層為例,為了讓service層使用dao層的類創建的對象,需要將dao對象注入到service層類中。具體實現過程中如下:
(1)創建service類、dao層接口、dao類,如下: UserService.java
package com.wm103.exp; public class UserService { private UserDao userDao; // 聲明為接口類型,降低service層與dao層的耦合度,不依賴於dao層的具體實現 public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add() { System.out.println("UserService Add..."); this.userDao.add(); } }
UserDao.java
package com.wm103.exp; /** * 暴露給service層的接口 * Created by DreamBoy on 2018/3/17. */ public interface UserDao { void add(); }
UserDaoImpl.java
package com.wm103.exp; /** * 接口UserDao的具體實現 * Created by DreamBoy on 2018/3/17. */ public class UserDaoImpl implements UserDao { @Override public void add() { System.out.println("UserDaoImpl Add..."); } }
(2)在配置文件中注入關系,如下:
<!-- 配置service和dao對象 --> <!-- 因為service依賴於dao,所以先進行dao對象的bean配置 --> <bean id="userDaoImpl" class="com.wm103.exp.UserDaoImpl"></bean> <bean id="userService" class="com.wm103.exp.UserService"> <!-- 注入dao對象 name屬性值為:service中的某一屬性名稱 ref屬性值為:被引用的對象對應的bean標簽的id屬性值 --> <property name="userDao" ref="userDaoImpl"></property> </bean>
(3)創建測試方法進行測試,如下:
@Test public void runUserService() { // 1. 加載Spring配置文件,根據創建對象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2. 得到配置創建的對象 UserService userService = (UserService) context.getBean("userService"); userService.add(); }
3.4 p名稱空間注入屬性
之前提到了一種set方法的屬性注入方式,這里將介紹另一種屬性注入的方式,名為 p名稱空間注入。對比set方法的屬性注入方式,核心配置文件配置修改如下:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="teacher" class="com.wm103.ioc.Teacher" p:name="Teacher Wu"></bean> </beans>
3.5 注入復雜類型屬性
對象注入復雜類型屬性,如數組、List、Map、Properties。
案例:PropertyDemo.java
package com.wm103.ioc; import java.util.List; import java.util.Map; import java.util.Properties; public class PropertyDemo { private String[] arrs; private List<String> list; private Map<String, String> map; private Properties properties; public String[] getArrs() { return arrs; } public void setArrs(String[] arrs) { this.arrs = arrs; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
bean配置文件,內容如下:
<?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="prop" class="com.wm103.ioc.PropertyDemo"> <!-- 注入數組 --> <property name="arrs"> <list> <value>Value 1 of Array</value> <value>Value 2 of Array</value> <value>Value 3 of Array</value> </list> </property> <!-- 注入List集合 --> <property name="list"> <list> <value>Value 1 of List</value> <value>Value 2 of List</value> <value>Value 3 of List</value> </list> </property> <!-- 注入Map集合 --> <property name="map"> <map> <entry key="key1" value="Value 1 of Map"></entry> <entry key="key2" value="Value 2 of Map"></entry> <entry key="key3" value="Value 3 of Map"></entry> </map> </property> <!-- 注入Properties --> <property name="properties"> <props> <prop key="username">root</prop> <prop key="password">123456</prop> </props> </property> </bean> </beans>
創建PropertyDemo對象進行測試:
@Test public void runPropertyDemo() {
// 1. 加載Spring配置文件,根據創建對象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 得到配置創建的對象
PropertyDemo pd = (PropertyDemo) context.getBean("prop");
System.out.println(pd);
System.out.println(Arrays.toString(pd.getArrs()));
System.out.println(pd.getList());
System.out.println(pd.getMap());
System.out.println(pd.getProperties());
}
IoC和DI的區別
IoC,控制反轉,將傳統的對象創建流程轉變為交由框架進行創建和管理。在Spring中,對象的創建交給Spring進行配置。它包括依賴注入。
DI,依賴注入,向類的屬性設置值。
IoC與DI的關系:依賴注入不能單獨存在,需要在IoC基礎之上完成操作。
4. Spring的bean管理(注解)
注解是代碼中特殊的標記,使用注解可以完成特定的功能。注解可以使用在類、方法或屬性上,寫法如:@注解名稱(屬性名稱=屬性值)
。
Spring的bean管理注解方式,案例如下。
- 注解:就是一個類,使用@注解名稱
- 開發中:使用注解 取代 xml配置文件。
1.@Component取代<bean class="">
@Component("id") 取代 <bean id="" class="">
2. web開發,提供3個@Component注解衍生注解(功能一樣)取代@Repository :dao層
@Service:service層
@Controller:web層
3. 依賴注入,給私有字段設值,也可以給setter方法設值- 普通值:
@Value(" ")
- 引用值:
方式1:按照【類型】注入
@Autowired
方式2:按照【名稱】注入1
@Autowired
@Qualifier("名稱")
方式3:按照【名稱】注入2
@Resource("名稱")
4.生命周期初始化:@PostConstruct
銷毀:@PreDestroy
5.作用域@Scope("prototype") 多例
注解使用前提,添加命名空間,讓spring掃描含有注解類
- 普通值:
4.1 Spring注解開發准備
(1)導入jar包:
- 導入基本的jar包:
commons-logging
、log4j
、spring-beans
、spring-context
、spring-core
、spring-expression
相關jar包。 - 導入AOP的jar包:
spring-aop
jar包。
(2)創建類、方法
User.java
package com.wm103.anno; import org.springframework.stereotype.Component; public class User { public void add() { System.out.println("User Add Method."); } }
(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" 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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 開啟注解掃描 (1)到包中掃描類、方法、屬性上是否有注解 --> <context:component-scan base-package="com.wm103"></context:component-scan> <!-- (2)只掃描屬性上的注解 --> <!--<context:annotation-config></context:annotation-config>--> </beans>
4.2 注解創建對象
在創建對象的類上面使用注解實現,如:
@Component(value="user") public class User {
創建測試類 TestAnno.java和測試方法,如:
package com.wm103.anno; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAnno { @Test public void runUser() {
// 1. 加載Spring配置文件,根據創建對象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 得到配置創建的對象 User user = (User) context.getBean("user"); user.add(); } }
除了上述提到的 @Component
注解外,Spring中還提供了@Component
的3個衍生注解,其功能就目前來說是一致的,均是為了創建對象。
- @Controller :WEB層
- @Service :業務層
-
@Repository :持久層
以單例或多實例方式創建對象,默認為單例,多例對象設置注解如下:
@Component(value="user") @Scope(value="prototype") public class User {
4.3 注解注入屬性
案例:創建Service類和Dao類,並在Service中注入Dao對象。如下:
(1)創建Dao和Service對象
UserDao.java
package com.wm103.anno; import org.springframework.stereotype.Repository; @Repository(value="userDao") public class UserDao { public void add() { System.out.println("UserDao Add Method."); } }
UserService.java
package com.wm103.anno; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service(value="userService") public class UserService { public void add() { System.out.println("UserService Add Method."); userDao.add(); } }
(2)在Service類中定義UserDao類型的屬性,並使用注解完成對象的注入
@Autowired
:自動注入或自動裝配,是根據類名去找到類對應的對象來完成注入的。
@Autowired private UserDao userDao;
或者 @Resource
@Resource(name="userDao") private UserDao userDao;
其中該注解的name屬性值為注解創建Dao對象的value屬性的值。
這兩種注解方式都不一定要為需要注入的屬性定義set方法。
(3)創建測試方法
@Test public void runUserService() {
// 1. 加載Spring配置文件,根據創建對象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 得到配置創建的對象 UserService userService = (UserService) context.getBean("userService"); userService.add(); }
注:配置文件和注解混合使用
1)創建對象的操作一般使用配置文件方式實現;
2)注入屬性的操作一般使用注解方式實現。
二、Spring框架—面向切面編程(AOP)
1. 什么是AOP
-
在軟件業,AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。AOP是OOP(面向對象編程)的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生范型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
-
AOP采取橫向抽取機制,取代了傳統縱向繼承體系重復性代碼
-
經典應用:事務管理、性能監視、安全檢查、緩存 、日志等
-
Spring AOP使用純Java實現,不需要專門的編譯過程和類加載器,在運行期通過代理方式向目標類織入增強代碼
-
AspectJ是一個基於Java語言的AOP框架,Spring2.0開始,Spring AOP引入對Aspect的支持,AspectJ擴展了Java語言,提供了一個專門的編譯器,在編譯時提供橫向代碼的織入
2. AOP實現原理
-
aop底層將采用代理機制進行實現。
-
接口 + 實現類 :spring采用 jdk 的動態代理Proxy。
-
實現類:spring 采用 cglib字節碼增強。
3. AOP術語【掌握】
1. target:目標類,需要被代理的類。例如:UserService
2. Joinpoint(連接點):所謂連接點是指那些可能被攔截到的方法。例如:所有的方法
3. PointCut 切入點:已經被增強的連接點。例如:addUser()
4. advice 通知/增強,增強代碼。例如:after、before
5. Weaving(織入):是指把增強advice應用到目標對象target來創建新的代理對象proxy的過程.
6. proxy 代理類
7. Aspect(切面): 是切入點pointcut和通知advice的結合
一個線是一個特殊的面。
一個切入點和一個通知,組成成一個特殊的面。
4. AOP實現方式
有四種實現方式:手動方式,半自動方式,全自動方式,注解方式
4.1 手動方式
4.1.1 JDK動態代理
- JDK動態代理 對“裝飾者”設計模式 簡化。使用前提:必須有接口
1. 目標類:接口 + 實現類(UserServiceImpl)
public interface UserService { public void addUser(); public void updateUser(); public void deleteUser(); }
2. 切面類:用於存通知 MyAspect
public class MyAspect { public void before(){ System.out.println("雞首"); } public void after(){ System.out.println("牛后"); } }
3. 工廠類:編寫工廠生成代理
public class MyBeanFactory { public static UserService createService(){ //1 目標類 final UserService userService = new UserServiceImpl(); //2切面類 final MyAspect myAspect = new MyAspect(); /* 3 代理類:將目標類(切入點)和 切面類(通知) 結合 --> 切面 * Proxy.newProxyInstance * 參數1:loader ,類加載器,動態代理類 運行時創建,任何類都需要類加載器將其加載到內存。 * 一般情況:當前類.class.getClassLoader(); * 目標類實例.getClass().get... * 參數2:Class[] interfaces 代理類需要實現的所有接口 * 方式1:目標類實例.getClass().getInterfaces() ;注意:只能獲得自己接口,不能獲得父元素接口 * 方式2:new Class[]{UserService.class} * 例如:jdbc 驅動 --> DriverManager 獲得接口 Connection * 參數3:InvocationHandler 處理類,接口,必須進行實現類,一般采用匿名內部 * 提供 invoke 方法,代理類的每一個方法執行時,都將調用一次invoke * 參數31:Object proxy :代理對象 * 參數32:Method method : 代理對象當前執行的方法的描述對象(反射) * 執行方法名:method.getName() * 執行方法:method.invoke(對象,實際參數) * 參數33:Object[] args :方法實際參數 * */ UserService proxService = (UserService)Proxy.newProxyInstance( MyBeanFactory.class.getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //前執行 myAspect.before(); //執行目標類的方法 Object obj = method.invoke(userService, args); //后執行 myAspect.after(); return obj; } }); return proxService; } }
4. 測試
@Test public void demo01(){ UserService userService = MyBeanFactory.createService(); userService.addUser(); userService.updateUser(); userService.deleteUser(); }
4.1.2 CGLIB字節碼增強
- 沒有接口,只有實現類。
- 采用字節碼增強框架 cglib,在運行時 創建目標類的子類,從而對目標類進行增強。
工廠類
public class MyBeanFactory { public static UserServiceImpl createService(){ //1 目標類 final UserServiceImpl userService = new UserServiceImpl(); //2切面類 final MyAspect myAspect = new MyAspect(); // 3.代理類 ,采用cglib,底層創建目標類的子類 //3.1 核心類 Enhancer enhancer = new Enhancer(); //3.2 確定父類 enhancer.setSuperclass(userService.getClass()); /* 3.3 設置回調函數 , MethodInterceptor接口 等效 jdk InvocationHandler接口 * intercept() 等效 jdk invoke() * 參數1、參數2、參數3:以invoke一樣 * 參數4:methodProxy 方法的代理 * * */ enhancer.setCallback(new MethodInterceptor(){ @Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { //前 myAspect.before(); //執行目標類的方法 Object obj = method.invoke(userService, args); // * 執行代理類的父類 ,執行目標類 (目標類和代理類 父子關系) methodProxy.invokeSuper(proxy, args); //后 myAspect.after(); return obj; } }); //3.4 創建代理 UserServiceImpl proxService = (UserServiceImpl) enhancer.create(); return proxService; } }
4.2 半自動
- 讓spring 容器創建代理對象,從spring容器中手動的獲取代理對象
4.2.1 目標類
public interface UserService { public void addUser(); public void updateUser(); public void deleteUser(); }
4.2.2 切面類
package com.spring.aop; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 切面類:用於存通知 * 切面類中確定通知,需要實現不同接口,接口就是規范,從而就確定方法名稱。 * 采用“環繞通知” MethodInterceptor * */ public class MyAspect implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { Object result = null; try { System.out.println("--環繞通知開始--開啟事務--自動--"); long start = System.currentTimeMillis(); //手動執行目標方法(有返回參數 則需返回值) result = invocation.proceed(); long end = System.currentTimeMillis(); System.out.println("總共執行時長" + (end - start) + " 毫秒"); System.out.println("--環繞通知結束--提交事務--自動--"); } catch (Throwable t) { System.out.println("--環繞通知--出現錯誤"); } return result; } }
4.2.3 Spring 配置
<!-- 1 創建目標類 --> <bean id="userServiceId" class="com.spring.aop.UserServiceImpl"></bean> <bean id="orderServiceId" class="com.spring.aop.OrderService"></bean> <!-- 2 創建切面類(通知) --> <bean id="myAspectId" class="com.spring.aop.MyAspect"></bean> <!-- 3 創建代理類 * 使用工廠bean FactoryBean ,底層調用 getObject() 返回特殊bean * ProxyFactoryBean 用於創建代理工廠bean,生成特殊代理對象 interfaces : 確定接口們 通過<array>可以設置多個值 只有一個值時,value="" target : 確定目標類 interceptorNames : 通知 切面類的名稱,類型String[],如果設置一個值 value="" optimize :強制使用cglib <property name="optimize" value="true"></property> 底層機制 如果目標類有接口,采用jdk動態代理 如果沒有接口,采用cglib 字節碼增強 如果聲明 optimize = true ,無論是否有接口,都采用cglib --> <bean id="proxyServiceId" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interfaces" value="com.spring.aop.UserService"></property> <property name="target" ref="userServiceId"></property> <property name="interceptorNames" value="myAspectId"></property> </bean>
4.2.4 測試
@Test public void demo01(){ // 1. 加載Spring配置文件,根據創建對象 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //獲得代理類 UserService userService = (UserService) applicationContext.getBean("proxyServiceId"); userService.addUser(); userService.updateUser(); userService.deleteUser(); }
4.3 全自動
- 從spring 容器獲得目標類,如果配置aop,spring將自動生成代理。
4.3.1 切面類
package com.spring.aop; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 切面類:用於存通知 * 切面類中確定通知,需要實現不同接口,接口就是規范,從而就確定方法名稱。 * 采用“環繞通知” MethodInterceptor * */ public class MyAspectZD implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { Object result = null; try { System.out.println("--環繞通知開始--開啟事務--自動--"); long start = System.currentTimeMillis(); //手動執行目標方法(有返回參數 則需返回值) result = invocation.proceed(); long end = System.currentTimeMillis(); System.out.println("總共執行時長" + (end - start) + " 毫秒"); System.out.println("--環繞通知結束--提交事務--自動--"); } catch (Throwable t) { System.out.println("--環繞通知--出現錯誤"); } return result; } }
切面類中根據獲取到的注解來通知,示例如下:
package com.demo.aop; import com.demo.annotation.DataSourceChange; import com.demo.datasource.DynamicDataSourceHolder; import lombok.extern.slf4j.Slf4j; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import java.lang.reflect.Method; @Slf4j public class DynamicDataSourceAspectDao implements MethodInterceptor { /** * 切面類:用於存通知 * 切面類中確定通知,需要實現不同接口,接口就是規范,從而就確定方法名稱。 * 采用“環繞通知” MethodInterceptor * @param invocation * @return * @throws Throwable */ @Override public Object invoke(MethodInvocation invocation) throws Throwable { log.info("around"); Object result = null; //獲取代理接口或者類 Object target = joinPoint.getTarget(); String methodName = joinPoint.getSignature().getName(); //獲取目標類的接口,所以注解@DataSourceChange需要寫在接口上 //Class<?>[] clazz = target.getClass().getInterfaces(); //獲取目標類,所以注解@DataSourceChange需要寫在類里面 Class<?>[] clazz = new Class<?>[]{target.getClass()}; Class<?>[] parameterTypes = invocation.getMethod().getParameterTypes(); try { Method method = clazz[0].getMethod(methodName, parameterTypes); //判斷是否使用了該注解 if (method != null && method.isAnnotationPresent(DataSourceChange.class)) { DataSourceChange data = method.getAnnotation(DataSourceChange.class); if (data.slave()) { DynamicDataSourceHolder.setDataSource(DynamicDataSourceHolder.DB_SLAVE); } else { DynamicDataSourceHolder.setDataSource(DynamicDataSourceHolder.DB_MASTER); } } System.out.println("--環繞通知開始--開啟事務--自動--"); long start = System.currentTimeMillis(); //手動執行目標方法(有返回參數 則需返回值) result = invocation.proceed(); long end = System.currentTimeMillis(); System.out.println("總共執行時長" + (end - start) + " 毫秒"); System.out.println("--環繞通知結束--提交事務--自動--"); } catch (Throwable ex) { System.out.println("--環繞通知--出現錯誤"); log.error(String.format("Choose DataSource error, method:%s, msg:%s", methodName, ex.getMessage())); } finally { DynamicDataSourceHolder.clearDataSource(); } return result; } }
4.3.2 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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 1 創建目標類 --> <bean id="userServiceId" class="com.spring.aop.UserServiceImpl"></bean> <bean id="orderServiceId" class="com.spring.aop.OrderService"></bean> <!-- 2 創建切面類(通知) --> <bean id="myAspectId" class="com.spring.aop.MyAspectZD"></bean> <!-- 3 aop編程 3.1 導入命名空間 3.2 使用 <aop:config>進行配置 默認情況下會采用JDK的動態代理實現AOP(只能對實現了接口的類生成代理,而不能針對類) 如果proxy-target-class="true" 聲明時強制使用cglib代理(針對類實現代理) <aop:pointcut> 切入點 ,從目標對象獲得具體方法 <aop:advisor> 特殊的切面,只有一個通知 和 一個切入點 advice-ref 通知引用 pointcut-ref 切入點引用
order 切面順序 3.3 切入點表達式 execution(* com.spring.aop..*.*(..)) 選擇方法 返回值任意 包及所有子包 類名任意 方法名任意 參數任意 例如:匹配所有”set”開頭的方法:execution(* set*(..)) --> <aop:config proxy-target-class="true"> <aop:pointcut id="myPointCut" expression="execution(* com.spring.aop..*.update*(..))" /> <aop:advisor advice-ref="myAspectId" pointcut-ref="myPointCut" order="1" /> </aop:config> </beans>
4.3.3 測試
@Test public void demo01(){ // 1. 加載Spring配置文件,根據創建對象 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 獲得目標類 UserService userService = (UserService) applicationContext.getBean("userServiceId"); userService.addUser(); userService.updateUser(); userService.deleteUser(); // 獲得目標類 OrderService orderService = (OrderService) applicationContext.getBean("orderServiceId"); orderService.addOrder(); orderService.updateOrder(); orderService.deleteOrder(); }
4.4 注解方式
Spring AOP基於注解的“零配置”方式實現
1. 為了在Spring中啟動@AspectJ支持,需要在類加載路徑下新增兩個AspectJ庫:aspectjweaver-1.74.jar和aspectjrt-1.74.jar。除此之外,Spring AOP還需要依賴一個aopalliance-1.0.jar包
2. 定義一個類似XmlAopDemoOrder.java這樣的切面

package com.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /* * 聲明一個切面 * 自動注解AOP */ @Aspect @Component(value="xmlAopDemoOrder") public class XmlAopDemoOrder { // 配置切點 及要傳的參數 @Pointcut("execution(* com.spring.aop.OrderService.GetDemoOrder(..)) && args(id)") public void pointCut(int id) { } // 配置連接點 方法開始執行前通知 @Before("pointCut(id)") public void beforeLog(int id) { System.out.println("開始執行前置通知 日志記錄:"+id); } // 方法執行完后通知 @After("pointCut(id)") public void afterLog(int id) { System.out.println("方法執行完后置通知 日志記錄:"+id); } // 執行成功后通知 @AfterReturning("pointCut(id)") public void afterReturningLog(int id) { System.out.println("方法成功執行后通知 日志記錄:"+id); } // 拋出異常后通知 @AfterThrowing("pointCut(id)") public void afterThrowingLog(int id) { System.out.println("方法拋出異常后通知 日志記錄"+id); } // 環繞通知 @Around("pointCut(id)") public Object aroundLog(ProceedingJoinPoint joinpoint, int id) { Object result = null; try { System.out.println("環繞通知開始 日志記錄"+id); long start = System.currentTimeMillis(); //有返回參數 則需返回值 result = joinpoint.proceed(); long end = System.currentTimeMillis(); System.out.println("總共執行時長" + (end - start) + " 毫秒"); System.out.println("環繞通知結束 日志記錄"+id); } catch (Throwable t) { System.out.println("出現錯誤"); } return result; } }
3. 定義一個業務組件,如:

package com.spring.aop; import org.springframework.stereotype.Component; /* * 使用注解創建對象 * @Component取代<bean class=""> * @Component("id") 取代 <bean id="" class=""> */ @Component(value="orderService") public class OrderService { public void addOrder() { System.out.println("添加訂單"); } public void updateOrder() { System.out.println("更新訂單"); } public void deleteOrder() { System.out.println("刪除訂單"); } public void GetDemoOrder(int id) { System.out.println("使用注解獲取訂單" + id); } }
4. 在bean.xml中加入下面配置
<!-- 開啟注解掃描 (1)到包及其子包下面自動掃描類、方法、屬性上是否有注解 --> <context:component-scan base-package="com.spring.helloworld,com.spring.aop"></context:component-scan> <!-- 啟動AspectJ支持,開啟自動注解AOP 使用配置注解,首先我們要將切面在spring上下文中聲明成自動代理bean 默認情況下會采用JDK的動態代理實現AOP(只能對實現了接口的類生成代理,而不能針對類) 如果proxy-target-class="true" 聲明時強制使用cglib代理(針對類實現代理) --> <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> --> <aop:aspectj-autoproxy/>
5. 測試
@Test public void demo01(){ //獲得目標類 UserService userService = (UserService) applicationContext.getBean("userServiceId"); userService.addUser(); userService.updateUser(); userService.deleteUser(); //獲得目標類 OrderService orderService = (OrderService) applicationContext.getBean("orderServiceId"); orderService.addOrder(); orderService.updateOrder(); orderService.deleteOrder(); //AOP使用注解 orderService.GetDemoOrder(1); }
本人推薦使用注解方式實現AOP或者全自動方式