spring复习:(39)注解方式的ProxyFactoryBean

这篇具有很好参考价值的文章主要介绍了spring复习:(39)注解方式的ProxyFactoryBean。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、定义接口

package cn.edu.tju.study.service;

public interface MyService {
    void myMethod();
}

二、定义实现类:

package cn.edu.tju.study.service;

public class MyServiceImpl implements MyService{
    @Override
    public void myMethod() {
        System.out.println("hello world...");
    }
}

三、定义配置类,配置业务bean、advisor bean、ProxyFactoryBean

package cn.edu.tju.study.service;



import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

@Configuration
public class MyConfig {
    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }

    //MethodBeforeAdvice
    @Bean
    public MethodBeforeAdvice beforeAdvice() {
        MethodBeforeAdvice advice = new MethodBeforeAdvice() {

            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                System.out.println("will call :"+ method);
                System.out.println("args are: "+args);
            }
        };
        return advice;
    }



    //MethodInterceptor
    @Bean
    public MethodInterceptor methodInterceptor() {
        MethodInterceptor methodInterceptor = new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("before method invoke......");
                Object re = invocation.proceed();
                System.out.println("after method invoke......");
                return re;
            }
        };
        return methodInterceptor;
    }

    //MethodInterceptor2
    @Bean
    public MethodInterceptor methodInterceptor2() {
        MethodInterceptor methodInterceptor = new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("before method invoke2......");
                Object re = invocation.proceed();
                System.out.println("after method invoke2......");
                return re;
            }
        };
        return methodInterceptor;
    }


    //ProxyFactoryBean
    @Bean
    public ProxyFactoryBean proxyFactoryBean() {
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.setTargetName("myService");
        proxyFactoryBean.setInterceptorNames("beforeAdvice", "methodInterceptor", "methodInterceptor2");
        return proxyFactoryBean;
    }
}

四、定义主类,获取ProxyFactoryBean并使用

package cn.edu.tju.study.service;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AopAnnotationTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(MyConfig.class);
        context.refresh();

        MyService myService = context.getBean("proxyFactoryBean", MyService.class);
        myService.myMethod();


    }
}

五、运行结果
spring复习:(39)注解方式的ProxyFactoryBean,Spring,spring,java,后端文章来源地址https://www.toymoban.com/news/detail-598257.html

到了这里,关于spring复习:(39)注解方式的ProxyFactoryBean的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Spring】基于注解方式存取JavaBean:Spring有几种注入方式?有什么区别?

     Hello,我是小黄。众所周知,Spring是一个开源的Java应用程序框架,其中包括许多通过注解实现依赖注入的功能。Spring提供了多种注入方式,可以满足不同的需求和场景。常见的注入方式包括构造函数注入、Setter方法注入和属性注入。不同的注入方式有不同的适用场景和优缺

    2024年02月11日
    浏览(33)
  • spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?

    一、主类: 二、配置类: 三、singleton bean的创建流程 运行到context.refresh(); 进入refresh方法: 向下运行到红线位置时: 会实例化所有的singleton bean.进入finisheBeanFactoryInitialization方法: 向下拖动代码,可以看到beanFactory.preInstantiateSingletons(); 进入preInstantiateSingletons方法: 可以看

    2024年02月16日
    浏览(28)
  • spring复习:(55)注解配置的情况下@ComponentScan指定的包中的组件是怎么被注册到容器的?

    配置类: 主类: 结论:是在context.refresh()处完成扫描和注册的。 fresh()的代码片段如下: 其中调用的invokeBeanFactoryPostProcessor代码如下: 其中调用的静态方法invokeBeanFactoryPostProcessors代码如下: 其中包含如下代码片段: invokeBeanDefinitionRegisteyPostProcessor的代码如下: 其中调用的

    2024年02月15日
    浏览(32)
  • Spring+SprinMVC+MyBatis注解方式简易模板

    Spring+SprinMVC+MyBatis注解方式简易模板代码Demo GitHub访问 ssm-tpl-anno 创建数据库test,执行下方SQL创建表ssm-tpl-cfg 一个简单的基于注解的增删改查就实现了

    2024年01月23日
    浏览(30)
  • 【Spring】使用自定义注解方式实现AOP鉴权

    AOP,是一种面向切面编程,可以通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。 在软件开发中,鉴权(Authentication)是一项非常重要的安全措施,用于验证用户身份和权限。在应用程序中,我们通常会使用AOP(Aspect-Oriented Programming)来实现鉴权功能

    2024年02月11日
    浏览(39)
  • javaee spring 用注解的方式实现ioc

    spring核心依赖 spring配置文件

    2024年02月10日
    浏览(35)
  • Spring Boot中整合MyBatis(基于xml方式&基于注解实现方式)

    在Spring Boot中整合MyBatis时,你需要导入JDBC(不需要手动添加)、Druid的相关依赖、MySQL相关依赖。 JDBC依赖:在Spring Boot中整合MyBatis时,并不需要显式地添加JDBC的包依赖。这是因为,当你添加 mybatis-spring-boot-starter 依赖时,它已经包含了对JDBC的依赖。 mybatis-spring-boot-starter 是

    2024年02月15日
    浏览(43)
  • 【spring源码系列-04】注解方式启动spring时refresh的前置工作

    Spring源码系列整体栏目 内容 链接地址 【一】spring源码整体概述 https://blog.csdn.net/zhenghuishengq/article/details/130940885 【二】通过refresh方法剖析IOC的整体流程 https://blog.csdn.net/zhenghuishengq/article/details/131003428 【三】xml配置文件启动spring时refresh的前置工作 https://blog.csdn.net/zhenghuishen

    2024年02月08日
    浏览(32)
  • Spring Boot中最常用注解的使用方式(上篇)

    摘要:本文将详细介绍Spring Boot中最常用的注解的使用方式,并通过代码示例加以说明。通过学习这些注解,读者将能够更好地理解和运用Spring Boot框架,构建高效的企业级应用。 1.@RequestMapping @RequestMapping :将一个HTTP请求映射到对应的控制器方法上。可以用于类和方法级别。

    2024年02月07日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包