1. 程式人生 > 資訊 >加拿大將投資 2.4 億加元發展半導體產業,支援晶片製造和研究

加拿大將投資 2.4 億加元發展半導體產業,支援晶片製造和研究

Spring是一個輕量級的控制反轉和麵向切面程式設計的框架 所有版本的doc:https://docs.spring.io/spring-framework/docs/ 1.安裝,在pom檔案下新增依賴 ```xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.6.RELEASE</version> </dependency> </dependencies> ``` 2.在resources目錄下的beans xml檔案中配置bean。bean的id值為相當於變數名,class的值相當於型別,properties用於給物件的賦值,其中value:具體的值,基本資料型別,ref:引用Spring容器中建立好的物件。 ```xml <bean id="hello" class="com.libaiwen.pojo.Hello"> <property name="str" value="Spring"/> </bean> ```
3.測試 ```java public class HelloSpringTest { public static void main(String[] args) { //拿到容器contex ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //需要什麼就get拿什麼 Hello hello = (Hello) context.getBean("hello"); System.out.println(hello.toString());
} } ```
在測試中沒有顯式的new Hello物件,但是卻可以使用hello,Hello物件是由Spring建立的,其屬性也是由Spring容器設定的。這個過程就叫做控制反轉。
控制:誰來控制物件的建立,傳統應用程式的物件由程式本身控制建立,使用Spring後,物件是由Spring來建立的。
反轉:程式本身不建立物件,而程式設計被動的接受物件。
依賴注入:就是利用set方法進行注入