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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:34:33+00:00 2026-06-12T12:34:33+00:00

I have an Interface with Component annotation and some classes that implemented it as

  • 0

I have an Interface with Component annotation and some classes that implemented it as follows:

@Component
public interface A {
}

public class B implements A {
}
public class C implements A {
}

Also, I have a class with an Autowired variable like this:

public class Collector {
    @Autowired
    private Collection<A> objects;

    public Collection<A> getObjects() {
        return objects;
    }
}

My context file consists of these definitions:

<context:component-scan base-package="org.iust.ce.me"></context:component-scan>

<bean id="objectCollector" class="org.iust.ce.me.Collector" autowire="byType"/>

<bean id="b" class="org.iust.ce.me.B"></bean>
<bean id="c" class="org.iust.ce.me.C"></bean>

And in the main class, I have some codes as follows:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
B b = (B) context.getBean("b");
C c = (C) context.getBean("c");
Collector objectCollector = (Collector) context.getBean("objectCollector");

for (A object : objectCollector.getObjects()) {
    System.out.println(object);
}

Output:

org.iust.ce.me.B@1142196
org.iust.ce.me.C@a9255c

These codes work well, but for some reasons I’m not willing to use xml context file. Besides it, I prefer to create the objects with the new operator rather than using the getBean() method. Nevertheless, since the AutoWiring is really good idea in programming, I don’t want to lose it.

Now I have two questions!!

  1. how can I AutoWire classes that implements the A Interface without using the xml context file?
    Is it possible at all?

  2. when I change the scope of a bean from singlton to
    prototype as follows:

    <bean id="b" class="org.iust.ce.me.B" scope="prototype"></bean>

    and instantiate several beans of it, only the bean which was instantiated during creating context, is injected into AutoWired variable. Why?

Any help will be appreciated.

  • 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-12T12:34:34+00:00Added an answer on June 12, 2026 at 12:34 pm

    Not sure the version of Spring you are using. But currently you can use @Configuration to replace .xml. Take a look at @Configuration

    Below is the code in documentation

    @Configuration
    public class ServiceConfig {
        private @Autowired RepositoryConfig repositoryConfig;
        public @Bean TransferService transferService() {
            return new TransferServiceImpl(repositoryConfig.accountRepository());
        }
    }
    
    @Configuration
    public interface RepositoryConfig {
        @Bean AccountRepository accountRepository();
    }
    
    @Configuration
    public class DefaultRepositoryConfig implements RepositoryConfig {
        public @Bean AccountRepository accountRepository() {
            return new JdbcAccountRepository(...);
        }
    }
    
    @Configuration
    @Import({ServiceConfig.class, DefaultRepositoryConfig.class}) // import the concrete config!
    public class SystemTestConfig {
        public @Bean DataSource dataSource() { /* return DataSource */ }
    }
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class);
        TransferService transferService = ctx.getBean(TransferService.class);
        transferService.transfer(100.00, "A123", "C456");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an interface and some classes: public interface IPanel<ComponentType extends Component> {
If I have interface and implementing classes like public interface IA {} public class
I have an interface and two classes that implements it. One of them has
I have a base class called Component. I also have 2 interfaces I2DComponent, and
I have interface Foo public interface Foo { public void test(); } Class FooChild
I have an Strategy interface, that is implemented by StrategyA and StrategyB , both
My code: @Component public class A { @Autowired private B b; public void method()
I have interface IConfigurationSource { .... } and class FileConfigurationSource : IConfigurationSource { FileConfigurationSource(string
Let's say we have interface window_creator that responsible for creation of windows. For simplicity
I have an interface: public interface: IA { ... } and I try to

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.