16

I am using Spring Boot annotation configuration. I have a class whose constructor accepts 2 parameters (string, another class).

Fruit.java

public class Fruit {
    public Fruit(String FruitType, Apple apple) {
            this.FruitType = FruitType;
            this.apple = apple;
        }
}

Apple.java

public class Apple {

}

I have a class that needs to autowire the above class by injecting parameters to the constructor("iron Fruit",Apple class)

Cook.java

public class Cook {

    @Autowired
    Fruit applefruit;
}

The cook class need to autowire Fruit class with parameters("iron Fruit",Apple class)

The XML configuration looks like this:

<bean id="redapple" class="Apple" />
<bean id="greenapple" class="Apple" />
<bean name="appleCook" class="Cook">
          <constructor-arg index="0" value="iron Fruit"/>
          <constructor-arg index="1" ref="redapple"/>
</bean>
<bean name="appleCook2" class="Cook">
          <constructor-arg index="0" value="iron Fruit"/>
          <constructor-arg index="1" ref="greenapple"/>
</bean>

How to achieve it using annotation configuration only?

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
vishnumanohar
  • 671
  • 2
  • 8
  • 14

1 Answers1

15

Apple must be a spring-managed bean:

@Component
public class Apple{

}

Fruit as well:

@Component
public class Fruit {

    @Autowired
    public Fruit(
        @Value("iron Fruit") String FruitType,
        Apple apple
        ) {
            this.FruitType = FruitType;
            this.apple = apple;
        }
}

Note the usage of @Autowired and @Value annotations.

Cook should have @Component too.

Update

Or you could use @Configuration and @Bean annotations:

@Configuration 
public class Config {

    @Bean(name = "redapple")
    public Apple redApple() {
        return new Apple();
    }

    @Bean(name = "greeapple")
    public Apple greenApple() {
        retturn new Apple();
    }

    @Bean(name = "appleCook")
    public Cook appleCook() {
        return new Cook("iron Fruit", redApple());
    }
    ...
}
medvedev1088
  • 3,645
  • 24
  • 42
  • I have improved my question a bit. If I need to inject different apple(redapple or green apple), what to do. Different beans with same class . the Fruit class constructor parameters must be injected with redapple or greenapple – vishnumanohar May 29 '15 at 15:49
  • What if Apple is not spring-managed and from a third party library that can't be modified? – Claus Sep 21 '16 at 13:34
  • @Claus then you can use \@Configuration and \@Bean annotations – medvedev1088 Sep 21 '16 at 14:26
  • How? Remember any changes to the Apple class is off limit. – Claus Sep 22 '16 at 19:05
  • Isn't your second example problematic? By invoking redApple() in the Method appleCook you will NOT get the global SpringBean "redapple", but instead will create a new instance... – Falco Sep 28 '16 at 11:30
  • @Falco Spring is smart enough to handle this. See 2.2.2 Injecting Dependencies example here http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html – medvedev1088 Sep 28 '16 at 19:11
  • You are right. But this cost me a lot of nerves, because it only works when the class is annotated with \@Configuration. Else the magic doesn't work, they should immediately print a warning on finding a \@Bean Method in a class without \@Configuration – Falco Sep 29 '16 at 14:14