Spring DI依賴注入實(shí)戰(zhàn)教程
1 概述
- 依賴
- A對(duì)象中有著B對(duì)象的引用,需要使用B對(duì)象完成一些操作
- A對(duì)象依賴B
- 注入
- 給A對(duì)象依賴的B對(duì)象賦值稱為注入
2 注入方式
1、set注入
2、構(gòu)造注入
3、自動(dòng)注入
4、p命名空間注入
3 可以注入的數(shù)據(jù)類型
基本類型
自定義對(duì)象
容器類型
? 數(shù)組
? 集合
? Map
? Properties
4 set注入
- 創(chuàng)建對(duì)象的時(shí)候,Spring工廠會(huì)調(diào)用set方法給對(duì)象中的屬性賦值
- 如果一個(gè)對(duì)象中的屬性沒(méi)有添加set方法,使用property賦值的時(shí)候偶會(huì)報(bào)錯(cuò)
Error:(11, 13) java: 找不到符號(hào) 符號(hào): 方法 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)造器注入
通過(guò)對(duì)象的構(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 自動(dòng)注入
在bean標(biāo)簽中聲明autowire的方式
Spring框架會(huì)在容器中查找符合參數(shù)的數(shù)據(jù)進(jìn)行注入
- 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);
}自動(dòng)注入還是通過(guò)set注入的方式注入
7 p命名空間注入
- 使用p作為屬性的前綴,調(diào)用屬性執(zhí)行復(fù)制的操作
- 實(shí)質(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>測(cè)試
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
25行Java代碼將普通圖片轉(zhuǎn)換為字符畫圖片和文本的實(shí)現(xiàn)
這篇文章主要介紹了25行Java代碼將普通圖片轉(zhuǎn)換為字符畫圖片和文本的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Spring事務(wù)失效場(chǎng)景的詳細(xì)整理
Spring 事務(wù)的傳播特性說(shuō)的是,當(dāng)多個(gè)事務(wù)同時(shí)存在的時(shí)候,Spring 如何處理這些事務(wù)的特性,下面這篇文章主要給大家介紹了關(guān)于Spring事務(wù)失效場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2022-02-02
Java獲取接口的所有實(shí)現(xiàn)類方法總結(jié)示例
這篇文章主要給大家介紹了關(guān)于Java獲取接口的所有實(shí)現(xiàn)類方法的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-06-06
SpringbBoot實(shí)現(xiàn)Tomcat集群的會(huì)話管理的詳細(xì)過(guò)程
文章介紹了如何使用Nginx作為負(fù)載均衡器和SpringSession配合Redis實(shí)現(xiàn)Tomcat集群的會(huì)話共享,確保跨節(jié)點(diǎn)訪問(wèn)時(shí)會(huì)話的一致性和持久性,通過(guò)具體的步驟和示例代碼,感興趣的朋友一起看看吧2024-12-12
基于SpringMVC攔截器實(shí)現(xiàn)接口耗時(shí)監(jiān)控功能
本文呢主要介紹了基于SpringMVC攔截器實(shí)現(xiàn)的接口耗時(shí)監(jiān)控功能,統(tǒng)計(jì)接口的耗時(shí)情況屬于一個(gè)可以復(fù)用的功能點(diǎn),因此這里直接使用 SpringMVC的HandlerInterceptor攔截器來(lái)實(shí)現(xiàn),需要的朋友可以參考下2024-02-02

