Spring(四)--bean的属性赋值
bean的属性赋值
1.需要的实体类
2.需要的配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置Grade对应的bean-->
<bean id="grade" class="com.xdf.bean.Grade" c:id="2" c:gradeName="2年级">
<!--01.设值注入 推荐使用 便于阅读
<property name="id" value="1"/>
<property name="gradeName" value="一年级"/>
02. p命名空间注入
p:id="1" p:gradeName="一年级"
03.通过构造方法赋值 必须有对应的构造方法
001.构造方法的参数下标来赋值
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="一年级"/>
002.构造方法的参数名称来赋值
<constructor-arg name="id" value="1"/>
<constructor-arg name="gradeName" value="一年级"/>
003.按照默认顺序
<constructor-arg value="一年级"/>
<constructor-arg value="1"/>
04.使用c命名空间
-->
</bean> <!--配置 Student对应的bean-->
<bean id="student" class="com.xdf.bean.Student">
<property name="name" value="小黑"/> <!--普通属性-->
<property name="grade" ref="grade"/><!--域属性-->
<property name="names">
<array> <!--数组-->
<value>小黑1</value>
<value>小黑2</value>
</array>
</property>
<property name="list">
<list><!--list集合-->
<value>小黑1</value>
<value>小黑2</value>
</list>
</property>
<property name="set">
<set><!--set集合-->
<value>小黑1</value>
<value>小黑2</value>
</set>
</property>
<property name="map">
<map><!--map集合-->
<entry key="id" value="1"/>
<entry key="name" value="小白"/>
<entry key="address" value="海淀区"/>
</map>
</property>
<property name="properties">
<props><!--properties属性-->
<prop key="id">1</prop>
<prop key="name">小白</prop>
<prop key="address">海淀区</prop>
</props>
</property>
</bean>
</beans>
3.测试类
public class PropertyDemo { public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Grade grade= context.getBean("grade", Grade.class);
System.out.println(grade);
}
}
未完待续!!!我们一起努力!
Spring(四)--bean的属性赋值的更多相关文章
- spring(四):spring中给bean的属性赋值
spring中给bean的属性赋值 xml文件properties标签设置 <bean id="student" class="com.enjoy.study.ca ...
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 【Spring注解驱动开发】如何使用@Value注解为bean的属性赋值,我们一起吊打面试官!
写在前面 在之前的文章中,我们探讨了如何向Spring的IOC容器中注册bean组件,讲解了有关bean组件的生命周期的知识.今天,我们就来一起聊聊@Value注解的用法. 项目工程源码已经提交到Gi ...
- 【spring源码系列】之【Bean的属性赋值】
每次进入源码的世界,就像完成一场奇妙的旅行! 1. 属性赋值概述 上一篇讲述了bean实例化中的创建实例过程,实例化后就需要对类中的属性进行依赖注入操作,本篇将重点分析属性赋值相关流程.其中属性赋值, ...
- Spring(四)Bean注入方试
一.构造方法注入 定义:通过构造函数来完成依赖关系的设定 优缺点: 在构造对象的同时,完成依赖关系的建立 如果关联的对象很多,那和不得不在构造方法上加入过多的参数 基中有index:如果指定索引从0开 ...
- Spring注解开发系列Ⅳ --- 属性赋值
在Spring框架中,属性的注入我们有多种方式,我们可以通过构造方法注入,可以通过set方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List集合.map集合.P ...
- spring 引用Bean的属性值
引用Bean的属性值 从Spring3.0开始,可以通过#{beanName.beanProp}的方式方便地引用另一个bean的属性值1.不需要使用PropertyPlaceholderConfigu ...
- Spring中bean作用域属性scope
关键字: spring中属性scope的prototype是什么意思 默认情况下,从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spri ...
- 03 Spring框架 bean的属性以及bean前处理和bean后处理
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们给出了三个小demo,具体的流程是这样的: 1.首先在aplicationContext.xml中添加< ...
随机推荐
- centos7排查swap占用过高
使用free -h 查看发现服务器在可用内存还有91G的情况下,使用Swap分区空间 查看具体是哪进程在占用Swap分区 ###for i in $( cd /proc;ls |grep " ...
- android 小音频频繁播放
android中多媒体文件(音乐和视频)的播放是用MediaPlayer方式是大家比较熟悉的,但是现在要来说一下另外一种音乐文件播放的方式SoundPool,相比较而言,用MediaPlayer来播放 ...
- Spark译文(三)
Structured Streaming Programming Guide(结构化流编程指南) Overview(概貌) ·Structured Streaming是一种基于Spark SQL引擎的 ...
- ZooKeeper java例子解读
转载链接:https://blog.csdn.net/liyiming2017/article/details/83276706 需求理解我们先回顾一下例子的需求,此客户端有如下四个需求: 1.它接收 ...
- 在linux 系统下 使用命令行对mysql 数据库进行操作
1.连接mysql root@test:/home# mysql -uroot -proot <uroot是用户名,proot是密码> 2.查询所有的库 mysql> show da ...
- 顺序表栈C语言实现
/* * SeqStack.h * * Created on: 2019年8月1日 * Author: Administrator */ #ifndef SEQSTACK_H_ #define SEQ ...
- linux 中文乱码解决办法
就是从数据库中取出来时,在存入linux的文件里时,在字符流时制定编码格式.代码如下: FileOutputStream fos=new FileOutputStream(new File(fileP ...
- if && grep
if 条件 then Command else Command fi 别忘了这个结尾 ——————— ...
- ffmpeg循环推流
ffmpeg循环推流 有时候需要轮播出一路直播 这个时候循环推流就比较方便了 ffmpeg -stream_loop - -re -i d:/Media/a.ts -vcodec h264 -acod ...
- Oracle9i的详细安装与卸载步骤(有图解)
Oracle9i的安装和卸载详解 本章将以Windows操作系统为例讲述Oracle9i数据库的安装 ...