Maybe the best way to give a question will be to show code below :
public class DBConnection {
private String Host;
private String username;
private String password;
public DBConnection(String Host, String username, String password)
{
this.Host = Host;
this.username = username;
this.password = password;
}
public void addUser(String name)
{
System.out.println("add to db");
}
}
And class below :
public class UserService {
public DBConnection dbConnection;
public UserService(DBConnection dbConnection){
this.dbConnection = dbConnection;
}
public void register()
{
dbConnection.addUser("xd");
}
}
And now, why i have to use spring for DI ? I can't see difference between above code and code below (of course in below case i know that in DBConnection i have to use @Component):
public class UserService {
public DBConnection dbConnection;
@Autowired
public UserService(DBConnection dbConnection){
this.dbConnection = dbConnection;
}
public void register()
{
dbConnection.addUser("xd");
}
}
I can do DI without Spring. What is advantage of using spring Boot?