Spring的依赖注入方法
文章目录
- Spring中依赖注入的方法
- 基于构造方法的注入
- 根据索引赋值
- 根据所属类型传值
- 根据所属类型传值(不推荐)
- 基于setter注入
- 基于接口的注入(不常用,这里不说明了)
- 拓展方式注入
- P命名空间注入(基于setter方法注入)
- C命名空间注入(基于控制器注入)
Spring中依赖注入的方法
基于构造方法的注入
基于构造方法的注入:可以带参,也可以不带参,以下展示的是带参的
首先新建一个StudentConstructor类,里面带有一个带参的构造方法
package com.zhbit.pojo;public class StudentConstructor {private String name;private String sex;private String studentNo;private int age;public StudentConstructor(String name, String sex, String studentNo, int age) {this.name = name;this.sex = sex;this.studentNo = studentNo;this.age = age;}@Overridepublic String toString() {return "StudentConstructor{" +"name='" + name + '\'' +", sex='" + sex + '\'' +", studentNo='" + studentNo + '\'' +", age=" + age +'}';}
}
spring的构造器注入方法主要分为三种:1.根据索引赋值,索引都是以0开始 2.根据参数的名字传值 3.根据所属类型传值
下面对这三种方法进行简单分析。
根据索引赋值
配置constructor-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.xsd"><!--采用构造器的index-value方式注入 --><bean id="studentConstructor" class="com.zhbit.pojo.StudentConstructor"><constructor-arg index="0" value="小明"/><constructor-arg index="1" value="男"/><constructor-arg index="2" value="S001"/><constructor-arg index="3" value="23"/></bean></beans>
编写测试类MyConstructorBeanTest.class
import com.zhbit.pojo.StudentConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyConstructorBeanTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("constructor-beans.xml");StudentConstructor StudentConstructor = (StudentConstructor) context.getBean("studentConstructor");System.out.println(StudentConstructor);}
}
测试结果:
根据所属类型传值
配置constructor-beans.xml
<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.xsd"><!--<!–采用构造器的index-value方式注入 –><bean id="studentConstructor" class="com.zhbit.pojo.StudentConstructor"><constructor-arg index="0" value="小明"/><constructor-arg index="1" value="男"/><constructor-arg index="2" value="S001"/><constructor-arg index="3" value="23"/></bean>--><!-- 使用构造器的name-value方式注入 该办法通过反射实现注入--><bean id="studentConstructor" class="com.zhbit.pojo.StudentConstructor"><constructor-arg name="name" value="小红"/><constructor-arg name="age" value="18"/><constructor-arg name="sex" value="女"/><constructor-arg name="studentNo" value="160202103483"/></bean>
</beans>
编写测试类MyConstructorBeanTest.class
import com.zhbit.pojo.StudentConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyConstructorBeanTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("constructor-beans.xml");StudentConstructor StudentConstructor = (StudentConstructor) context.getBean("studentConstructor");System.out.println(StudentConstructor);}
}
测试结果
根据所属类型传值(不推荐)
不推荐的原因:一个类可以有好几个相同基本类型的变量,这样会混淆,这里就不做详细说明了。
基于setter注入
编写StudentSetter.class
package com.zhbit.pojo;public class StudentSetter {private String name;private String sex;private String studentNo;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getStudentNo() {return studentNo;}public void setStudentNo(String studentNo) {this.studentNo = studentNo;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "StudentSetter{" +"name='" + name + '\'' +", sex='" + sex + '\'' +", studentNo='" + studentNo + '\'' +", age=" + age +'}';}
}
这里面的每一个属性的set方法必须有,没有的话会在xml文件里面报错
接下来就是setter-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.xsd"><!--通过setter方法注入属性--><bean id="studentSetter" class="com.zhbit.pojo.StudentSetter" ><property name="name" value="小橙"/><property name="age" value="18"/><property name="sex" value="男"/><property name="studentNo" value="123"/></bean></beans>
编写测试类
import com.zhbit.pojo.StudentSetter;
import com.zhbit.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MySetterTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("setter-beans.xml");StudentSetter studentSetter = (StudentSetter) context.getBean("studentSetter");System.out.println(studentSetter);}
}
测试结果:
基于接口的注入(不常用,这里不说明了)
拓展方式注入
P命名空间注入(基于setter方法注入)
编写UserP.class类
package com.zhbit.pojo;public class UserP {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "UserP{" +"name='" + name + '\'' +", age=" + age +'}';}
}
编写userPBeans.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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--p命名空间注入,可以直接注入属性的值:property--><bean id="userP" class="com.zhbit.pojo.UserP" p:name="小明" p:age="18"></bean>
</beans>
其中
xmlns:p="http://www.springframework.org/schema/p"
是用来导入P-namespace命令
编写测试类MyUserPTest.class
import com.zhbit.pojo.UserP;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyUserPTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("userPBeans.xml");UserP userP = context.getBean("userP",UserP.class);System.out.println(userP.toString());}
}
测试结果:
C命名空间注入(基于控制器注入)
编写UserC.class
package com.zhbit.pojo;public class UserC {private String name;private int age;public UserC() {}public UserC(String name, int age) {this.name = name;this.age = age;}
}
编写userCBeans.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"xmlns:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--c命名空间注入,通过构造器注入:constructor-arg --><bean id="userC" class="com.zhbit.pojo.UserC" c:age="18" c:name="拼命三郎"/>
</beans>
其中
xmlns:c="http://www.springframework.org/schema/c"
为C命名空间的导入
编写测试类MyUserCTest.class
import com.zhbit.pojo.UserC;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyUserCTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("userCBeans.xml");UserC userC = context.getBean("userC", UserC.class);System.out.println(userC.toString());}}
测试结果:
注意点:p命名和c命名空间不能直接使用,需要导入xml约束,可以到spring官网查找
Spring的依赖注入方法相关推荐
- factorybean 代理类不能按照类型注入_《Spring入门经典》:使用Spring进行依赖注入
第二章:使用Spring进行依赖注入 重点:配置并使用Spring容器 使用不同类型的配置元数据来配置Spring容器 理解依赖解析 了解自动装配的优缺点 在容器中执行显式Bean查找 学习不同的Be ...
- 在ABAP里模拟实现Java Spring的依赖注入
Dependency Injection- 依赖注入,在Java Spring框架中有着广泛地应用.通过依赖注入,我们不必在应用代码里繁琐地初始化依赖的资源,非常方便. 那么ABAP能否从语言层面上也 ...
- Spring Setter依赖注入示例
学习如何编写Spring Setter依赖注入示例 . Setter注入是Spring依赖注入的一种 . Spring支持字段注入,Setter注入以及构造函数注入,以将依赖项注入Spring托管的b ...
- Spring字段依赖注入示例
学习如何编写Spring Field Injection示例 . 字段注入是Spring框架 依赖注入的一种 . 在本教程中,我们将编写几个类,并看一看现场注入工程. 有关Spring依赖注入的更多信 ...
- 据说,80%的人没有真正理解了Spring的依赖注入
前言 提起Spring,大家肯定不陌生,它是每一个Java开发者绕不过去的坎.Spring 框架为基于 java 的企业应用程序提供了一整套解决方案,方便开发人员在框架基础快速进行业务开发. 在官网中 ...
- Spring框架----Spring的依赖注入
1.spring的依赖注入的概念 依赖注入:dependency Injection IOC的作用:降低程序之间的依赖关系,但不是消除. 依赖关系的管理:以后都交给了spring来维护 在当前类中需要 ...
- Spring和依赖注入的价值
javaeye上看到有帖子,置疑spring和依赖注入的价值,回复内容整理如下: 依赖注入对设计有利,而spring则促进了依赖注入的使用. 如果业务处理类,它所使用的倚赖,都是依靠在这个类内部实现或 ...
- spring(一)依赖注入与 SPEL
Spring之依赖注入与 SPEL 一.控制反转与依赖注入 二.helloworld 三.构造注入 四.级联注入 五.单例与多例 六.工厂方法创建 Bean 七.包扫描管理 bean 八.SPEL与资 ...
- Spring实现依赖注入的几种方式
Spring实现依赖注入的几种方式 1.基于有参构造实现 <bean id="user" class="com.ccu.twj"><const ...
最新文章
- API pytorch tensorflow
- 零基础入门学习Python22-递归2 斐波那契数列和汉诺塔
- 卷积:kernel size/padding/stride
- SPOJ LCS Longest Common Substring
- win7环境变量设置
- 玩纸牌游戏计算机教案,小班数学好玩的扑克牌教案
- MATLAB编程练习题
- 第二、三章:信息系统项目管理基础与立项管理-章节真题
- c#中将WM_CLOSE消息发送到没有窗口的进程的方法
- mds算法python函数_分享python mds,sha256加密算法,c#对应sha256加密算法
- xamarin误删vEthernet(internal Ethernet Port Windows Phone Emulator) 网络设备的处理。
- 如何安装一个优秀的BUG管理平台(转)
- html中li整体变色,JS+CSS实现鼠标经过div(li)背景变色
- excel或wps查找文本字符串子串或拆分字符串公式
- phpspider 简单用法和学习,分类一对多爬取数据
- C#读取Excel文件(*.xls)|*.xls(2种方法)
- android 日历翻页动画,Android开源库合集:轻松实现Android动态,炫目:日历效果...
- Java技术栈学习路线
- python nlp文本摘要实现_用TextRank算法实现自动文本摘要
- Hot Water Pipe
热门文章
- dva model里面的effects函数可以调用effects函数
- 推荐系统算法--ItemCF--MF(ALS)--FF
- 安卓7.0核心破解示列
- SQL轻松入门(1):增删改与简单查询
- WEB超大文件上传与下载
- 关于微信公众号调起支付
- nginx配置访问本地静态资源
- 错误接受率 (FAR), 错误拒绝率(FRR), 等错误率(EER)
- win7无线局域网_存储卡具备WiFi有多方便?东芝 FlashAir 无线存储卡上手体验
- STM32Cube和HAL库使用初体验-第5季第2部分-朱有鹏-专题视频课程