Suppose i have class named SomeClass
which takes value in constructor and populates the field.
String text;
String sometext;
public someClass(String text, String sometext){
this.text = text;
this.sometext = sometext;
}
SomeClass
has a method which creates a new object. In java when we create a new object we can instantiate with values like
ClassName variable = new ClassName(text, sometext);
and populate the fields in ClassName
like this using constructor
public ClassName(String text, String sometext){
this.text = text;
this.sometext = sometext;
}
But in spring using autowired how can we do that?
@Autowired
public ClassName(SomeClass someClass){
this.text = someClass.text;
this.sometext = someClass.sometext;
}
This wont work. how spring will know which the instance of SomeClass
UPDATE:
I was wrong. i was not thinking in DI way. instead of autowiring SomeClass
. i had to autowire ClassName
.
Without DI i created a new object in a method of class ClassName
When using DI i had to autowire in the method of the class ClassName
@Autowired
ClassName className;
I can populate the fields directly making the fields public
className.text = text;
className.sometext = sometext;
and i can do using javabeans. but how to do via constructor.
NOTE: there is nothing wrong with spring config. base scan is enabled.