[請益] Spring boot的依賴注入降低耦合的例子
推文有個連結有解答我的疑惑
感謝bron大
文章有點長
先說說我對依賴注入的理解
Spring boot
依賴注入大致有三種方式
透過建構子的 透過setter的 或是 field
這三種都可以透過@Autowired註解來達到依賴注入的效果
我自己想到的建構子的舉例是
假設有兩個類 Address 和 Employee好了
1.
public class Address {
String Country;
String City;
String Street;
public Address(String country, String city, String street) {
Country = country;
City = city;
Street = street;
}
}
2.
public class Employee {
String sex;
String name;
Address address;
// 沒有依賴注入的方式
public Employee(String Country,String City,String Street,String
sex, String name ) {
this.sex=sex;
this.address = new Address( Country, City,Street );
this.name=name;
}
// 有依賴注入的方式
public Employee(String sex, String name, Address address) {
this.sex = sex;
this.name = name;
this.address = address;
}
}
在上面的例子當中可以發現,如果哪一天
Address這個類新增了一個屬性叫 phoneNumber好了
沒有依賴注入的方式,必須要更改 Employee 的
this.address =new Address(Country,City,Street,phoneNumber)
而有依賴注入的方式確實降低了耦合
因為他不用更改Employee的建構方式
所以我理解依賴注入可以降低耦合
所以我理解依賴注入可以降低耦合
所以我理解依賴注入可以降低耦合
但我的問題是Spring boot 的 autowird annotation 有幫助我們降低耦合嗎
在常見的開發中 我們經常都會有 Dao 以及 Service
假設我有兩個 Dao 好了 分別是 Dao1 和 Dao2
以及一個Service
Dao1
public class Dao {
public void sayhi() {
System.out.println("hello");
}
}
Dao1
public class Dao {
public void sayhi() {
System.out.println("hello");
}
}
Dao2
public class Dao2 {
public void saygoodbye() {
System.out.println("say goodbye");
}
}
如果我不在service上面使用autowired
我的service會是
public class Service {
Dao1 dao=new Dao1();
Dao2 dao2=new Dao2();
public void sayhi() {
dao.sayhi();
}
public void saygoodbye() {
dao2.saygoodbye();
}
}
如果我使用了@Autowired註解
那我只是將
Dao1 dao=new Dao1();
Dao2 dao2=new Dao2();
替換成
@Autowired
Dao1 dao
@Autowired
Dao2 dao2
我想請問所以我使用了Autowired註解
我知道我可以不需要使用new 來建構實體
但 Spring 真的有幫我降低耦合嗎
即使我換成 setter 配合 autowired的方式好了
那個 setter也是要我自己去撰寫
Spring 幫我降低了耦合甚麼?
我的問題簡單來說就是
我知道依賴注入可以降低耦合
但Spring boot透過 @Autowired註解幫我降低耦合在哪
謝謝
p.s 因為面試的時候常常被面試官問說懂不懂甚麼是
控制反轉還有DI,我基本上舉例都舉 Address還有 Employee的例子
但當我反問下面例子的時候,他們好像也說要再回去想一下...
只有其中一個就說更複雜的例子會用到,但也沒說甚麼是更複雜的例子QQ
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.167.157.11 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Soft_Job/M.1648731970.A.382.html
※ 編輯: ntpuisbest (49.216.186.239 臺灣), 03/31/2022 21:14:38
推
03/31 21:27,
3年前
, 1F
03/31 21:27, 1F
推
03/31 21:29,
3年前
, 2F
03/31 21:29, 2F
→
03/31 21:29,
3年前
, 3F
03/31 21:29, 3F
→
03/31 21:29,
3年前
, 4F
03/31 21:29, 4F
→
03/31 21:29,
3年前
, 5F
03/31 21:29, 5F
→
03/31 21:29,
3年前
, 6F
03/31 21:29, 6F
→
03/31 21:29,
3年前
, 7F
03/31 21:29, 7F
→
03/31 21:29,
3年前
, 8F
03/31 21:29, 8F
→
03/31 21:29,
3年前
, 9F
03/31 21:29, 9F
→
03/31 21:29,
3年前
, 10F
03/31 21:29, 10F
推
03/31 21:33,
3年前
, 11F
03/31 21:33, 11F
推
03/31 21:35,
3年前
, 12F
03/31 21:35, 12F
→
03/31 21:39,
3年前
, 13F
03/31 21:39, 13F
→
03/31 21:42,
3年前
, 14F
03/31 21:42, 14F
推
03/31 21:43,
3年前
, 15F
03/31 21:43, 15F
→
03/31 21:44,
3年前
, 16F
03/31 21:44, 16F
→
03/31 22:00,
3年前
, 17F
03/31 22:00, 17F
→
03/31 22:01,
3年前
, 18F
03/31 22:01, 18F
→
03/31 22:03,
3年前
, 19F
03/31 22:03, 19F
→
03/31 22:03,
3年前
, 20F
03/31 22:03, 20F
推
03/31 22:04,
3年前
, 21F
03/31 22:04, 21F
→
03/31 22:05,
3年前
, 22F
03/31 22:05, 22F
→
03/31 22:07,
3年前
, 23F
03/31 22:07, 23F
→
03/31 22:07,
3年前
, 24F
03/31 22:07, 24F
→
03/31 22:08,
3年前
, 25F
03/31 22:08, 25F
→
03/31 22:08,
3年前
, 26F
03/31 22:08, 26F
→
03/31 22:10,
3年前
, 27F
03/31 22:10, 27F
→
03/31 22:10,
3年前
, 28F
03/31 22:10, 28F
→
03/31 22:13,
3年前
, 29F
03/31 22:13, 29F
→
03/31 22:16,
3年前
, 30F
03/31 22:16, 30F
→
03/31 22:16,
3年前
, 31F
03/31 22:16, 31F
→
03/31 22:18,
3年前
, 32F
03/31 22:18, 32F
→
03/31 22:18,
3年前
, 33F
03/31 22:18, 33F
推
03/31 22:19,
3年前
, 34F
03/31 22:19, 34F
→
03/31 22:24,
3年前
, 35F
03/31 22:24, 35F
→
03/31 22:24,
3年前
, 36F
03/31 22:24, 36F
→
03/31 22:24,
3年前
, 37F
03/31 22:24, 37F
→
03/31 22:24,
3年前
, 38F
03/31 22:24, 38F
我有試著在Controller當中去new service
@RestController
public class Controller {
Service service=new Service( );
@GetMapping("test")
public void saysomething() {
service.saygoodbye();
service.sayhi();
}
}
即使Service 裡面需要20個DAO好了
在Controller裡面new Service不也一樣只要
一行
Service service=new Service( );
另外如果是setter 或是 Constructor方式的 DI
就我的理解 setter和建構子也是要自己寫
Spring 不會幫你產生
那我這樣看起來好像只是幫你從 new 換成了 @Autowired
這樣真的看不太出來 降低了甚麼耦合
因為建構子也是要自己寫啊
我覺得我好像陷入了泥淖中了QQ
翻了很多網頁,舉的例子大都跟我自己舉的 員工還有地址的差不多
※ 編輯: ntpuisbest (118.167.157.11 臺灣), 03/31/2022 22:36:56
還有 49 則推文
還有 1 段內文
→
04/01 09:29,
3年前
, 88F
04/01 09:29, 88F
→
04/01 10:25,
3年前
, 89F
04/01 10:25, 89F
→
04/01 10:27,
3年前
, 90F
04/01 10:27, 90F
→
04/01 10:27,
3年前
, 91F
04/01 10:27, 91F
推
04/01 10:40,
3年前
, 92F
04/01 10:40, 92F
→
04/01 10:40,
3年前
, 93F
04/01 10:40, 93F
推
04/01 10:52,
3年前
, 94F
04/01 10:52, 94F
→
04/01 10:52,
3年前
, 95F
04/01 10:52, 95F
推
04/01 13:45,
3年前
, 96F
04/01 13:45, 96F
→
04/01 13:45,
3年前
, 97F
04/01 13:45, 97F
→
04/01 18:42,
3年前
, 98F
04/01 18:42, 98F
→
04/01 18:42,
3年前
, 99F
04/01 18:42, 99F
→
04/01 23:51,
3年前
, 100F
04/01 23:51, 100F
→
04/01 23:51,
3年前
, 101F
04/01 23:51, 101F
→
04/01 23:52,
3年前
, 102F
04/01 23:52, 102F
→
04/01 23:53,
3年前
, 103F
04/01 23:53, 103F
→
04/01 23:54,
3年前
, 104F
04/01 23:54, 104F
→
04/01 23:55,
3年前
, 105F
04/01 23:55, 105F
→
04/01 23:57,
3年前
, 106F
04/01 23:57, 106F
→
04/01 23:57,
3年前
, 107F
04/01 23:57, 107F
→
04/01 23:57,
3年前
, 108F
04/01 23:57, 108F
→
04/02 00:00,
3年前
, 109F
04/02 00:00, 109F
→
04/02 00:01,
3年前
, 110F
04/02 00:01, 110F
→
04/02 00:06,
3年前
, 111F
04/02 00:06, 111F
→
04/02 10:32,
3年前
, 112F
04/02 10:32, 112F
→
04/02 10:32,
3年前
, 113F
04/02 10:32, 113F
→
04/02 22:42,
3年前
, 114F
04/02 22:42, 114F
→
04/02 22:44,
3年前
, 115F
04/02 22:44, 115F
→
04/02 22:46,
3年前
, 116F
04/02 22:46, 116F
→
04/02 22:48,
3年前
, 117F
04/02 22:48, 117F
推
04/03 10:31,
3年前
, 118F
04/03 10:31, 118F
→
04/03 10:31,
3年前
, 119F
04/03 10:31, 119F
→
04/03 10:31,
3年前
, 120F
04/03 10:31, 120F
推
04/03 21:17,
3年前
, 121F
04/03 21:17, 121F
→
04/03 21:17,
3年前
, 122F
04/03 21:17, 122F
推
04/04 13:21,
3年前
, 123F
04/04 13:21, 123F
→
04/04 13:21,
3年前
, 124F
04/04 13:21, 124F
→
04/04 13:21,
3年前
, 125F
04/04 13:21, 125F
推
04/04 14:04,
3年前
, 126F
04/04 14:04, 126F

推
04/04 21:15,
3年前
, 127F
04/04 21:15, 127F
討論串 (同標題文章)
Soft_Job 近期熱門文章
PTT職涯區 即時熱門文章