Spring的p标签
看Spring in action的时候看过p标签,可惜这东西不用就忘。
p标签是为了简化setter的注入而引入的。
用法:
p:属性 = "{值}"
p:属性-ref = "{引用bean的id}"
示例Bean:
package o3.bean;
import java.util.Date;
public class Person {
private String name;
private int age;
private Date date;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public Date getDate(){
return date;
}
public void setDate(Date date){
this.date = date;
}
@Override public String toString(){
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", date=" + date +
'}';
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="o3.bean"/> <bean id="person" class="o3.bean.Person">
<property name="name" value="Kute"/>
<property name="age" value="22"/>
<property name="date" ref="date"/>
</bean>
<bean id="date" class="java.util.Date" autowire="constructor"/> <bean id="person2" class="o3.bean.Person" p:name="Bill" p:age="23" p:date-ref="date" />
</beans>
测试代码:
package o3; import o3.bean.Person;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class P {
private ApplicationContext ac;
@Before
public void setUp(){
ac=new ClassPathXmlApplicationContext("classpath:applicationContextPerson.xml");
} @Test
public void run1(){
Person person = ac.getBean("person", Person.class);
Person person2 = ac.getBean("person2", Person.class);
System.out.println(person);
System.out.println(person2);
} }
测试结果:
Person{name='Kute', age=22, date=Fri May 27 10:13:27 CST 2016}
Person{name='Bill', age=23, date=Fri May 27 10:13:27 CST 2016}
Spring的p标签的更多相关文章
- 基于Spring开发——自定义标签及其解析
1. XML Schema 1.1 最简单的标签 一个最简单的标签,形式如: <bf:head-routing key="1" value="1" to= ...
- Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签
写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...
- Spring中bean标签的属性和值:
Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...
- Spring——使用自定义标签
文章内容参考了<Spring源码深度解析>一书.自己照着书中内容做了一遍,不懂的地方以及采坑的地方会在文中记录. 推荐一篇post,关于Spring配置文件的命名空间: https://w ...
- Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明
Spring <context:component-scan>标签作用有很多,最基本就是 开启包扫描,可以使用@Component.@Service.@Component等注解: 今天要作 ...
- Spring表单标签
虽然我们可以使用HTML原生的form表单标签来轻松的写出一个表单,其实我一直都是这样做的,但是使用Spring表单标签可以更方便我们完成例如:验证失败后表单数据的回填功能(虽然你可以使用EL+JST ...
- spring:使用<prop>标签为Java持久属性集注入值
spring:使用<prop>标签为Java持久属性集注入值 使用 spring 提供的<prop>为Java持久属性集注入值,也就是向 java.util.Propertie ...
- Spring IoC 自定义标签解析
前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 本篇文章主要介绍 Spring IoC 容 ...
- [Java] 解决spring的xml标签内不能自由增加说明的难题,方便调试、部署时进行批量屏蔽
作者:zyl910 以往我们想在spring的xml配置文件中增加说明文本时,只能使用xml注释(<!-- 注释 -->).这对于"调试.部署时想批量屏蔽部分bean" ...
- Spring的自定义标签
当Spring拿到一个元素时首先要做的是根据命名空间进行解析,如果是默认的命名空间,则使用parseDefaultElement方法进行元素解析,否则使用parseCustom Element方法进行 ...
随机推荐
- spring sessionFactory 属性配置详解,applicationContext中各种属性详解
1.Bean的id为sessionFactory,对应的类为AnnotationSessionFactory,即采用注解的形式实现hibernate. 2.hibernateProperties,配置 ...
- 腾讯 OCR 情况
OCR技术之检测篇 https://cloud.tencent.com/developer/article/1101342 OCR技术之数据篇 https://cloud.tencent.com/de ...
- mysql - 语法复习与学习
//本月的第一天,最后一天 $start=date('Y-m-01', strtotime(date("Y-m-d"))); echo date('Y-m-d', strtotim ...
- OpenGl学习 SelectObject函数
SelectObject 函数功能:该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象. 函数原型:HGDIOBJ SelectObject(HDC hdc, HGDIOB ...
- μCOS-II系统之事件(event)的使用规则及Semaphore实例
**************************************************************************************************** ...
- Linux下的tree命令 --Linux下文件夹树查看
Linux下的tree命令 --Linux下文件夹树查看 有时我们须要生成文件夹树结构,能够使用的有ls -R,可是实际效果并不好 这时须要用到tree命令,可是大部分Linux系统是默认不安装该命令 ...
- 强者联盟——Python语言结合Spark框架
引言:Spark由AMPLab实验室开发,其本质是基于内存的高速迭代框架,"迭代"是机器学习最大的特点,因此很适合做机器学习. 得益于在数据科学中强大的表现,Python语言的粉丝 ...
- Objective-C之成魔之路【13-预处理程序】
郝萌主倾心贡献.尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠.支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 预处理提供 ...
- GCC实现多文件编译,静态库,动态库
一 代码 //add.h int add(int a, int b); //add.c int add(int a, int b) { return a+b; } //main.c #incl ...
- MYSQL COST optimizer
http://blog.chinaunix.net/uid-26896862-id-3326400.html https://www.slideshare.net/olavsa/mysql-optim ...