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 ...
随机推荐
- VS·调试过程中某个操作导致调试突然退出之解决方案
阅文时长 | 0.11分钟 字数统计 | 232字符 主要内容 | 1.引言&背景 2.声明与参考资料 『VS·调试过程中某个操作导致调试突然退出之解决方案』 编写人 | SCscHero 编 ...
- 马哥Linux SysAdmin学习笔记(二)
Linux网络属性管理: 局域网:以太网,令牌环网 Ethernet:CSMA/CD 冲突域 广播域 MAC:media access control地址 48bit: 24bits 24bits ...
- SPEC 2000 整形和浮点性能测试结果是各项基准程序得分的几何平均值,几何平均值是 n 个数连乘之 后再开 n 次根号
SPEC 2000 能够生成多种格式的测试结果报表,包括 asc,ps,raw,pdf,html 等格式,报 表所在目录为/home/sepc2000all/result. 整形和浮点性能测试结果是 ...
- 云计算OpenStack---云计算、大数据、人工智能(14)
一.互联网行业及云计算 在互联网时代,技术是推动社会发展的驱动,云计算则是一个包罗万象的技术栈集合,通过网络提供IAAS.PAAS.SAAS等资源,涵盖从数据中心底层的硬件设置到最上层客户的应用.给我 ...
- shell基础之bus实战(if 练习)
题目: 一条公交路线共30站,乘客上车需支付1元,之后每超过5站支付0.5元:最好可以实现"余额不足的提醒"和"逃票,漏刷卡的显示". 注意:需下载bc计算器 ...
- Python-名片管理器
# 需要完成的基本功能: # 添加名片 # 删除名片 # 修改名片 # 查询名片 # 退出系统 # 程序运行后,除非选择退出系统,否则重复执行功能 list_info = [] # 创建一个空列表 # ...
- 排查利器:Tcpdump抓包 & Wireshark解析
在工作这一块,免不了和其他开发人员打交道.比如,和其他部门 or 公司联调,甚至是和自己部门的人联调的时候.这时候,对接问题就很容易暴露出来,特别是Tcp/Udp会话的时候,很容易就会呈现出公说公有理 ...
- CAP理论之思考
分布式系统的最大难点就是各个节点如何保持一致.最近我在工作中就遇到这样的问题,不同节点之间,彼此通过API,进行通信,交互数据,但有些服务节点存在延迟等问题,导致我看到的并不是实时的数据,以及系统更新 ...
- GO学习-(39) 优雅地关机或重启
优雅地关机或重启 我们编写的Web项目部署之后,经常会因为需要进行配置变更或功能迭代而重启服务,单纯的kill -9 pid的方式会强制关闭进程,这样就会导致服务端当前正在处理的请求失败,那有没有更优 ...
- 代码生成codegen
代码生成codegen 该模块提供了从SymPy表达式生成直接可编译代码的功能.该codegen功能是SymPy中代码生成功能的用户界面.下面为可能希望直接使用框架的高级用户提供了一些实现细节. 注意 ...