Spring属性注入(set方式、构造函数方式、p名称空间、spel、复杂类型)
1、set注入方式
(1)注入的为值类型(八大数据类型)的数据
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="student" class="pers.zhb.domain.Student">
<property name="sname" value="zhai"></property>
<property name="snum" value="20200210"></property>
<property name="sex" value="nan"></property>
</bean>
</beans>
也可以以子标签的方式配置:
<property name="">
<value>123</value>
</property>
测试类:
public class Test {
public void test1(){
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
Student student=(Student)applicationContext.getBean("student");
System.out.println(student);
}
public static void main(String[] args){
Test test=new Test();
test.test1();
}
}

(2)注入的为引用数据类型的数据:
Student对象:
package pers.zhb.domain;
public class Student {
private String snum;
private String sname;
private String sex;
private Course course;
public Course getCourse() {
return course;
} public void setCourse(Course course) {
this.course = course;
} public Student(){
System.out.println("Student对象创建了!");
}
public String getSnum() {
return snum;
} public void setSnum(String snum) {
this.snum = snum;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"snum='" + snum + '\'' +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
", course=" + course +
'}';
} public void destory(){
System.out.println("我是销毁的方法!");
} public void init(){
System.out.println("我是初始化的方法!");
} }
Course对象:
public class Course {
private String cname;
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="student" class="pers.zhb.domain.Student">
<property name="sname" value="zhai"></property>
<property name="snum" value="20200210"></property>
<property name="sex" value="nan"></property>
<property name="course" ref="course"></property>
</bean>
<bean name="course" class="pers.zhb.domain.Course">
<property name="cname" value="算法设计与分析"></property>
</bean>
</beans>
测试类:
public class Test {
public void test1(){
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
Student student=(Student)applicationContext.getBean("student");
System.out.println(student);
}
public static void main(String[] args){
Test test=new Test();
test.test1();
}
}

2、构造函数注入
创建Student对象:
package pers.zhb.domain;
public class Student {
private String snum;
private String sname;
private String sex;
private Course course;
public Student(String snum, String sname, String sex, Course course) {
this.snum = snum;
this.sname = sname;
this.sex = sex;
this.course = course;
}
public Course getCourse() {
return course;
} public void setCourse(Course course) {
this.course = course;
} public Student(){
System.out.println("Student对象创建了!");
}
public String getSnum() {
return snum;
} public void setSnum(String snum) {
this.snum = snum;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"snum='" + snum + '\'' +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
", course=" + course +
'}';
} public void destory(){
System.out.println("我是销毁的方法!");
} public void init(){
System.out.println("我是初始化的方法!");
} }
需要在Student类中创建一个构造函数。
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="student" class="pers.zhb.domain.Student">
<constructor-arg name="sname" value="zhai"></constructor-arg>
<constructor-arg name="snum" value="123456"></constructor-arg>
<constructor-arg name="sex" value="nan"></constructor-arg>
<constructor-arg name="course" ref="course"></constructor-arg>
</bean>
<bean name="course" class="pers.zhb.domain.Course">
<property name="cname" value="算法设计与分析"></property>
</bean>
</beans>
配置文件中的配置需要和构造函数中属性的配置一一对应。

其他属性:
index:指定构造函数的参数的索引
type:指定构造函数参数的类型
3、p名称空间方式(对set方式注入进行简化)
(1)导入p名称空间(前提):
xmlns:p="http://www.springframework.org/schema/p"
(2)配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="student" class="pers.zhb.domain.Student" p:sname="zhai"
p:sex="nan" p:snum="1234" p:course-ref="course">
</bean>
<bean name="course" class="pers.zhb.domain.Course">
<property name="cname" value="电子技术"></property>
</bean>
</beans>
测试:

4、spel(Spring表达式语言)注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="student" class="pers.zhb.domain.Student">
<property name="sname" value="zhang"></property>
<property name="snum" value="11111"></property>
<property name="sex" value="nv"></property>
</bean>
<bean name="course" class="pers.zhb.domain.Course">
<property name="cname" value="电子技术"></property>
</bean>
<bean name="student1" class="pers.zhb.domain.Student">
<property name="sname" value="#{student.sname}"></property>
<property name="snum" value="#{student.snum}"></property>
<property name="sex" value="#{student.sex}"></property>
<property name="course" ref="course"></property>
</bean>
</beans>
可以直接去取已经创建的对象的值。
5、复杂类型注入
(1)数组类型:
添加一个值:
<bean name="arr" class="pers.zhb.domain.CollectionBean">
<property name="arr" value="加油!!"></property>
</bean>
添加多个值:
<bean name="arr" class="pers.zhb.domain.CollectionBean">
<property name="arr">
<array>
<value>你好</value>
<value>加油</value>
<value>努力</value>
</array>
</property>
</bean>
值加对象:
<bean name="arr" class="pers.zhb.domain.CollectionBean">
<property name="arr">
<array>
<value>你好</value>
<value>加油</value>
<value>努力</value>
<ref bean="student"></ref>
</array>
</property>
</bean>
第四个值为一个Student对象。
(2)List类型:
<bean name="cb" class="pers.zhb.domain.CollectionBean">
<property name="li">
<list>
<value>你好</value>
<value>加油</value>
<value>努力</value>
<ref bean="student"></ref>
</list>
</property>
</bean>
(3)map类型:
<bean name="map" class="pers.zhb.domain.CollectionBean">
<property name="map">
<map>
<entry key="key" value="value"></entry>
<entry key="student" value-ref="student"></entry>
</map>
</property>
</bean>

(4)properties类型:
<bean name="prop" class="pers.zhb.domain.CollectionBean">
<property name="properties">
<props>
<prop key="key">key</prop>
</props>
</property>
</bean>

Spring属性注入(set方式、构造函数方式、p名称空间、spel、复杂类型)的更多相关文章
- 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式
Spring的属性注入: 构造方法的属性注入 set方法的属性注入
- Spring中属性注入的几种方式以及复杂属性的注入
在Spring框架中,属性的注入我们有多种方式,我们可以通过构造方法注入,可以通过set方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List集合.map集合.P ...
- Spring中属性注入的几种方式以及复杂属性的注入详解
在spring框架中,属性的注入我们有多种方式,我们可以通过set方法注入,可以通过构造方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List.Map.Prope ...
- Spring静态注入的三种方式
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/chen1403876161/article/details/53644024Spring静态注入的三 ...
- Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用
Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...
- Spring 属性注入(二)BeanWrapper 结构
Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...
- Spring 属性注入(三)AbstractNestablePropertyAccessor
Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...
- Spring 属性注入(四)属性键值对 - PropertyValue
Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...
- SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式
于java发育.一类程序猿必须依靠类的其他方法,它是通常new依赖类的方法,然后调用类的实例,这样的发展问题new良好的班统一管理的例子.spring提出了依赖注入的思想,即依赖类不由程 ...
随机推荐
- Linux系统时间同步方法
在Windwos中,系统时间的设置很简单,界面操作,通俗易懂,而且设置后,重启,关机都没关系.系统时间会自动保存在BIOS时钟里面,启动计算机的时候,系统会自动在BIOS里面取硬件时间,以保证时间的不 ...
- 使用服务进程启动asp.net core程序
昨天在PuTTY中启动了测试网站,因为时间太晚,看到效果,立马关机睡觉,刚上床又想看看手机端访问效果,结果一打开,发现nginx找不到页面,意识到应该是退出PuTTY导致进程结束,于是今天赶紧查了解决 ...
- GitHub 热点速览 Vol.35:Let's Go,Rust 大放异彩
摘要:语言之争,一直存在于各类社群,不论是单个编程语言的交流群,亦或是 NoSQL.云开发等技术群,总能看到"要不要换 Go"."Rust 比 C++ 更强"的 ...
- js中map()、some()、every()、filter()的区别
js的Array对象有map.some.every.filter几个方法都能对数组中的每个对象进行处理,但是他们之间的功能又各有差别,所以在记下. map():通过指定函数处理数组的每个元素,并返回处 ...
- [BUUOJ记录] [ZJCTF 2019]NiZhuanSiWei
考察PHP伪协议+反序列化,都比较简单 进入题目给出源码: <?php $text = $_GET["text"]; $file = $_GET["file&quo ...
- 2020重新出发,NOSQL,Redis主从复制
Redis主从复制 尽管 Redis 的性能很好,但是有时候依旧满足不了应用的需要,比如过多的用户进入主页,导致 Redis 被频繁访问,此时就存在大量的读操作. 对于一些热门网站的某个时刻(比如促销 ...
- Linq 下的扩展方法太少了,您期待的 MoreLinq 来啦
一:背景 1. 讲故事 前几天看同事在用 linq 给内存中的两个 model 做左连接,用过的朋友都知道,你一定少不了一个叫做 DefaultIfEmpty 函数,这玩意吧,本来很流畅的 from. ...
- Maven 仓库镜像设置
<mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>al ...
- 递归方式---通过子级id,获取子级和父级Name
#region 递归--返回 父级|子级 名称 #region --返回 父级|子级 名称 public string RetrurnTypeNames(string TypeId) { String ...
- WebApi OAuth2身份认证
一.什么是OAuth OAuth是一个关于授权(Authorization)的开放网络标准,目前的版本是2.0版.注意是Authorization(授权),而不是Authentication(认证). ...