Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass class constructor parameters in Spring bean Autowired by annotations

Tags:

java

spring

The normal way without IOC containers would be:

new User("Names", 22);

where the parameter values here are dynamic, whereby, for example, they are fetched via a user submission form, thereby can't be stored in a file.

TextField userNames = new TextField();

names = userNames.getText()

same for the other parameters.

where:

@Component
public class User {
    public User(String names, int age) {
        .
        .
        .
    }
}

How do I initialize User, while passing the constructor's parameters, where User is Autowired into another class:

@Component
public class AnotherClass {
    @Autowired
    User user(....)????? // How do I do it here
    .
    .
    .
}
like image 814
Program-Me-Rev Avatar asked Dec 21 '14 20:12

Program-Me-Rev


People also ask

Is there a way to @autowire a bean that requires constructor arguments?

And this can be done either by using the @Autowired annotation or the @Value annotation. You use the @Autowired notation when the constructor argument is another Object, while the @Value annotation comes in handy when the contructor argument can easily be evaluated using Spring expression.

Can we use @autowired for constructor?

Using @Autowired After enabling annotation injection, we can use autowiring on properties, setters, and constructors.

How do you call a parameterized constructor using an Autowired 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.

What is the use of * Autowired * annotation?

Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.


2 Answers

I doubt that is what you really want to do. My guess is that User is some kind of model object that should not be handled by Spring's dependency injection.

Dependency injection (which is greatly explained here) creates and wires beans together typically when the container is started or for Spring MVC when a request is executed. The User object must therefore be created before the instance of AnotherClass is created.

If this is part of a request using Spring MVC the @ModelAttribute together with @RequestParam and @PathVariable are likely to be your friends. For some great documentation of this please check the Spring docs

like image 78
wassgren Avatar answered Oct 05 '22 18:10

wassgren


public User(  @Value("Ganesh") String names,               
@Value("27")  int age) {
names=names;
this.age=age;
}

Other than @value, using index in XML also comes with flexibility, if index=0, say, is used two times, the later value is used to overwrite the older value. Similarly, type can be specified in case of overloaded and parametrised constructors and the IOC takes care by itself to select the appropriate constructor. If type or index are not provided, the default order of constructor args is considered and if the parametrised cons does not match, it gIves exception. Suppose, setters are used and inside the bean tag, two property tags are provided for same name, in that case, exception is thrown instead of overriding the value. In setters two parameters are not allowed.

like image 44
hi.nitish Avatar answered Oct 05 '22 19:10

hi.nitish