Hi I guess this question is quite basic but please help out!!!
Let say I have one interface Write and 2 implementing classes Pen and Pencil
public interface Write {
public void writeSomething();
}
public class Pen implements Write {
@Override
public void writeSomething() {
System.out.println("Writing using Pen!!");
}
}
public class Pencil implements Write {
@Override
public void writeSomething() {
System.out.println("Writing using Pencil!!");
}
}
public class Test1 {
public static void main(String[] args) {
Write wr = new Pen();
wr.writeSomething();
}
}
public class Test2 {
@Autowired
@Qualifier("Pen")
private static Write wr;
public static void main(String[] args) {
wr.writeSomething();
}
}
Now,
How does Test2 class implementation of spring boot is better then Test1 class implementaion, in terms of loose coupling in java.
And what is the benefit of @Autowired then ?
The
@Autowired
annotation create a singleObject(Bean)
that your application reused instead of creating new Object each time.