ignoreDependencyInterface()与ignoreDependencyType()

Posted vvii

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ignoreDependencyInterface()与ignoreDependencyType()相关的知识,希望对你有一定的参考价值。

官方源码介绍:

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
/**
 * Ignore the given dependency type for autowiring:
 * for example, String. Default is none.
 */
public void ignoreDependencyType(Class<?> type) {
   this.ignoredDependencyTypes.add(type);
}

/**
 * Ignore the given dependency interface for autowiring.
 * <p>This will typically be used by application contexts to register
 * dependencies that are resolved in other ways, like BeanFactory through
 * BeanFactoryAware or ApplicationContext through ApplicationContextAware.
 * <p>By default, only the BeanFactoryAware interface is ignored.
 * For further types to ignore, invoke this method for each type.
 * @see org.springframework.beans.factory.BeanFactoryAware
 * @see org.springframework.context.ApplicationContextAware
 */
public void ignoreDependencyInterface(Class<?> ifc) {
   this.ignoredDependencyInterfaces.add(ifc);
}

(一)IgnorDenpendencyInterface()的用法
spring-config.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-autowire="byType">

    <bean id="list" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <value>hello</value>
                <value>Spring</value>
            </list>
        </constructor-arg>
    </bean>

    <bean id = "ignorDependency" class="com.vi.springbean.IgnoreDependencyByInterfaceImpl"></bean>
    <bean class="com.vi.springbean.IgnoreDependencyHelper"/>
</beans>

 接口:

public interface IgnoreDependencyByInterfaceSrv {
    void setList(List<String> list);
}

接口实现

public class IgnoreDependencyByInterfaceImpl implements IgnoreDependencyByInterfaceSrv {
    private  List<String> list;

    @Override
    public void setList(List<String> list) {
        this.list = list;
    }

    public List<String> getList() {
        return list;
    }
}


辅助类 IgnorDenpendencyHelper

package com.vi.springbean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class IgnoreDependencyHelper implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.ignoreDependencyInterface(IgnoreDependencyByInterfaceSrv.class);
    }
}

测试类:

 1 import com.vi.springbean.IgnoreDependencyByInterfaceImpl;
 2 import com.vi.springbean.IgnoreDependencyHelper;
 3 import org.springframework.beans.factory.BeanFactory;
 4 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 5 import org.springframework.beans.factory.xml.XmlBeanFactory;
 6 import org.springframework.core.io.ClassPathResource;
 7 
 8 public class Test {
 9     @org.junit.Test
10     public void test() {
11         String path = "spring-config.xml";
12         BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(path));
13 
14         IgnoreDependencyHelper ignoreDependencyHelper = beanFactory.getBean(IgnoreDependencyHelper.class);
15         ignoreDependencyHelper.postProcessBeanFactory((ConfigurableListableBeanFactory)beanFactory);
16 
17         IgnoreDependencyByInterfaceImpl bean = beanFactory.getBean(IgnoreDependencyByInterfaceImpl.class);
18         System.out.println(bean.getList());
19 
2     21 
22     }
23 }

注:

     当前测试结果为null

     当第15行代码注掉,打印结果为[hello, Spring]

 
(二)ignoreDependencyType()用法

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-autowire="byType">

    <bean id="list" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <value>hello</value>
                <value>Spring</value>
            </list>
        </constructor-arg>
    </bean>

    <bean id = "ignorDependency" class="com.vi.springbean.IgnoreDependencyByType"></bean>
    <bean class="com.vi.springbean.IgnoreDependencyHelper"/>

</beans>

主类:

package com.vi.springbean;

import java.util.List;

public class IgnoreDependencyByType {
    private List<String> list;

    public List<String> getList() {
        return list;
    }

    public IgnoreDependencyByType setList(List<String> list) {
        this.list = list;
        return this;
    }
}

辅助类:

public class IgnoreDependencyHelper implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.ignoreDependencyType(List.class);
    }
}

Test类:

 1 public class Test {
 2     @org.junit.Test
 3     public void test() {
 4         String path = "spring-config.xml";
 5         BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(path));
 6 
 7         IgnoreDependencyHelper ignoreDependencyHelper = beanFactory.getBean(IgnoreDependencyHelper.class);
 8         ignoreDependencyHelper.postProcessBeanFactory((ConfigurableListableBeanFactory)beanFactory);
 9 
10         IgnoreDependencyByType bean = beanFactory.getBean(IgnoreDependencyByType.class);
11         System.out.println(bean.getList());
12 
13     }
14 }

注:

当前运行结果为null

当第8行代码注释掉,运行结果为[hello, Spring]

 

以上是关于ignoreDependencyInterface()与ignoreDependencyType()的主要内容,如果未能解决你的问题,请参考以下文章