Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject parameters to constructor through annotation in Spring

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?

like image 518
vishnumanohar Avatar asked May 29 '15 14:05

vishnumanohar


People also ask

What is AllArgsConstructor annotation in Spring boot?

The @AllArgsConstructor annotation generates a constructor with one parameter for every field in the class. Fields that are annotated with @NonNull result in null checks with the corresponding parameters in the constructor. The annotation won't generate a parameter for the static and initialized final fields.

Why is constructor injection better than Autowired?

Constructor injection makes code more robust. It allows us to create immutable objects, preventing NullPointerException s and other errors. You can find the code example on GitHub.

How do you create a parameterized constructor in Spring boot?

You need to specify this bean in the constructor: @Component public class MainClass { private final AnotherClass anotherClass; // this annotation is NOT required if there is only 1 constructor, shown for clarity. @Autowired MainClass(AnotherClass anotherClass) { this.


1 Answers

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());
    }
    ...
}
like image 80
medvedev1088 Avatar answered Sep 27 '22 21:09

medvedev1088