Spring学习日记02_IOC_属性注入_其他类型属性
ICO操作Bean管理(xml注入其它类型属性)
- 字面量
- null值
<property name="address">
<null></null>
</property>
- 属性值包含特殊符号
<!--
1.把<>进行转义<;>
2.把带特殊符号内容写到<![CDATA[...]]>
-->
<property name="address">
<value><![CDATA[<<南京>>]]></value>
</property>
- 注入属性-外部bean
- 创建两个类:service类和dao类
- 在service调用dao里面热点方法
service层
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add...");
userDao.update();
}
}
Dao层
public interface UserDao {
public void update();
}
public class UserDapImpl implements UserDao {
public void update() {
System.out.println("Dao update...");
}
}
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">
<!--1.service和dao对象创建-->
<bean id="userService" class="Spring.Ioc.Day04.service.UserService">
<!--注入userDao对象
name属性:类里面属性名称
ref属性:创建userDao对象bean标签id值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="Spring.Ioc.Day04.dao.UserDapImpl"></bean>
</beans>
test测试
public class testBean {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
UserService userService = context.getBean("userService",UserService.class);
userService.add();
}
}
- 注入属性-内部bean
- 一对多关系:部门和员工
- 一个部门有多个员工,一个员工属于一个部门
Dept类
public class Dept {
private String dname;
public void setDname(String name) {
this.dname = name;
}
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
}
Emp类
public class Emp {
private String ename;
private String gender;
//员工属于一个部门,使用对象形式表示
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void add(){
System.out.println(ename+"::"+gender+"::"+dept);
}
}
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">
<!--内部bean-->
<bean id="emp" class="Spring.Ioc.Day04.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="aaa"></property>
<property name="gender" value="man"></property>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="Spring.Ioc.Day04.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
</beans>
test类
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Emp emp = context.getBean("emp",Emp.class);
emp.add();
}
- 注入属性-级联赋值
- 第一种写法
xml
<bean id="emp" class="Spring.Ioc.Day04.bean.Emp">
<property name="ename" value="bbb"></property>
<property name="gender" value="man"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="Spring.Ioc.Day04.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>
- 第二种写法
- 需要提供属性的get方法
Emp类
//Dept属性增加get方法
public Dept getDept() {
return dept;
}
xml
<bean id="emp" class="Spring.Ioc.Day04.bean.Emp">
<property name="ename" value="bbb"></property>
<property name="gender" value="man"></property>
<!--级联赋值-->
<property name="dept.dname" value="技术部"></property>
</bean>
Spring学习日记02_IOC_属性注入_其他类型属性的更多相关文章
- Spring学习日记03_IOC_属性注入_集合类型属性
Ioc操作Bean管理(xml注入集合属性) 注入数组类型属性 注入List集合类型属性 注入Map集合类型属性 Stu类 public class Stu { //1. 数组类型属性 private ...
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- Spring 属性注入(四)属性键值对 - PropertyValue
Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...
- Spring 学习笔记 4. 尚硅谷_佟刚_Spring_属性配置细节
1,字面值 •字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入. •基本数据类型及其封装类.String 等类型都可以采取字面值注入的方式 •若字 ...
- Spring学习日记01_IOC_xml的三种注入方式
什么是IOC 控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理 使用IOC目的:为了耦合度降低 做入门案例就是IOC实现 IOC底层原理 xml解析 工厂模式 反射 原始方式 cla ...
- Spring 学习笔记 3. 尚硅谷_佟刚_Spring_配置 Bean
1,bean 的配置 <bean id="helloWorld" class="com.yfy.HelloWorld"> <property ...
- Spring学习笔记1—依赖注入(构造器注入、set注入和注解注入)
什么是依赖注入 在以前的java开发中,某个类中需要依赖其它类的方法时,通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,spring提出了依赖注入的思想,即依赖类不由程序员 ...
- Spring 学习笔记 数据绑定,校验,BeanWrapper 与属性编辑器
Spring 数据绑定,校验,BeanWrapper,与属性编辑器 Data Binding 数据绑定(Data binding)非常有用,它可以动态把用户输入与应用程序的域模型(或者你用于处理用户输 ...
- Spring学习笔记 5. 尚硅谷_佟刚_Spring_自动装配
1,回顾以前的做法 一个人有姓名,有住址,有一辆车.其中住址和车也是一个类,这种情况下不用自动装配是十分容易实现的 (1)Person类 package com.zsq; public class P ...
随机推荐
- (原创)高DPI适配经验系列:(三)字体与字号、缩放锚点
一.前言 程序最基本的元素,就是文本,也就是字体.如果程序未进行高DPI的适配,最直观的感受便是字体的模糊.所以本篇便来说一下高DPI适配中的字体问题. 高DPI的适配,简单来说便是便是根据不同的DP ...
- Sed常用功能个人整理
Sed常用功能个人整理 AsdilFibrizo关注 2019.06.24 10:23:41字数 240阅读 15 Sed对1G以下的数据效率很高这里介绍一些个人在工作中遇到的sed问题 1.查找字段 ...
- stm32之ADC应用实例(单通道、多通道、基于DMA)-转载精华帖,最后一部分的代码是精华
硬件:STM32F103VCT6 开发工具:Keil uVision4 下载调试工具:ARM仿真器网上资料很多,这里做一个详细的整合.(也不是很详细,但很通俗).所用的芯片内嵌3个12位的 ...
- centos更换损坏硬盘uuid改变导致系统不能正常启动处理
1.因为挂载磁盘uuid错误导致,而系统在启动的时候,会读取fstab文件.来加载预设的硬盘到指定的分区目录,但读取到需要挂载的UUID的磁盘不存在,中断文件的读取,然后提示文件系统错误,不能解析UU ...
- 10.16-17 mailq&mail:显示邮件传输队列&发送邮件
mailq命令 是mail queue(邮件队列)的缩写,它会显示待发送的邮件队列,显示的条目包括邮件队列ID.邮件大小.加入队列时间.邮件发送者和接受者.如果邮件进行最后一次尝试后还没有将邮件投递出 ...
- Spring AOP 用法浅谈(Day_18)
当你这一天没有在进步,那你就是在退步! [简述] Aspect Oriented Programming :面向切面编程 所谓面向切面编程,是一种通过预编译和运行期动态代理的方式实现在不修改源代码的情 ...
- JavaScript正则表达式(深度)(Day_14)
忘不掉的是回忆,继续的是生活,错过的,就当是路过. 简介 正则表达式是用于匹配字符串中字符组合的模式.在 JavaScript中,正则表达式也是对象. 这些模式被用于 RegExp 的 exec 和 ...
- wxPython开发之密码管理程序
不想记密码?密码全设置成一样担心安全?用别人程序担心密码泄露?看完本博客,开发一个属于自己的密码管理程序吧 我们用到的是python的wxPython界面库包 先来看下成果界面:简洁主题明确 要想开 ...
- java并发编程工具类JUC第二篇:ArrayBlockingQueue
类ArrayBlockingQueue是BlockingQueue接口的实现类,它是有界的阻塞队列,内部使用数组存储队列元素.这里的"有界"是指存储容量存在上限,不能无限存储元素. ...
- urllib2连接超时设置
#urllib2设置超时 #获取网页的源码 def getHtml(url,i): if i > 2: return try: req = urllib2.Request(url) time.s ...