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

The usual way without IOC containers would be:

new User("Names", 22);

where the parameter values ​​here are dynamic, as a result of which, for example, they are selected through the user presentation form, therefore they cannot be saved in a file.

TextField userNames = new TextField();

names = userNames.getText()

for other parameters.

Where:

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

How to initialize Userwhen passing constructor parameters, where User Autowiredto another class:

@Component
public class AnotherClass {
    @Autowired
    User user(....)????? // How do I do it here
    .
    .
    .
}
+4
source share
2 answers

I doubt that this is what you really want to do. I assume that Userthis is some kind of model object that should not be handled by Spring dependency injection.

( ) beans , , Spring MVC , User AnotherClass.

Spring MVC, @ModelAttribute @RequestParam @PathVariable . , , Spring docs

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

@value, XML , index = 0, , , . , , IOC . , , , gIves. , bean , . .

+2

Source: https://habr.com/ru/post/1568549/


All Articles