Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 348053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:19:44+00:00 2026-05-12T11:19:44+00:00

I have many cookie-cutter spring beans and don’t want to explicitly define each one

  • 0

I have many cookie-cutter spring beans and don’t want to explicitly define each one in xml. So I went the component scanning route which lets me do this. This is nice, but I just realized that MyBeanPostProcessor isn’t being called for the beans loaded in using the component-scan technique. MyBeanPostProcessor simply attempts to do some setter injection on these beans. The below configuration just shows this approach I tried which doesn’t work. Any other ideas how to do setter injection on these beans?

I’m using Spring 2.5.5

Thanks,
Ben

<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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:19:44+00:00Added an answer on May 12, 2026 at 11:19 am

    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.

    Start.java (entry-point)

    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");
        }
    }
    

    Context.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 (this is the I want to find – see 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());
        }
    }
    

    This produces the following output:

    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
    

    So, as you can see, the customBean bean was found by my type filter, added as a bean, and when applicationContext.getBean("customBean") was called a new object was instantiated and then it was passed to my post bean processor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have many settings that I want to store in a cookie, should
I have many items inside a list control. I want each item to have
I want to share session cookie among domains. I have more than one domain:
I have a site (like many). I want to show but disable a particular
I have many years of experience in Java including Swing, Servlet and JDBC, but
I have many emails coming in from different sources. they all have attachments, many
I have many small files containing code fragments, pseudo-code algorithms, classes, templates, SQL-samples, etc.,
I have many jobs that have a step to send 1 specific email out
We have many projects that use a common base of shared components (dlls). Currently
I have many different branches/checkouts of the same project code on my development machine.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.