1. 程式人生 > 實用技巧 >centos7關機自動進行遠端伺服器備份

centos7關機自動進行遠端伺服器備份

1. 什麼是AOP

2. 匯入依賴

匯入AOP織入包

1  <dependency>
2             <groupId>org.aspectj</groupId>
3             <artifactId>aspectjweaver</artifactId>
4             <version>1.9.4</version>
5         </dependency>

3.spring AOP的簡單實現(方法一:使用spring API介面)

1.介面

1 public
interface UserService { 2 public void add(); 3 public void delete(); 4 public void update(); 5 public void select(); 6 }

2.實現類

 1 public class UserServiceImpl implements UserService {
 2     public void add() {
 3         System.out.println("執行了新增方法");
 4     }
 5 
 6     public void
delete() { 7 System.out.println("執行了刪除方法"); 8 } 9 10 public void update() { 11 System.out.println("執行了更新方法"); 12 } 13 14 public void select() { 15 System.out.println("執行了查詢方法"); 16 } 17 }

3.新增日誌

Log日誌:MethodBeforeAdvice為前置通知,連線點為方法前

重寫before方法

1 //method:要執行的目標物件的方法
2 //objects:引數 args 3 //o:目標物件 target 4 public class Log implements MethodBeforeAdvice { 5 public void before(Method method, Object[] objects, Object o) throws Throwable { 6 System.out.println(o.getClass().getName()+"方法"); 7 } 8 }

AfterLog日誌:AfterReturningAdvice為後置通知,連線點為方法後

1 //o:返回值
2 public class AfterLog implements AfterReturningAdvice {
3     public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
4         System.out.println("執行的方法為"+method.getName()+"返回值為"+o);
5     }
6 }

4.配置

建立applicationContext.xml配置檔案

具體步驟:1.註冊bean,註冊介面的實現類和需要新增的Log日誌方法,格式為:<bean id="任意取" class="全類名和包名"></bean>

     2.配置aop,配置aop時需要匯入aop約束

     3.配置aop切入點,切入點為介面的實現類

     4.配置環繞通知,環繞通知的ref為需要新增的Log日誌

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 7            http://www.springframework.org/schema/aop
 8            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
 9            ">
10     <!-- 註冊bean-->
11     <bean id="UserServiceImpl" class="com.dz.serviceImpl.UserServiceImpl"></bean>
12     <bean id="Log" class="com.dz.log.Log"></bean>
13     <bean id="AfterLog" class="com.dz.log.AfterLog"></bean>
14     <!-- 使用原生的spring API介面-->
15     <!-- 配置aop:需要匯入aop的約束-->
16     <aop:config>
17         <!-- 切入點:expression表示式,execution表示要執行的位置-->
18         <aop:pointcut id="pointcut" expression="execution(* com.dz.serviceImpl.UserServiceImpl.*(..))"/>
19 
20         <!-- 新增環繞通知-->
21         <aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut"></aop:advisor>
22         <aop:advisor advice-ref="Log" pointcut-ref="pointcut"></aop:advisor>
23     </aop:config>
24 
25 </beans>

5.測試

    1.首先新增配置好的applicationContext.xml配置檔案

    2.動態代理的是介面,配置檔案獲取的bean為介面的實現類的

1 public class MyTest {
2     public static void main(String[] args) {
3         ClassPathXmlApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
4         //動態代理的是介面
5         UserService userService =(UserService) Context.getBean("UserServiceImpl");
6         userService.add();
7     }
8 }

4. spring AOP的簡單實現(方法二:使用自定義類)

1. 建立自定義類,定義方法

1 public class DiyPiontCut {
2     public void before(){
3         System.out.println("執行了Before方法");
4     }
5     public void after(){
6         System.out.println("執行了after方法");
7     }
8 }

2.applicationContext.xml配置檔案

  註冊bean為自定義類,自定義切面,ref引用的是註冊的bean

  建立切入點,切入點為介面的實現類

  建立通知,通知使用的方法為自定義類定義的方法,切入點為aop建立的切入點

 1  <!-- 自定義類-->
 2     <bean id="diy" class="com.dz.diy.DiyPiontCut"></bean>
 3     <aop:config>
 4         <!-- 自定義切面,ref表示要引用的類-->
 5         <aop:aspect ref="diy">
 6         <!-- 切入點-->
 7         <aop:pointcut id="point" expression="execution(* com.dz.serviceImpl.UserServiceImpl.*(..))"/>
 8         <!-- 通知-->
 9             <aop:before method="before" pointcut-ref="point"></aop:before>
10             <aop:after method="after" pointcut-ref="point"></aop:after>
11         </aop:aspect>
12     </aop:config>