当前位置: 首页 > news >正文

Spring使用P命名空间实现注入数值信息-----Spring框架

<?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"><!--       第一步,在Spring的头部添加p命名空间,这就引入了P命名空间了,可以使用P命名空间了-->
<!--    p冒号加属性值就可以赋值使用了--><bean id="Dog" class="com.powernode.spring6.Bean.Dog" p:age="3" p:name="小花" p:birth-ref="Date"/><bean id="Date" class="java.util.Date"/>
</beans>
<?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"><!--       第一步,在Spring的头部添加p命名空间,这就引入了P命名空间了,可以使用P命名空间了-->
<!--    p冒号加属性值就可以赋值使用了--><bean id="Dog" class="com.powernode.spring6.Bean.Dog" p:age="3" p:name="小花" p:birth-ref="Date"/><bean id="Date" class="java.util.Date"/>
</beans>
package com.powernode.spring6.Bean;import java.util.Date;public class Dog
{private String name;private int age;private Date birth;@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +", birth=" + birth +'}';}//p空间注入底层还是set注入,只不过p命名空间注入可以让spring配置变得更简单,一旦set方法不存在就会报错了public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public void setBirth(Date birth) {this.birth = birth;}
}
package com.powernode.spring6.Bean;import java.util.Date;public class Dog
{private String name;private int age;private Date birth;@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +", birth=" + birth +'}';}//p空间注入底层还是set注入,只不过p命名空间注入可以让spring配置变得更简单,一旦set方法不存在就会报错了public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public void setBirth(Date birth) {this.birth = birth;}
}
    @Testpublic void TestSpecial(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");MathBean MathBean = applicationContext.getBean("MathBean",MathBean.class);System.out.println(MathBean);applicationContext = new ClassPathXmlApplicationContext("Spring-p.xml");System.out.println(applicationContext.getBean("Dog",Dog.class));}
@Test
public void TestSpecial()
{ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");MathBean MathBean = applicationContext.getBean("MathBean",MathBean.class);System.out.println(MathBean);applicationContext = new ClassPathXmlApplicationContext("Spring-p.xml");System.out.println(applicationContext.getBean("Dog",Dog.class));
}
http://www.lryc.cn/news/109139.html

相关文章:

  • windows环境下安装RabbitMQ
  • Java源码规则引擎:jvs-rules决策流的自定义权限控制
  • Python-字符串的世界
  • 使用上 Spring 的事件机制
  • Linux安装QT
  • 如何用arduino uno主板播放自己想要的曲子。《我爱你中国》单片机版本。
  • redis入门2-命令
  • Typescript 枚举类型
  • docker小记-容器中启动映射端口号但访问不到
  • Java中的Map常见使用案例代码
  • 计算机视觉实验:图像处理综合-路沿检测
  • Linux环境下VS code的python与C++调试环境的安装
  • AlexNet卷积神经网络-笔记
  • 剑指 Offer 53 - I. !!在排序数组中查找数字 I (考查二分法)
  • RANSAC算法在Python中的实现与应用探索:线性拟合与平面拟合示例
  • PHP接口自动化测试框架实现
  • VLAN原理+配置
  • MongoDB文档-基础使用-在客户端(dos窗口)/可视化工具中使用MongoDB基础语句
  • “RISC-V成长日记” blog发布,第一个运行在RISC-V服务器上的blog?
  • gitlab配置webhook
  • 编译安装Linux内核实践与踩坑
  • 郑州https数字证书
  • 第125天:内网安全-隧道技术SMBICMP正反向连接防火墙出入规则上线
  • 性能测试怎么做?测试工具怎么选择?
  • 【Eclise配置Jdk环境】Jre环境常见配置错误问题
  • UM2080F32——32位SoC芯片
  • IDEA基础使用
  • [数据集][目标检测]遛狗不牵绳数据集VOC格式-1980张
  • 基于WebRTC升级的低延时直播
  • 数学知识(二)