@Import導入ImportSelector實現類demo


1、測試類接口

package com.fh.imports.dao;

public interface TestDao {

    void test();
}

測試類實現

package com.fh.imports.dao;

import org.springframework.stereotype.Component;

@Component(value = "testDao")
public class TestDaoImpl implements TestDao {
    @Override
    public void test() {
        System.out.println("TestDaoImpl");
    }
}

2、添加一個ImportSelector的實現類

package com.fh.imports;

import com.fh.imports.dao.TestDaoImpl3;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 可以做開關
 */
public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        //返回需要實例化的對象的名稱
        return new String[]{TestDaoImpl3.class.getName()};
    }
}

3、添加上面的TestDaoImpl3這個類

package com.fh.imports.dao;

import com.fh.imports.ProxyInvocationHandler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Proxy;

public class TestDaoImpl3 implements  BeanPostProcessor {


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if("testDao".equals(beanName)){
            bean =  Proxy.newProxyInstance(this.getClass().getClassLoader(),new Class[]{TestDao.class},new ProxyInvocationHandler(bean));
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }
}

4、添加對應的InvocationHandler類

package com.fh.imports;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class ProxyInvocationHandler implements InvocationHandler {

    Object target;

    public ProxyInvocationHandler(Object object){
        this.target = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理類執行!!!");
        return method.invoke(target,args);
    }
}

5、添加注解類導入

package com.fh.imports;

import org.springframework.context.annotation.Import;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(MyImportSelector.class)
public @interface EnableProxy {
}

6、添加一個配置類,進行注解配置即可

package com.fh.imports;

import org.springframework.context.annotation.Configuration;


@Configuration
@EnableProxy
public class ImportConfig {

//    @Bean
//    public TestDao getTest(){
//        return new TestDaoImpl();
//    }
}

7、最后添加測試類,從容器中獲取相關的實例,查看是否被代理

TestDao testDao = (TestDao) context.getBean("testDao");
testDao.test();

最后,項目的目錄結構:

 

 完


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM