建設(shè)網(wǎng)站需要獨立ip嗎百度客戶服務(wù)中心
Spring的IOC/DI,依賴注入的實現(xiàn)
https://download.csdn.net/download/weixin_41957626/87546826 資源地址
?
1.什么是Spring
1.1spring3 的體系結(jié)構(gòu)圖
圖1 spring3的體系結(jié)構(gòu)圖
圖2 spring4體系結(jié)構(gòu)圖
比較spring3的體系結(jié)構(gòu)圖,spring4去掉了spring3中的struts模塊,添加了messaging模塊和websocket模塊,其他模塊保持不變。spring的jar包有20個。
1.下面是spring4和5的區(qū)別?
在增強開發(fā)方面spring4是web開發(fā)增強,spring5是JDK8的增強。
在特性改進方面spring4是注解、腳本、任務(wù)、MVC等其他特性改進,spring5是測試方面的改進。
2.IOC/DI
2.1 IOC/DI
1.控制反轉(zhuǎn)或者是依賴注入的含義。面向接口的編程。
2.實現(xiàn)方式
- 采用工廠類的方式
- 采用spring的ioc實現(xiàn)
2.2Spring的配置
1.依賴導入
<!-- spring依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
2.創(chuàng)建配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 配置bean
id指定的是bean的名稱
class指定的是bean所在的地址
ref指定的是要引入的bean
property設(shè)置的是bean的依賴的項,ref采用的是引用的數(shù)據(jù)類型的方式,value是賦一般的值的形式
-->
<bean id="studentDao" class="cn.lxz.dao.impl.StudentDaoImpl"/>
<bean id="studentService" class="cn.lxz.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean></beans>
3.項目目錄
圖3 項目目錄
4.實現(xiàn)的代碼
采用的是gif的形式展示
?
圖4 spring代碼展示
測試類代碼
@Test
public void testContext(){
//采用的是讀取xml的文件的配置形式
ApplicationContext context= new ClassPathXmlApplicationContext("Spring-context.xml");
StudentService service1= context.getBean(StudentService.class);
StudentService service2= (StudentService)context.getBean("studentService");
System.out.println(service1);
System.out.println(service2);
service1.addStudent(new Student(1,"張三"));
}
圖5 效果圖