Spring DI依赖注入实战教程
作者:你读书了吗?
这篇文章主要介绍了SpringDI依赖注入实战教程,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
1 概述
- 依赖
- A对象中有着B对象的引用,需要使用B对象完成一些操作
- A对象依赖B
- 注入
- 给A对象依赖的B对象赋值称为注入
2 注入方式
1、set注入
2、构造注入
3、自动注入
4、p命名空间注入
3 可以注入的数据类型
基本类型
自定义对象
容器类型
数组
集合
Map
Properties
4 set注入
- 创建对象的时候,Spring工厂会调用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="关羽"></property>
<property name="info" value="卖杂粮的关羽"></property>
</bean>5 构造器注入
通过对象的构造器注入属性
<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框架会在容器中查找符合参数的数据进行注入
- 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作为属性的前缀,调用属性执行复制的操作
- 实质上还是使用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 数据源注入
- 使用Spring工厂注入Druid数据源
- 依赖
<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中声明数据源的引用
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>
<!-- 配置数据源 -->
<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 容器类型数据注入
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>
<!-- 注入数组 -->
<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参数 -->
<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);
}
}到此这篇关于SpringDI依赖注入的文章就介绍到这了,更多相关SpringDI依赖注入内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
