Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is my test case, that appears to work (Spring 2.5.6). I thought about excluding some files for brevity, but I decided against it.</p> <p>Start.java (entry-point)</p> <pre><code>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"); } } </code></pre> <p>Context.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;context:component-scan base-package="se.waxwing.test" use-default-filters="false" annotation-config="false" scope-resolver="se.waxwing.test.MyScopeResolver"&gt; &lt;context:include-filter type="custom" expression="se.waxwing.test.MyTypeFilter" /&gt; &lt;/context:component-scan&gt; &lt;bean id="beanProcessor" class="se.waxwing.test.MyBeanPostProcessor" /&gt; &lt;/beans&gt; </code></pre> <p>CustomBean.java (this is the I want to find - see MyTypeFilter)</p> <pre><code>package se.waxwing.test; public class CustomBean { public CustomBean() { System.err.println("instantiating component"); } } </code></pre> <p>MyBeanPostProcessor.java</p> <pre><code>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; } } </code></pre> <p>MyScopeResolver.java</p> <pre><code>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; } } </code></pre> <p>MyTypeFilter.java</p> <pre><code>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()); } } </code></pre> <p>This produces the following output:</p> <pre> 2009-aug-26 15:44:02 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: 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 hierarchy 2009-aug-26 15:44:02 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [se/waxwing/test/Context.xml] 2009-aug-26 15:44:02 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@121f1d 2009-aug-26 15:44:02 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@121f1d: defining beans [customBean,beanProcessor]; root of factory hierarchy instantiating component before customBean after customBean </pre> <p>So, as you can see, the <code>customBean</code> bean was found by my type filter, added as a bean, and when <code>applicationContext.getBean("customBean")</code> was called a new object was instantiated and then it was passed to my post bean processor.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload