国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

spring復(fù)習:(39)注解方式的ProxyFactoryBean

這篇具有很好參考價值的文章主要介紹了spring復(fù)習:(39)注解方式的ProxyFactoryBean。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、定義接口

package cn.edu.tju.study.service;

public interface MyService {
    void myMethod();
}

二、定義實現(xiàn)類:

package cn.edu.tju.study.service;

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

三、定義配置類,配置業(yè)務(wù)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();


    }
}

五、運行結(jié)果
spring復(fù)習:(39)注解方式的ProxyFactoryBean,Spring,spring,java,后端文章來源地址http://www.zghlxwxcb.cn/news/detail-598257.html

到了這里,關(guān)于spring復(fù)習:(39)注解方式的ProxyFactoryBean的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【Spring】基于注解方式存取JavaBean:Spring有幾種注入方式?有什么區(qū)別?

    【Spring】基于注解方式存取JavaBean:Spring有幾種注入方式?有什么區(qū)別?

    ?Hello,我是小黃。眾所周知,Spring是一個開源的Java應(yīng)用程序框架,其中包括許多通過注解實現(xiàn)依賴注入的功能。Spring提供了多種注入方式,可以滿足不同的需求和場景。常見的注入方式包括構(gòu)造函數(shù)注入、Setter方法注入和屬性注入。不同的注入方式有不同的適用場景和優(yōu)缺

    2024年02月11日
    瀏覽(19)
  • spring復(fù)習:(50)@Configuration注解配置的singleton的bean是什么時候被創(chuàng)建出來并緩存到容器的?

    spring復(fù)習:(50)@Configuration注解配置的singleton的bean是什么時候被創(chuàng)建出來并緩存到容器的?

    一、主類: 二、配置類: 三、singleton bean的創(chuàng)建流程 運行到context.refresh(); 進入refresh方法: 向下運行到紅線位置時: 會實例化所有的singleton bean.進入finisheBeanFactoryInitialization方法: 向下拖動代碼,可以看到beanFactory.preInstantiateSingletons(); 進入preInstantiateSingletons方法: 可以看

    2024年02月16日
    瀏覽(23)
  • spring復(fù)習:(55)注解配置的情況下@ComponentScan指定的包中的組件是怎么被注冊到容器的?

    spring復(fù)習:(55)注解配置的情況下@ComponentScan指定的包中的組件是怎么被注冊到容器的?

    配置類: 主類: 結(jié)論:是在context.refresh()處完成掃描和注冊的。 fresh()的代碼片段如下: 其中調(diào)用的invokeBeanFactoryPostProcessor代碼如下: 其中調(diào)用的靜態(tài)方法invokeBeanFactoryPostProcessors代碼如下: 其中包含如下代碼片段: invokeBeanDefinitionRegisteyPostProcessor的代碼如下: 其中調(diào)用的

    2024年02月15日
    瀏覽(18)
  • Spring+SprinMVC+MyBatis注解方式簡易模板

    Spring+SprinMVC+MyBatis注解方式簡易模板

    Spring+SprinMVC+MyBatis注解方式簡易模板代碼Demo GitHub訪問 ssm-tpl-anno 創(chuàng)建數(shù)據(jù)庫test,執(zhí)行下方SQL創(chuàng)建表ssm-tpl-cfg 一個簡單的基于注解的增刪改查就實現(xiàn)了

    2024年01月23日
    瀏覽(27)
  • javaee spring 用注解的方式實現(xiàn)ioc

    spring核心依賴 spring配置文件

    2024年02月10日
    瀏覽(26)
  • 【Spring】使用自定義注解方式實現(xiàn)AOP鑒權(quán)

    AOP,是一種面向切面編程,可以通過預(yù)編譯方式和運行期間動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術(shù)。 在軟件開發(fā)中,鑒權(quán)(Authentication)是一項非常重要的安全措施,用于驗證用戶身份和權(quán)限。在應(yīng)用程序中,我們通常會使用AOP(Aspect-Oriented Programming)來實現(xiàn)鑒權(quán)功能

    2024年02月11日
    瀏覽(20)
  • Spring Boot中整合MyBatis(基于xml方式&基于注解實現(xiàn)方式)

    Spring Boot中整合MyBatis(基于xml方式&基于注解實現(xiàn)方式)

    在Spring Boot中整合MyBatis時,你需要導入JDBC(不需要手動添加)、Druid的相關(guān)依賴、MySQL相關(guān)依賴。 JDBC依賴:在Spring Boot中整合MyBatis時,并不需要顯式地添加JDBC的包依賴。這是因為,當你添加 mybatis-spring-boot-starter 依賴時,它已經(jīng)包含了對JDBC的依賴。 mybatis-spring-boot-starter 是

    2024年02月15日
    瀏覽(38)
  • 【spring源碼系列-04】注解方式啟動spring時refresh的前置工作

    【spring源碼系列-04】注解方式啟動spring時refresh的前置工作

    Spring源碼系列整體欄目 內(nèi)容 鏈接地址 【一】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日
    瀏覽(19)
  • Spring Boot中最常用注解的使用方式(下篇)

    Spring Boot中最常用注解的使用方式(下篇)

    摘要:本文是《深入解析Spring Boot中最常用注解的使用方式》的下篇內(nèi)容,將繼續(xù)介紹Spring Boot中其他常用的注解的使用方式,并通過代碼示例進行說明,幫助讀者更好地理解和運用Spring Boot框架。 1.@Autowired @Autowired :自動裝配依賴對象。示例代碼如下: 2. @Configuration @Config

    2024年02月07日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包