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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:01:12+00:00 2026-06-15T18:01:12+00:00

Using Guice, one can do the following: interface Leg {} _ class LeftLeg implements

  • 0

Using Guice, one can do the following:

interface Leg {}

_

class LeftLeg implements Leg {
    public String toString() {
        return "LeftLeg";
    }
}

_

class RightLeg implements Leg {
    public String toString() {
        return "RightLeg";
    }
}

_

class Robot {
    final Leg leftLeg_;
    final Leg rightLeg_;

    @Inject
    Robot(@Named("left") Leg leftLeg, @Named("right") Leg rightLeg) {
        leftLeg_ = leftLeg;
        rightLeg_ = rightLeg;
    }

    public String toString() {
        return "leftLeg_=" + leftLeg_ + ", rightLeg_=" + rightLeg_;
    }
}

_

class RobotTest {
    @Test
    public void t1() throws Exception {
        Injector inj = Guice.createInjector(new AnGuiceModule());
        Robot r = inj.getInstance(Robot.class);
        assertEquals(r.toString(), "leftLeg_=LeftLeg, rightLeg_=RightLeg");
    }
}

_

class AnGuiceModule extends AbstractModule {
    protected void configure() {
        bind(Leg.class).annotatedWith(Names.named("left")).to(LeftLeg.class);
        bind(Leg.class).annotatedWith(Names.named("right")).to(RightLeg.class);
    }
}

How can i achieve the same thing with Spring 3.x (3.1.x or 3.2) using JSR-330 (optional) annotations and JavaConfig without using XML configuration?

  • 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-15T18:01:14+00:00Added an answer on June 15, 2026 at 6:01 pm

    The closest i could find is following (The definition of the Robot and Leg* classes does not change):

    public class RobotTest {
    
        @Test
        public void t1() throws Exception {
            ApplicationContext ctx = new 
                   AnnotationConfigApplicationContext(RobotConfig.class, Robot.class);
            Robot r = ctx.getBean(Robot.class);
            assertEquals("leftLeg_=LeftLeg, rightLeg_=RightLeg", r.toString());
        }
    }
    
    @Configuration
    class RobotConfig {
    
        @Bean
        public Leg leftLeg() {
            return new LeftLeg();
        }
    
        @Bean
        public Leg rightLeg() {
            return new RightLeg();
        }
    
    }
    

    Alternative would be:

    public class RobotTest {
    
        @Test public void t1() throws Exception {
            ApplicationContext ctx = new 
                   AnnotationConfigApplicationContext(RobotConfig.class);
            Robot r = ctx.getBean(Robot.class);
            assertEquals("leftLeg_=LeftLeg, rightLeg_=RightLeg", r.toString());
        }
    }
    
    @Configuration
    class RobotConfig {
    
       @Bean @Scope("prototype") public Robot robot() {
           return new Robot(leftLeg(), rightLeg());
       }
    
        @Bean @Scope("prototype") public Leg leftLeg() {
            return new LeftLeg();
        }
    
        @Bean @Scope("prototype") public Leg rightLeg() {
            return new RightLeg();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im using the following code to send email via JavaMail API can some one
I want to be able to achieve the following using guice/gin: Get all sort
I am using following code to open Android Contacts @Override public void onCreate(Bundle savedInstanceState)
I'm transforming xml using the following code. It works fine for one xslt but
The following link describes how to handle module initialization and destruction in Guice using
We are using GUICE framework as part of service layer. Now there is a
I just started to use Guice. When I am using Guice my application classes
The Guice IoC container provides MapBinder, which is often useful. We're also using Windsor
I'm trying to use construction injection using both Jersey-injected @Context parameters and Guice-injected parameters.
I'm creating a matlab GUI using GUIDE. I can't get the context menus 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.