gpt4 book ai didi

java - spring beanpostprocessor 不适用于组件扫描?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:44 29 4
gpt4 key购买 nike

我有很多千篇一律的 spring bean,不想在 xml 中显式定义每一个。所以我去了让我这样做的组件扫描路线。这很好,但我刚刚意识到 MyBeanPostProcessor 没有被调用用于使用组件扫描技术加载的 bean。 MyBeanPostProcessor 只是尝试对这些 bean 进行一些 setter 注入(inject)。下面的配置仅显示了我尝试过的这种方法,但它不起作用。还有其他关于如何对这些 bean 进行 setter 注入(inject)的想法吗?

我正在使用 Spring 2.5.5

谢谢,本

<context:component-scan base-package="us.benanderson" 
use-default-filters="false"
annotation-config="false"
scope-resolver="us.benanderson.MyScopeResolver"
name-generator="us.benanderson.MyBeanNameGenerator">
<context:include-filter type="custom" expression="us.benanderson.MyTypeFilter" />
</context:component-scan>
<bean class="us.benanderson.MyBeanPostProcessor">
<property name="order" value="500" />
</bean>

最佳答案

这是我的测试用例,似乎有效(Spring 2.5.6)。为了简洁起见,我考虑过排除一些文件,但我决定反对。

Start.java(入口点)

package se.waxwing.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Start {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("se/waxwing/test/Context.xml");
context.getBean("customBean");
}
}

上下文.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<context:component-scan base-package="se.waxwing.test"
use-default-filters="false"
annotation-config="false"
scope-resolver="se.waxwing.test.MyScopeResolver">
<context:include-filter type="custom" expression="se.waxwing.test.MyTypeFilter" />
</context:component-scan>

<bean id="beanProcessor" class="se.waxwing.test.MyBeanPostProcessor" />

</beans>

CustomBean.java(这是我想要找到的 - 请参阅 MyTypeFilter)

package se.waxwing.test;

public class CustomBean {

public CustomBean() {
System.err.println("instantiating component");
}
}

MyBeanPostProcessor.java

package se.waxwing.test;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

public MyBeanPostProcessor() {
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.err.println("after " + beanName);
return bean;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.err.println("before " + beanName);
return bean;
}
}

MyScopeResolver.java

package se.waxwing.test;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ScopeMetadata;
import org.springframework.context.annotation.ScopeMetadataResolver;
import org.springframework.context.annotation.ScopedProxyMode;

public class MyScopeResolver implements ScopeMetadataResolver {

@Override
public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
ScopeMetadata result = new ScopeMetadata();
result.setScopedProxyMode(ScopedProxyMode.NO);
result.setScopeName("prototype");
return result;
}

}

MyTypeFilter.java

package se.waxwing.test;

import java.io.IOException;

import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

public class MyTypeFilter implements TypeFilter {

@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
return metadataReader.getClassMetadata().getClassName().equals(CustomBean.class.getCanonicalName());
}
}

这会产生以下输出:

2009-aug-26 15:44:02 org.springframework.context.support.AbstractApplicationContext prepareRefreshINFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Wed Aug 26 15:44:02 CEST 2009]; root of context hierarchy2009-aug-26 15:44:02 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [se/waxwing/test/Context.xml]2009-aug-26 15:44:02 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactoryINFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@121f1d2009-aug-26 15:44:02 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletonsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@121f1d: defining beans [customBean,beanProcessor]; root of factory hierarchyinstantiating componentbefore customBeanafter customBean

因此,如您所见,customBean bean 被我的类型过滤器找到,作为 bean 添加,并且当 applicationContext.getBean("customBean") 被调用一个新对象被实例化,然后它被传递到我的 post bean 处理器。

关于java - spring beanpostprocessor 不适用于组件扫描?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1121575/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com