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
.
.
.
}
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.
Using @Autowired After enabling annotation injection, we can use autowiring on properties, setters, and constructors.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With