AOP-切入点表达式1-bean表达式定义切入点

指示符 作用
bean 匹配指定类型的方法
AOP-切入点表达式1-bean表达式定义切入点_第1张图片
项目结构

pom.xml


    4.0.0
    com.ssttisme
    spring_aop_expression
    0.0.1-SNAPSHOT
    
        
            org.springframework
            spring-context
            4.3.9.RELEASE
        
        
        
            org.aspectj
            aspectjrt
            1.8.9
        
        
            org.aspectj
            aspectjweaver
            1.8.9
        
    

applicationContext.xml



    
    
    
    

package com.service;

public interface RoleService {
    void save(String role);
    int update(String role);
}
package com.service.impl;

import org.springframework.stereotype.Component;

import com.service.RoleService;
@Component
public class RoleServiceImpl implements RoleService{

    @Override
    public void save(String role) {
        System.out.println("save role "+role);
    }

    @Override
    public int update(String role) {
        System.out.println("update role "+role);
        return 1;
    }

}
package com.aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**切面*/
@Aspect
@Component
public class LogManager {
    @Before("bean(*ServiceImpl)")
    public void beforeAdvice(){
        System.out.println("开始日志。。。");
    }
    @After("bean(roleServiceImpl)")
    public void afterAdvice(){
        System.out.println("结束日志。。。");
    }
}

package com.app;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.service.RoleService;

public class RunApp {
    private static ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext();
    //初始化
    public static void init(){
        ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    //释放资源
    public static void destory(){
        ctx.close();
    }
    public static void main(String[] args) {
        init();
        RoleService roleService=ctx.getBean("roleServiceImpl",RoleService.class);
        roleService.update("normal user");
        destory();
    }
}

运行结果

开始日志。。。
update role normal user
结束日志。。。

你可能感兴趣的:(AOP-切入点表达式1-bean表达式定义切入点)