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 6577467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:38:21+00:00 2026-05-25T15:38:21+00:00

Given a set of classes wired together by spring. There are several classes that

  • 0

Given a set of classes wired together by spring. There are several classes that are used with different configuration in multiple instances in the environment. They have different beanid of course.

The problems:

  • When they make log entries, we dont know exactly which bean made the log, since the log4j displays the classname only
  • I know that I could use logger instantiated by spring InitializationBean+BeanNameAware interface methods, but I do not want to do it, since I do not want to implement them in all classes

The solution could be:

  • Having some effect on bean factory to store the id of the beans in a map with the bean reference (key is the ref, name is the value)
  • Creating an aspect to be applied on every method, that would set an “BeanName” MDC entry in Log4j before the call, and would restore it to the previous value after the call. Meanwhile the previous beannames could be stored in a threadlocal in a stack.

The questions:

  • How can I change/configure the bean factory to do this trick for me? Is there any customization point I could use to this aim?
  • How can I avoid memory leaks in the map in the beanid registry? Maybe the registry is not needed at all, if somehow spring can look up the id for a reference.
  • Do you have any better idea, that would not result in changing ten thousand classes?

Thanks in advance.

UPDATE:
– Does anyone have solution for the prototype beans?

  • 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-25T15:38:22+00:00Added an answer on May 25, 2026 at 3:38 pm

    I have managed to hack something together based on this Spring AOP Example.

    I am not yet up to speed with Spring 3 so I have implemented this using Spring 2.5 – I dare say there are more elegant ways of achieving what you want. I have implemented this using System.out‘s for simplicity but these could easily be converted to log4j calls.

    Initially I create a map between the Spring’s bean names and the string representation of the object (InitBean). This map is used inside the MethodInterceptor – I did try making the MethodInterceptor an InitializingBean but the MethodInterceptor stopped working for some reason.

    Performing an equals between the bean passed in via the MethodInterceptor and the other beans in the application context did not work. e.g. by using something like “ctx.getBeansOfType(GoBean.class)” inside the MethodInterceptor. I presume this is because the object passed in via the MethodInvocation was a GoBean whereas objects obtained from the application context at this point are proxied (e.g. something like example.GoBean$$EnhancerByCGLIB$$bd27d40e).

    This is why I had to resort to a comparison of object string representations (which is not ideal). Also I specifically do not want to activate the MethodInterceptor logic when calling the “toString” method on an object (as since I’m using toString elsewhere leads to infinite loops and StackOverflow).

    I hope this is useful,

    applicationContext.xml

    <beans>
    
        <bean name="initBean" class="example.InitBean"/>
    
        <bean name="methodLoggingInterceptor" class="example.MethodLoggingInterceptor">
            <property name="initBean" ref="initBean"/>
        </bean>
    
        <bean name="proxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="beanNames">
                <list>
                    <value>go*</value>
                </list>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>methodLoggingInterceptor</value>
                </list>
            </property>
        </bean>
    
        <bean name="goBean1" class="example.GoBean" />
        <bean name="goBean2" class="example.GoBean" />   
        <bean name="goBean3" class="example.GoBean" />  
    
    </beans>
    

    GoBean.java

    public class GoBean {
        public void execute(){
            System.out.println(new Date());
        }        
    }    
    

    SimpleTestClass.java

    public static void main( String[] args ){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    
        ArrayList<GoBean> goBeans = new ArrayList<GoBean>();
        goBeans.add((GoBean) ctx.getBean("goBean1"));
        goBeans.add((GoBean) ctx.getBean("goBean2"));
        goBeans.add((GoBean) ctx.getBean("goBean3"));
    
        for(GoBean g: goBeans){
            g.execute();
        }
    }
    

    InitBean.java

    public class InitBean implements ApplicationContextAware, InitializingBean {
        private ApplicationContext ctx;
        private Map<String, String> beanMap = new HashMap<String,String>();
    
        public void setApplicationContext(ApplicationContext ac) throws BeansException {
            ctx = ac;
        }
    
        public void afterPropertiesSet() throws Exception {
            for(String beanName: ctx.getBeanNamesForType(GoBean.class)){
                beanMap.put(ctx.getBean(beanName).toString(), beanName);
            }
        }
    
        public Map<String,String> getBeanMap(){
            return beanMap;
        }    
    }
    

    MethodLoggingInterceptor.java

    public class MethodLoggingInterceptor implements MethodInterceptor{
    
        private InitBean initBean;
    
        public Object invoke(MethodInvocation method) throws Throwable {
            if (!"toString".equals(method.getMethod().getName())) {
                StringBuilder sb = new StringBuilder();
                Object obj = method.getThis();
                if (obj instanceof GoBean) {
                    Map<String,String> beanMap = initBean.getBeanMap();
                    String objToString = obj.toString();
                    if (beanMap.containsKey(objToString)) {
                        System.out.println(beanMap.get(objToString));
                        sb.append("bean: ");
                        sb.append(beanMap.get(objToString));
                        sb.append(" : ");
                    }
                }
                sb.append(method.getMethod().getDeclaringClass());
                sb.append('.');
                sb.append(method.getMethod().getName());
                System.out.println(sb.toString() + " starts");
                Object result = method.proceed();
                System.out.println(sb.toString() + " finished");
                return result;
            } else {
                return method.proceed();
            }
    
        }
    
        public void setInitBean(InitBean ib) {
            this.initBean = ib;
        }        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I notice that in the generated SWIG wrappers for a given set of classes,
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
Is there an application , which can parse a given set of stored procedures
Given a set of several million points with x,y coordinates, what is the algorithm
First the practical application that led me to the problem: Given a set of
Given the classes: public class Person { public string Name { get; set; }
Further to my other question given the following classes and fluent map, is there
I have a set of classes that all need to be acted on in
Given is a set S of size n, which is partitioned into classes (s1,..,sk)
Given the following, I would not expect the compiler to allow multiple attributes that

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.