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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:33:36+00:00 2026-06-15T06:33:36+00:00

If I declare a class using @Bean and then component scan for the class,

  • 0

If I declare a class using @Bean and then component scan for the class, spring will instantiate the class by invoking it’s constructor and injecting constructor args and injecting any fields marked with @Inject. For simplicity’s sake, lets call this spring auto-building.

I dislike component scan and wish to avoid it completely (I don’t wish to discuss my reasons for not liking it). I would like to use a @Configuration object instead but would still like to have the auto-building functionality available to me. Is it possible to invoke spring to auto-build my objects instead of explicitly having to pass all the constructor arguments in my @Configuration object?

Lets assume that I have a bean:

public class MyServiceImpl implements MyService {
    public MyServiceImpl(Dependency1 d1, Dependency d2) { ... }
    ....
}

I could define a configuration object like this:

@Configuration
public class MyConfiguration {
    // lets assume d1 and d2 are defined in another @Configuration
    @Inject
    Dependency1 d1; 

    @Inject
    Dependency2 d2;

    @Bean
    public MyService myService() { 
        // I dislike how I have to explicitly call the constructor here
        return new MyServiceImpl(d1, d2);
    }
}

But now, I have explicitly had to call the MyServiceImpl constructor myself so I will have to keep updating this as my constructor changes over time.

I was hoping that I could declare an abstract method so that spring auto-building could take place:

@Configuration
public abstract class MyConfiguration {
    @Bean
    public abstract MyServiceImpl myService();
}

But this doesn’t work. Is there a way that I can invoke spring auto-building without using a component scan?

In Google Guice, this can be done via the Binder:
https://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/Binder.html

In Tapestry IOC, this can be done via the ServiceBinder:
http://tapestry.apache.org/ioc-cookbook-basic-services-and-injection.html#IoCCookbook-BasicServicesandInjection-SimpleServices

Update

Based on spod’s answer, I was able to achieve what I was after (thanks!). Test case included for anyone that wants to do the same:

import java.util.Date;
import javax.inject.Inject;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class AutoBuildConfigurationTest {
    @Configuration
    public static class MyConfiguration {
        @Inject
        private AutowireCapableBeanFactory beanFactory;

        @Bean
        public Date date() {
            return new Date(12345);
        }

        @Bean
        public MyService myService() {
            return autoBuild(MyService.class);
        }

        protected <T> T autoBuild(Class<T> type) {
            return type.cast(beanFactory.createBean(type, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true));
        }
    }

    public static class MyService {
        private Date date;

        public MyService(Date date) {
            this.date = date;
        }

        public Date getDate() {
            return date;
        }
    }

    @Test
    public void testAutoBuild() {
        ApplicationContext appContext = new AnnotationConfigApplicationContext(MyConfiguration.class);
        MyService myService = appContext.getBean(MyService.class);
        Assert.assertEquals(12345, myService.getDate().getTime());
    }
}
  • 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-06-15T06:33:37+00:00Added an answer on June 15, 2026 at 6:33 am

    The java based container configuration doesnt depend on doing a component scan in any way. Its merely a different approach for the XML based component configuration. With the XML configuration you’d just have to declare your bean with the MyServiceImpl class in case its already @inject annotated. Spring would recognize the annotations and take care of them. If you really want to instanciate MyServiceImpl from a @Configuration java class without calling the constructor yourself, then you’d have to make use of the bean factory (havent tested it, just give it a try):

    @Configuration
    public class MyConfiguration {
    
        @Autowired AutowireCapableBeanFactory beanFactory;
    
        @Bean public MyService myService() { 
            return beanFactory.createBean(MyServiceImpl.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to declare two beans and instantiate them using Spring dependency injection? <bean
If I want to declare a bean using Spring 3's Java-based configuration, I can
If I declare lazy attribute in perl class using Moose, and the attribute is
How i can declare a global method in delphi prism using the __Global class?
If I declare class as abstract with no abstract methods declared in it, will
what is the best approach for using velocity component inside spring container. In my
In my main() method I create a PersonCollection Object using Spring and then I
I am using ThreadPoolExecutor utility and passing the values thru constructor in called class.
I'm using Spring's ObjectFactoryCreatingFactoryBean to retrieve a prototype scoped bean, as described in the
I am using refection to get the declared fields of a class. Now I

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.