4.spring 依赖注入的三种方式
来源:互联网 发布:会员视频源码 编辑:程序博客网 时间:2024/06/14 14:42
Spring依赖注入的方式(3种):
1.属性注入:是通过setter方法注入Bean的属性值或依赖的对象(使用多)方法:<property name=" " value=" " ref=" ">
<bean id="userInfo" class="com.UserInfo" scope="singleton">
<property name="userName" value="悟空"></property>
<property name="userId" value="1001"></property>
<property name="department" ref="department"></propety>
</bean>
2.构造器注入:是通过构造方法注入Bean的属性值或依赖的对象(使用少):是通过构造方法注入Bean的属性值或依赖的对象
第一步:在类中需要生成构造方法
第二步:在bean.xml中
<!--部门的bean 构造方法注入-->
方法:<constructor-arg>
1.按索引下标匹配参数
<!--按索引下标来匹配参数>
<bean id="department" class="com.Department">
<constructor-arg index="0" value="102"/>
<constructor-arg index="1" value="技术部"/>
</bean>
2.按参数类型匹配参数
<!--按参数类型配置参数-->
<bean id="userInfo" class="com.UserInfo">
<constructor-arg value="1002" type="java.lang.Integer"/>
<constructor-arg value="八戒" type="java.lang.String"/>
<constructor-arg value="department" type="com.Department"/>
</bean>
3.接口(工厂方法注入)(极少使用)
实例1:属性注入
1.结构目录:
代码:
Department.java
package com;public class Department {private Integer deId;private String deName;public void setDeId(Integer deId) {this.deId = deId;}public void setDeName(String deName) {this.deName = deName;}@Overridepublic String toString() {return "Department [deId=" + deId + ", deName=" + deName + "]";}}
package com;public class UserInfo {private Integer userId;private String userName;private Department department;public void setUserId(Integer userId) {this.userId = userId;}public void setUserName(String userName) {this.userName = userName;}public void getUserName() {System.out.println("姓名1:"+userName);}public void setDepartment(Department department) {this.department = department;}@Overridepublic String toString() {return "UserInfo [userId=" + userId + ", userName=" + userName + ", department=" + department + "]";}}
Test.java
package test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.UserInfo;public class Test {public static void main(String[] args) {BeanFactory bf=new ClassPathXmlApplicationContext("beans.xml");UserInfo user=(UserInfo)bf.getBean("userInfo");System.out.print(user);//UserInfo [userId=1, userName=悟空, department=Department [deId=101, deName=技术部]]user.getUserName();//姓名1:悟空}}
<?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-3.0.xsd"><!-- 部门的bean --> <bean id="department" class="com.Department"><property name="deId" value="101"></property><property name="deName" value="技术部"></property></bean><!-- UserInfo的bean --><!--UserInfo userInfo=new UserInfo(); userInfo.setUserName("悟空");--> <bean id="userInfo" class="com.UserInfo"> <property name="userId" value="001"/><!-- 属性值注入 --> <property name="userName" value="悟空"/><!-- 属性值注入 --> <property name="department" ref="department"></property><!-- 对象注入 --> </bean></beans>
打印结果:
UserInfo [userId=1, userName=悟空, department=Department [deId=101, deName=技术部]]姓名1:悟空
实例2:构造器注入
1.结构目录
2.代码
Department.java
package com;public class Department {private Integer deId;private String deName;public void setDeId(Integer deId) {this.deId = deId;}public void setDeName(String deName) {this.deName = deName;}//构造方法,无参和有参public Department() {};public Department(Integer deId, String deName) {super();this.deId = deId;this.deName = deName;}@Overridepublic String toString() {return "Department [deId=" + deId + ", deName=" + deName + "]";}}
package test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.Department;public class Test {public static void main(String[] args) {BeanFactory bf=new ClassPathXmlApplicationContext("beans.xml");Department de=(Department)bf.getBean("department");System.out.print(de);}}
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-3.0.xsd"><!-- 部门的bean --><!-- 1.按索引下标匹配参数 --><!-- <bean id="department" class="com.Department"><constructor-arg index="0" value="101"/><constructor-arg index="1" value="研发部"/></bean> --> <!-- 2.按参数类型匹配参数 --><bean id="department" class="com.Department"><constructor-arg value="八戒" type="java.lang.String"></constructor-arg><constructor-arg value="110" type="java.lang.Integer"></constructor-arg></bean></beans>
Department [deId=110, deName=八戒]
阅读全文
0 0
- spring依赖注入的三种方式
- Spring依赖注入的三种方式
- Spring依赖注入的三种方式
- Spring依赖注入的三种方式
- Spring依赖注入的三种方式
- Spring依赖注入的三种方式
- Spring的三种依赖注入方式
- spring的依赖注入三种方式
- spring依赖注入的三种方式
- Spring依赖注入的三种方式
- 4.spring 依赖注入的三种方式
- 【Spring】依赖注入三种方式
- Spring -- 三种依赖注入方式
- Spring-三种依赖注入方式
- Spring-三种依赖注入方式
- Spring依赖注入(DI)的三种实现方式
- Spring依赖注入(DI)的三种方式
- Spring配置依赖注入的三种方式
- 英语还是要学的,太软肋了
- 第20章 驱动开发之LED应用程序
- C++基础——用C++实例理解UML类图
- MAC之tar解压与压缩打包命令
- 使用pm2启动Vue项目
- 4.spring 依赖注入的三种方式
- VPN(一):Connecting to openvpn has failed
- Java随机数生产算法
- STL 关联容器 之set(无重复有序集合)
- JAVA学习-序号2 HashSet初入门,hashCode和equals方法
- eclipse中如何向开源中国(码云)上传代码
- HDU
- 开启新的篇章
- poj 2773