javascript
Spring之DI依赖注入
6、(DI)依賴注入
DI與IOC的關系:
相同問題不同角度分析。
-
名稱:property
-
類型:標簽
-
歸屬:bean標簽
-
作用:使用set方法的形式為bean提供資源
-
格式:
<bean><property /> </bean> -
基本屬性:
<property name="propertyName" value="propertyValue" ref="beanId"/>
? name:對應bean中的屬性名,要求該屬性必須提供可訪問的set方法(嚴格規范為此名稱是set方法對應名稱)
? value:設定非引用類型屬性對應的值,不能與ref同時使用
? ref:設定引用類型屬性對應bean的id ,不能與value同時使用
- 注意:一個bean可以有多個property標簽
6.1、構造器注入
-
名稱:constructor-arg
-
類型:標簽
-
歸屬:bean標簽
-
作用:使用構造方法的形式為bean提供資源,兼容早期遺留系統的升級工作
-
格式:
<bean><constructor-arg /> </bean> -
基本屬性:
<constructor-arg name="argsName" value="argsValue />
? name:對應bean中的構造方法所攜帶的參數名
? value:設定非引用類型構造方法參數對應的值,不能與ref同時使用
其他屬性:
<constructor-arg index="arg-index" type="arg-type" ref="beanId"/>? ref:設定引用類型構造方法參數對應bean的id ,不能與value同時使用
? type :設定構造方法參數的類型,用于按類型匹配參數或進行類型校驗
? index :設定構造方法參數的位置,用于按位置匹配參數,參數index值從0開始計數
- 注意:一個bean可以有多個constructor-arg標簽
6.2、Set方式注入【重點】
依賴注入:Set注入!
- 依賴:bean對象的創建依賴于容器!
- 注入: 上bean對象中的所有屬性,由容器來注入!
【環境搭建】
1.復雜類型
package com.blue.pojo;public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;} }2.真實測試對象
public class Student {private String name;private Address address;private String[] books;private List<String> hobbies;private Map<String,String> card;private Set<String> games;private String wife;private Properties into;beans.xml
<?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="student" class="com.blue.pojo.Student"><!-- 第一種,普通注入,value --><property name="name" value="blue"/></bean> </beans>MyTest
public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = (Student) context.getBean("student");System.out.println(student.getName());} }集合注入方式:
-
名稱:array,list,set,map,props
-
類型:標簽
-
歸屬:property標簽 或 constructor-arg標簽
-
作用:注入集合數據類型屬性
-
格式:
<property><list></list> </property>
(1)集合類型數據注入——list
<property name="al"><list><value>itheima</value><value>66666</value></list> </property>(2)集合類型數據注入——props
<property name="properties"><props><prop key="name">itheima666</prop><prop key="value">666666</prop></props> </property>(3)集合類型數據注入——array (了解)
<property name="arr"><array><value>123456</value><value>66666</value></array> </property>(4)集合類型數據注入——set(了解)
<property name="hs"><set><value>itheima</value><value>66666</value></set> </property>(5)集合類型數據注入——map(了解)
<property name="hm"><map><entry key="name" value="itheima66666"/><entry key="value" value="6666666666"/></map> </property>掌握:props、list
案例:
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--1.創建spring控制的資源--><!--bean可以使用多個名稱,使用那么屬性完成,中間使用,隔開--><!-- <bean id="userService" name="userService1" class="com.DI.service.Impl.UserServiceImpl"><!–3.將要租注入的引用類型的變量通過property屬性進行注入,對應的name是要注入的變量名 使用ref屬性聲明要注入的bean的id–><property name="userDao" ref="userDao"/><property name="num" value="666"/><property name="version" value="楠木"/></bean>--><!--將要注入的資源聲明為bean,引用類型需要聲明,非引用類型的不用聲明,直接賦值--><!--<bean id="userDao" class="com.DI.Dao.Impl.UserDaoImpl"><!–不加name,也可以加index:從0開始–><constructor-arg name="password" value="12345"/><constructor-arg name="username" value="root"/><constructor-arg name="driver" value="123"/></bean><!–構造注入–><bean id="userService" name="userService1" class="com.DI.service.Impl.UserServiceImpl"><!– 使用構造方法進行set注入,需要保障注入的屬性與bean中定義的屬性一致–><!–一致指順序一致或類型一致或使用index解決問題–><constructor-arg name="userDao" ref="userDao"/><constructor-arg name="num" value="66666"/><constructor-arg name="version" value="楠木"/></bean>--><!--其余類型注入--><bean id="userDao" class="com.DI.Dao.Impl.UserDaoImpl"><!--不加name,也可以加index:從0開始--><constructor-arg name="password" value="12345"/><constructor-arg name="username" value="root"/><constructor-arg name="driver" value="123"/></bean><bean id="userService" name="userService1" class="com.DI.service.Impl.UserServiceImpl"><property name="userDao" ref="userDao"/><property name="bookDao" ref="bookDao"/></bean><bean id="bookDao" name="bookDao" class="com.DI.Dao.Impl.BookDaoImpl"><property name="al"><list><value>楠木1</value><value>楠木2</value></list></property><property name="props"><props><prop key="name">小米</prop><prop key="value">大米</prop></props></property><property name="arr"><array><value>6666</value><value>9999</value></array></property><property name="hs"><set><value>楠木3</value><value>楠木4</value></set></property><property name="hm"><map><entry key="name" value="楠木5"></entry><entry key="name" value="楠木6"></entry></map></property></bean></beans>6.3、拓展方式注入
-
名稱:p:propertyName,p:propertyName-ref
-
類型:屬性
-
歸屬:bean標簽
-
作用:為bean注入屬性值
-
格式:
<bean p:propertyName="propertyValue" p:propertyName-ref="beanId"/> -
注意:使用p命令空間需要先開啟spring對p命令空間的的支持,在beans標簽中添加對應空間支持
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">后續課程中還將開啟其他的命名空間,方式同上
-
案例:
<beanid="userService"class="com.itheima.service.impl.UserServiceImpl"p:userDao-ref="userDao"p:bookDao-ref="bookDao"/>
標簽P
簡化書寫,直接注入屬性值
xmlns:p="http://www.springframework.org/schema/p"注意點:p命名和c命名空間不能直接使用,需要導入xml約束!
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="user" class="com.lfs.pojo.User" p:name="blue" p:age="18"/></beans>加配置–測試:
test:
public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");User user = context.getBean("user", User.class);System.out.println(user);} }標簽C
c標簽:通過構造器注入
xmlns:c="http://www.springframework.org/schema/c" public class User {private String name;private int age;public User() {}public User(String name, int age) {this.name = name;this.age = age;} <bean id="user" class="com.blue.pojo.User" p:name="blue" p:age="18"/><bean id="user" class="com.blue.pojo.User" c:age="18" c:name="blue"/>SpEL
-
Spring提供了對EL表達式的支持,統一屬性注入格式
-
類型:屬性值
-
歸屬:value屬性值
-
作用:為bean注入屬性值
-
格式:
<property value="EL表達式"></bean> -
注意:所有屬性值不區分是否引用類型,統一使用value賦值
-
所有格式統一使用 value=“********”
-
常量 #{10} #{3.14} #{2e5} #{‘itcast’}
-
引用bean #{beanId}
-
引用bean屬性 #{beanId.propertyName}
-
引用bean方法 beanId.methodName().method2()
-
引用靜態方法 T(java.lang.Math).PI
-
運算符支持 #{3 lt 4 == 4 ge 3}
-
正則表達式支持 #{user.name matches‘[a-z]{6,}’}
-
集合支持 #{likes[3]}
-
-
案例:
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"><property name="userDao" value="#{userDao}"/><property name="bookDao" value="#{bookDao}"/><property name="num" value="#{666666666}"/><property name="version" value="#{'itcast'}"/> </bean>
properties文件加載
-
Spring提供了讀取外部properties文件的機制,使用讀取到的數據為bean的屬性賦值
-
操作步驟
1.準備外部properties文件
2.開啟context命名空間支持
xmlns:context="http://www.springframework.org/schema/context"
? 3.加載指定的properties文件
<context:property-placeholder location="classpath:filename.properties">? 4.使用加載的數據
<property name="propertyName" value="${propertiesName}"/>-
注意:如果需要加載所有的properties文件,可以使用*.properties表示加載所有的properties文件
-
注意:讀取數據使用**${propertiesName}格式進行,其中propertiesName**指properties文件中的屬性名
團隊開發
-
名稱:import
-
類型:標簽
-
歸屬:beans標簽
-
作用:在當前配置文件中導入其他配置文件中的項
-
格式:
<beans><import /> </beans> -
基本屬性:
<import resource=“config.xml"/>
? resource:加載的配置文件名
-
Spring容器加載多個配置文件
new ClassPathXmlApplicationContext("config1.xml","config2.xml"); -
Spring容器中的bean定義沖突問題
-
同id的bean,后定義的覆蓋先定義的
-
導入配置文件可以理解為將導入的配置文件復制粘貼到對應位置
-
導入配置文件的順序與位置不同可能會導致最終程序運行結果不同
-
ApplicationContext
-
1.ApplicationContext是一個接口,提供了訪問spring容器的API
-
2.ClassPathXmlApplicationContext是一個類,實現了上述功能
-
3.ApplicationContext的頂層接口是BeanFactory
-
4.BeanFactory定義了bean相關的最基本操作
-
5.ApplicationContext在BeanFactory基礎上追加了若干新功能
對比BeanFactory
-
1.BeanFactory創建的bean采用延遲加載形式,使用才創建
-
2.ApplicationContext創建的bean默認采用立即加載形式
FileSystemXmlApplicationContext
- 可以加載文件系統中任意位置的配置文件,而ClassPathXmlApplicationContext只能加載類路徑下的配置文件
BeanFactory
Resource res = new ClassPathResource("applicationContext.xml"); BeanFactory bf = new XmlBeanFactory(res); UserService userService = (UserService)bf.getBean("userService");第三方資源配置—>Druid
-
阿里數據源方案Druid
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/spring_ioc"></property><property name="username" value="root"></property><property name="password" value="root"></property> </bean>
總結
以上是生活随笔為你收集整理的Spring之DI依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring DI的三种注入方式分析
- 下一篇: JSON 数据类型