0

Spring is famous with it Inverse Control and DI.

I know that dependency injection have several ways like Constructor Dependency Injection etc.

for example

we usually use @Autowired annotation to do Dependency Injection.

when we develop MVC web project.

My question is very simple

Why is a spring framework loosely coupled?

Suppose We have two Dao class One is Dao1 ,other is Dao2

Dao1

public class Dao {
    
    
    public void sayhi() {
        System.out.println("hello");
    }
}

Dao2

public class Dao2 {
    
    public void saygoodbye() {
        System.out.println("say goodbye");
        
    }
}

If we do not use Autowired annotation

the service should be

public class Service {
    
    Dao1 dao=new Dao1();
    
    Dao2 dao2=new Dao2();
    
    public void sayhi() {
        dao.sayhi();
        
    }
    
    public void saygoodbye() {
        
        dao2.saygoodbye();
        
    }
    
}

and the Controller should be

@RestController
public class Controller {
    
    
    Service service=new Service(     );
    
    @GetMapping("test")
    public void saysomething() {
        service.saygoodbye();
        service.sayhi();
    }
    
}

If we do not use Autowired annotation , we must use new keyword to make instance

If we use Autowired annotation

the code

Dao1 dao=new Dao1();
    
Dao2 dao2=new Dao2();

just change it into

@Autowired
Dao1 dao

@Autowired
Dao2 dao2

So,without Autowired annotation

Why is a spring framework loosely coupled?

陳寧寬
  • 33
  • 8

3 Answers3

2

If you use new keyword, then Service need to know how Dao1 and Dao2 are implemented and instantiated(concrete type, parameters...), more knowledge means more coupling.

However, if you use @Autowired, then Spring do everything for you by Dependency Injection technique, the coupling becomes more loosely.

The advantage of loosely coupling is: your code becomes more testable and maintainable.

Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
  • when i use new keyword, doent not implement any field in my Dao1 dao=new Dao1(); i can not see Spring do anything loosely coupling – 陳寧寬 Mar 31 '22 at 05:41
  • 1
    Because this example is too simple. If your code becomes more complexity, says Dao / Service need more dependencies or implements interfaces, you will see the different. – Kai-Sheng Yang Mar 31 '22 at 05:57
  • i think i will search any case need,TY – 陳寧寬 Mar 31 '22 at 08:26
1

You should read about the many benefits of dependency injection, this is a good place to start: https://en.m.wikipedia.org/wiki/Dependency_injection.

But if you want a short one line answer its main benefit is that it makes the code easier to test.

If you create the DAO classes with the new keyword in your service class you cannot easily replace them with mock objects during testing of the service class.

Johan Nordlinder
  • 1,672
  • 1
  • 13
  • 17
  • I think i know the benefit of dependecy injection,but my question is in my case i can not see Spring do more than I use new keyword, if i add more dao in my service . Spring use Autowird ,i use new. Spring and i both need to change one line. But still thanks for your answer – 陳寧寬 Mar 31 '22 at 05:44
  • 1
    For simply use cases and especially when there are no tests it doesn't matter, but when the application grows and you want to unit test every class it will become a mess without DI. So to avoid future technical depth I'd say it's best to use the DI features of the framework even for simple use cases. – Johan Nordlinder Mar 31 '22 at 06:19
  • ok,i will search any case need ,TY – 陳寧寬 Mar 31 '22 at 08:26
  • still can not find any case the autowired do for me. i feel very bad qq. – 陳寧寬 May 08 '22 at 16:34
0

First, note that field injection has been discouraged by Spring for several years now. Second, dependency injection alone does not magically guarantee loose coupling. Abstraction is also necessary. Since there is no abstraction in these examples, Service is coupled just as tightly to the Dao implementations whether you inject or instantiate them.

Spring still offers other benefits here, including lifecycle management and cohesion. Also note that since the Dao classes are not final, there is potential for abstraction, because Spring could inject subclasses whereas direct instantiation will always give you the base class.

jaco0646
  • 15,303
  • 7
  • 59
  • 83