Spring DI依賴注入實戰(zhàn)教程
1 概述
- 依賴
- A對象中有著B對象的引用,需要使用B對象完成一些操作
- A對象依賴B
- 注入
- 給A對象依賴的B對象賦值稱為注入
2 注入方式
1、set注入
2、構(gòu)造注入
3、自動注入
4、p命名空間注入
3 可以注入的數(shù)據(jù)類型
基本類型
自定義對象
容器類型
? 數(shù)組
? 集合
? Map
? Properties
4 set注入
- 創(chuàng)建對象的時候,Spring工廠會調(diào)用set方法給對象中的屬性賦值
- 如果一個對象中的屬性沒有添加set方法,使用property賦值的時候偶會報錯
Error:(11, 13) java: 找不到符號 符號: 方法 setId(int) 位置: 類型為com.sw.entity.User的變量 user
import lombok.Data; @Data public class User { private Integer id; private String username; private String password; private String addr; private String info; }
<!-- 定義user --> <bean id="user" class="com.sw.entity.User"> <property name="id" value="100111"></property> <property name="username" value="關(guān)羽"></property> <property name="info" value="賣雜糧的關(guān)羽"></property> </bean>
5 構(gòu)造器注入
通過對象的構(gòu)造器注入屬性
<constructor-arg name="id" value="100110"></constructor-arg>
package com.sw.entity; public class Hero { private Integer id; private String username; private String password; private String addr; private String info; public Hero() { } public Hero(String username, String password) { this.username = username; this.password = password; } public Hero(Integer id, String username) { this.id = id; this.username = username; } public Hero(Integer id, String username, String password, String addr, String info) { this.id = id; this.username = username; this.password = password; this.addr = addr; this.info = info; } @Override public String toString() { return "Hero{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", addr='" + addr + '\'' + ", info='" + info + '\'' + '}'; } }
<!-- 定義Hero --> <bean id="hero" class="com.sw.entity.Hero"> <constructor-arg name="id" value="100110"></constructor-arg> <constructor-arg name="username" value="劉備"></constructor-arg> </bean> <bean id="hero02" class="com.sw.entity.Hero"> <constructor-arg name="username" value="趙云"></constructor-arg> <constructor-arg name="password" value="zhao"></constructor-arg> </bean>
6 自動注入
在bean標簽中聲明autowire的方式
Spring框架會在容器中查找符合參數(shù)的數(shù)據(jù)進行注入
- byType
- byName
package com.sw.service.impl; import com.sw.dao.HeroDao; import com.sw.entity.Hero; import com.sw.service.HeroService; public class HeroServiceImpl implements HeroService { private HeroDao heroDao; public void setHeroDao(HeroDao heroDao) { this.heroDao = heroDao; } @Override public Hero queryHeroById(Integer id) { return heroDao.selectHeroById(id); } }
<!-- heroService --> <bean id="heroService01" class="com.sw.service.impl.HeroServiceImpl" autowire="byType"></bean> <!-- heroService --> <bean id="heroService02" class="com.sw.service.impl.HeroServiceImpl" autowire="byName"></bean>
@Test public void getHero01(){ ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); HeroService heroService = ioc.getBean("heroService01",HeroService.class); Hero hero = heroService.queryHeroById(10010); System.out.println(hero); } @Test public void getHero02(){ ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); HeroService heroService = ioc.getBean("heroService02",HeroService.class); Hero hero = heroService.queryHeroById(1001111); System.out.println(hero); }
自動注入還是通過set注入的方式注入
7 p命名空間注入
- 使用p作為屬性的前綴,調(diào)用屬性執(zhí)行復(fù)制的操作
- 實質(zhì)上還是使用set方法賦值
xmlns:p=“http://www.springframework.org/schema/p”
<!-- stu --> <bean id="stu" class="com.sw.entity.Stu" p:id="100111" p:username="張三"></bean>
@Test public void getStu(){ ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); Stu stu = ioc.getBean(Stu.class); System.out.println(stu); }
8 數(shù)據(jù)源注入
- 使用Spring工廠注入Druid數(shù)據(jù)源
- 依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
在dao中聲明數(shù)據(jù)源的引用
package com.sw.dao.impl; import com.alibaba.druid.pool.DruidDataSource; import com.sw.dao.HeroDao; import com.sw.entity.Hero; import java.sql.SQLException; public class HeroDaoImpl implements HeroDao { private DruidDataSource dataSource; public DruidDataSource getDataSource() { return dataSource; } public void setDataSource(DruidDataSource dataSource) { this.dataSource = dataSource; } @Override public Hero selectHeroById(Integer id) { try { System.out.println(dataSource.getConnection()); } catch (SQLException e) { e.printStackTrace(); } Hero hero = new Hero(); hero.setId(id); return hero; } }
配置
<!-- 引入外部配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!-- 配置數(shù)據(jù)源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- heroDao --> <bean id="heroDao" class="com.sw.dao.impl.HeroDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean>
測試
package com.sw; import com.sw.dao.HeroDao; import com.sw.entity.Hero; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.sql.SQLException; public class TestHeroDao { @Test public void getHeroDao() { ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); HeroDao heroDao = ioc.getBean(HeroDao.class); Hero hero = heroDao.selectHeroById(222); } }
4.9 容器類型數(shù)據(jù)注入
package com.sw.entity; import lombok.Data; import java.util.List; import java.util.Map; @Data public class Person { private Integer id; private String username; private String gender; private String info; private String[] hobby; private List<String> friend; private Map<String,String> phones; }
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- person基本屬性注入 --> <bean id="person01" class="com.sw.entity.Person"> <property name="id" value="100100"></property> <property name="username" value="宋江"></property> <property name="gender" value="male"></property> <property name="info" value="梁山頭頭"></property> </bean> <!-- 注入數(shù)組 --> <bean id="person02" class="com.sw.entity.Person"> <property name="hobby"> <array> <value>籃球</value> <value>足球</value> <value>乒乓球</value> </array> </property> </bean> <bean id="person03" class="com.sw.entity.Person"> <property name="friend"> <list> <value>晁蓋</value> <value>扈三娘</value> <value>王婆</value> <value>武松</value> </list> </property> </bean> <!-- map參數(shù) --> <bean id="person04" class="com.sw.entity.Person"> <property name="phones"> <map> <entry key="魯智深" value="10010"></entry> <entry key="林沖" value="10086"></entry> <entry key="李逵" value="10000"></entry> <entry key="李鬼" value="100000"></entry> </map> </property> </bean> </beans>
package com.sw; import com.sw.entity.Person; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestPerson { @Test public void getPerson01(){ ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person01 = ioc.getBean("person01", Person.class); System.out.println(person01); } @Test public void getPerson02(){ ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person02 = ioc.getBean("person02", Person.class); System.out.println(person02); } @Test public void getPerson03(){ ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person03 = ioc.getBean("person03", Person.class); System.out.println(person03); } @Test public void getPerson04(){ ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person04 = ioc.getBean("person04", Person.class); System.out.println(person04); } }
到此這篇關(guān)于SpringDI依賴注入的文章就介紹到這了,更多相關(guān)SpringDI依賴注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
25行Java代碼將普通圖片轉(zhuǎn)換為字符畫圖片和文本的實現(xiàn)
這篇文章主要介紹了25行Java代碼將普通圖片轉(zhuǎn)換為字符畫圖片和文本的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04Java獲取接口的所有實現(xiàn)類方法總結(jié)示例
這篇文章主要給大家介紹了關(guān)于Java獲取接口的所有實現(xiàn)類方法的相關(guān)資料,文中通過代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-06-06SpringbBoot實現(xiàn)Tomcat集群的會話管理的詳細過程
文章介紹了如何使用Nginx作為負載均衡器和SpringSession配合Redis實現(xiàn)Tomcat集群的會話共享,確??绻?jié)點訪問時會話的一致性和持久性,通過具體的步驟和示例代碼,感興趣的朋友一起看看吧2024-12-12基于SpringMVC攔截器實現(xiàn)接口耗時監(jiān)控功能
本文呢主要介紹了基于SpringMVC攔截器實現(xiàn)的接口耗時監(jiān)控功能,統(tǒng)計接口的耗時情況屬于一個可以復(fù)用的功能點,因此這里直接使用 SpringMVC的HandlerInterceptor攔截器來實現(xiàn),需要的朋友可以參考下2024-02-02