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依赖注入内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论