看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标签的更多相关文章

  1. 基于Spring开发——自定义标签及其解析

    1. XML Schema 1.1 最简单的标签 一个最简单的标签,形式如: <bf:head-routing key="1" value="1" to= ...

  2. Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签

    写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...

  3. Spring中bean标签的属性和值:

    Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...

  4. Spring——使用自定义标签

    文章内容参考了<Spring源码深度解析>一书.自己照着书中内容做了一遍,不懂的地方以及采坑的地方会在文中记录. 推荐一篇post,关于Spring配置文件的命名空间: https://w ...

  5. Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明

    Spring <context:component-scan>标签作用有很多,最基本就是 开启包扫描,可以使用@Component.@Service.@Component等注解: 今天要作 ...

  6. Spring表单标签

    虽然我们可以使用HTML原生的form表单标签来轻松的写出一个表单,其实我一直都是这样做的,但是使用Spring表单标签可以更方便我们完成例如:验证失败后表单数据的回填功能(虽然你可以使用EL+JST ...

  7. spring:使用<prop>标签为Java持久属性集注入值

    spring:使用<prop>标签为Java持久属性集注入值 使用 spring 提供的<prop>为Java持久属性集注入值,也就是向 java.util.Propertie ...

  8. Spring IoC 自定义标签解析

    前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 本篇文章主要介绍 Spring IoC 容 ...

  9. [Java] 解决spring的xml标签内不能自由增加说明的难题,方便调试、部署时进行批量屏蔽

    作者:zyl910 以往我们想在spring的xml配置文件中增加说明文本时,只能使用xml注释(<!-- 注释 -->).这对于"调试.部署时想批量屏蔽部分bean" ...

  10. Spring的自定义标签

    当Spring拿到一个元素时首先要做的是根据命名空间进行解析,如果是默认的命名空间,则使用parseDefaultElement方法进行元素解析,否则使用parseCustom Element方法进行 ...

随机推荐

  1. c#实现用SQL池(多线程),定时批量执行SQL语句

    在实际项目开发中,业务逻辑层的处理速度往往很快,特别是在开发Socket通信服务的时候,网络传输很快,但是一旦加上数据库操作,性能一落千丈,数据库操作的效率往往成为一个系统整体性能的瓶颈.面对这问题, ...

  2. c#的SortedList使用方法

    表示键/值对的集合,这些键和值按键排序并可按照键和索引访问. SortedList最合适对一列健/值对 进行排序,在排序时,是对键进行排序,SortedList 是 Hashtable 和 Array ...

  3. @RequestBody 处理类型 和 对象 和 json 相互转换

    1 @RequestBody 处理类型 在项目中经常看到controller 中有 @RequestBody 字样,他到底有什么作用? 一般使用表单提交数据时不需要使用@RequestBody 即可自 ...

  4. mysql-cluster 环境安装&配置

    一.mysql-cluster 的介绍: 1.说心里话mysql-cluster这货性能上是不行的,之前一个同事测试了来的结果是8个主机组成的mysql-cluster性能 上搞不过一个单机的mysq ...

  5. MATLAB(1)——基本调试方法(Debug)

    作者:桂. 时间:2017-02-28  07:06:30 链接:http://www.cnblogs.com/xingshansi/articles/6477185.html 声明:转载请注明出处, ...

  6. JavaScript Interview Questions: Event Delegation and This

    David Posin helps you land that next programming position by understanding important JavaScript fund ...

  7. svnserve配置文件详析

    svnserve是SVN自带的一个轻型服务器,客户端通过使用以svn://或svn+ssh://为前缀的URL来访问svnserve服务 器,实现远程访问SVN版本库. svnserve可以通过配置文 ...

  8. man-pages项目包含文档的说明

    man-pages项目是由linux kernel维护的一个文档项目,但是,该项目中只是包含了部分常用文档,还有大量gnu常用系统工具的manpanges没有包含,但一些在线网站提供一个详细版本的ma ...

  9. C#判断窗体是否存在重复打开

    foreach (Form f in Application.OpenForms) { f.Name //是打开窗体的Text //以下判断....... } Form2 F2 ; if(F2 == ...

  10. ntp服务的细节全解析

    在linux里设置NTP服务并不难,但是NTP本身确是一个很复杂的协议. 你都了解细节么? 1. 时间和时区 date命令可显示时间与市区 [root@T_PV1_DB ~]# date Tue Fe ...