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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:53:25+00:00 2026-05-27T11:53:25+00:00

Let’s assume I have an annotated bean property setter like this: public class Foo

  • 0

Let’s assume I have an annotated bean property setter like this:

public class Foo {
  ...
  @Autowired 
  public void setBar(Bar bar) {
    ...
}

The Springframework will lookup the matching Bar property as usual. However, I’d like to intercept the default bean resolving process and add a little bit of “magic” myself. I’d like to introduce a resolver like this:

public interface SomeResolverInterface<T> {
  public T resolve(Class<T> beanClass);
}

public class BarResolver implements SomeResolverInterface<Bar> {

  @Override
  public Bar resolve(Class<Bar> beanClass) {
    if(someCondition) {
      return someBean;
    } else {
      return anotherBean;
    }
  }

  ...

I know I could alway introduce some kind of wrapper bean and move the resolving logic into this but I’d prefer a more generic way using a resolver like described above to make Foo completely independent of the resolving logic.

Is there a way within the Springframework to achieve something like this?

  • 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-27T11:53:25+00:00Added an answer on May 27, 2026 at 11:53 am

    From your description it looks like you only need to perform additional logic at startup time (autowiring). There are several ways to tackle this problem (from worst to best):

    • AOP – bad idea, introduces runtime overhead by intercepting every call

    • custom scope – see: Custom spring scopes? Also works at runtime and also a bad idea

    • @Profile – define two matching beans and enable only one based on active profile. Pretty clean and introduces overhead only at startup time

    • @Configuration – defining beans in Java has an extra benefit of having full control over how are they created:


    @Configuration
    public class Config {
    
        @Autowired
        private Bar someBean;
    
        @Autowired
        private Bar anotherBean;
    
        @Bean
        @Primary
        public Bar primaryBean() {
            if(someCondition) {
              return someBean;
            } else {
              return anotherBean;
            }
          }
    
    }
    

    As you can see in this case we have three beans of Bar type: someBean, anotherBean and primaryBean. The first two can also be configured using @Bean or via component scan with @Service. But to make autowiring possible the last primaryBean is marked as @Primary. This way it will be preferred over other two.

    This is my recommended solution as the resolving logic is clean, maintainable and readable. IMHO this is the situation where Java-based @Configuration really shines.

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

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I have table with column 'URL' whrere I store urls like this
Let's imagine I have a Java class of the type: public class MyClass {
Let's say that I have a set of relations that looks like this: relations
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I can call a method like this: core::get() . What is the

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.