--- title: '01-Spring Boot Ioc and DI' disqus: hackmd --- ###### tags: `SpringCore` Spring Boot bean 的注入方式@Autowired === [TOC] ## IoC 和 DI的關係 控制反轉是一種設計原則、一種概念,而依賴注入則是實現這種概念的方式 ,依賴注入只是其中一種實踐他的方法 ## 英文解釋 IOC(Inversion of Control) is a concept that means: Instead of creating objects with the new operator,let the container do it for you. DI(Dependency injection) is way to inject the dependency of a framework component by the following ways of spring: ## Types of dependency injections Constructor Injection Setter Injection Field Injection(很少用暫且忽略) ## 沒有依賴注入的例子 ```java= package com.allproducts.ai_center.springcore; class Address { private String street; private String city; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } /** * @param street * @param city */ Address(String street, String city) { this.street = street; this.city = city; } } ``` ```java= package com.allproducts.ai_center.springcore; class Employee { private Address address; Employee() { address = new Address("Astreet","taipei"); } public void printEmployaddress() { System.out.println(address.getCity()); } public static void main(String[] args) { Employee myEmployee = new Employee(); myEmployee.printEmployaddress(); } } ``` ## 沒有依賴注入,而且新增了欄位 country ```java= package com.allproducts.ai_center.springcore; class Address { private String street; private String city; private String country; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } /** * @param street * @param city */ Address(String street, String city,String country) { this.street = street; this.city = city; this.country=country; } } ``` ```java= package com.allproducts.ai_center.springcore; class Employee { private Address address; Employee() { address = new Address("Astreet","taipei","taiwan"); } public void printEmployaddress() { System.out.println(address.getCity()); } public static void main(String[] args) { Employee myEmployee = new Employee(); myEmployee.printEmployaddress(); } } ``` > 除了必須要更改Address的constructur以外,Employee也要更著改 ## 使用了dependency injection後 ```java= package com.allproducts.ai_center.springcore; class Employee { private Address address; /** * @param address */ public Employee(Address address) { super(); this.address = address; } public void printEmployaddress() { System.out.println(address.getCity()); } public static void main(String[] args) { Address myAddress=new Address("B street", "taipei", "taiwan"); Employee myEmployeewithDI = new Employee( myAddress); myEmployeewithDI.printEmployaddress(); } } ``` ## 參考連結 [What is the advantage of autowiring in spring](https://stackoverflow.com/questions/16344062/what-is-the-advantage-of-autowiring-in-spring) [Spring Framework](https://spring.io/projects/spring-framework) [Spring 使用@Autowired依賴注入物件](https://matthung0807.blogspot.com/2018/08/spring-autowired.html) [Spring Dependency Injection with Example](https://www.geeksforgeeks.org/spring-dependency-injection-with-example/) [Inversion of Control and Dependency Injection](https://medium.com/geekculture/inversion-of-control-and-dependency-injection-58240337ef1a) [必看What is Dependency Injection and Inversion of Control in Spring Framework?](https://stackoverflow.com/questions/9403155/what-is-dependency-injection-and-inversion-of-control-in-spring-framework) [Spring Dependency Injection – Field vs Setter vs Constructor Injection](http://www.amitph.com/spring-field-setter-constructor-injection/)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up