需要引入命名空间

xmlns:p="http://www.springframework.org/schema/p"

p 是什么含义

p 是 property 的缩写,为了简化bean的配置

    <!-- 普通的bean配置 -->
<bean id="xiaomingPerson" class="cn.zno.Person">
<property name="age" value="1"></property>
<property name="car" ref="benz"></property>
</bean>
<!-- 瘦身的bean配置 -->
<bean id="xiaomingP" class="cn.zno.Person" p:age="1" p:car-ref="benz" /> <bean id="benz" class="cn.zno.Car">
<property name="brand" value="benz"></property>
</bean>

完整项目

依赖

        <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

Spring Bean Configuration File [test.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-4.1.xsd"> <context:annotation-config /> <!-- 普通的bean配置 -->
<bean id="xiaomingPerson" class="cn.zno.Person">
<property name="age" value="1"></property>
<property name="car" ref="benz"></property>
</bean>
<!-- 瘦身的bean配置 -->
<bean id="xiaomingP" class="cn.zno.Person" p:age="1" p:car-ref="benz" /> <bean id="benz" class="cn.zno.Car">
<property name="brand" value="benz"></property>
</bean>
</beans>

java bean 文件

package cn.zno;

public class Person {

    private int age;
private Car car; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
} @Override
public String toString() {
return "Person [age=" + age + ", car=" + car + "]";
} } class Car {
private String brand; public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} @Override
public String toString() {
return "Car [brand=" + brand + "]";
}
}

测试类 TestP.java

package ehcache;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.zno.Person; @ContextConfiguration(locations = { "classpath:test.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class TestP { @Autowired
private Person xiaomingPerson;
@Autowired
private Person xiaomingP; @Test
public void show() {
System.out.println(xiaomingPerson);
System.out.println(xiaomingP);
}
}

疑问

p:car 和 p:car-ref 用哪个?

用p:car-ref  

用p:car 

错误原因:

Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [cn.zno.Car] for property 'car': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:287)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:461)
... 45 more

不带-ref 的相当于value ,value不能转化bean类型

spring p 标签的更多相关文章

  1. <spring:message> 标签

    可以使用<spring:message>标签结合 ResourceBundleMessageSource 的功能,在网页上显示 messages.properties 中的文字讯息,例如在 ...

  2. dubbo源码—dubbo自定义spring xml标签

    dubbo为了和spring更好的集成,提供了一些xml配置标签,也就是自定义标签 spring自定义标签 spring自定义标签的方式如下: 设计配置属性和JavaBean 编写xsd文件,校验xm ...

  3. Spring component-scan 标签的实现

    在以前文章Spring自定义标签实现中,曾说过,在sprin g 配置文件中,除了be an beans import 常用的标签意外,其他的标签都是遵循Spring 自定义标签的扩展机制进行实现功能 ...

  4. spring boot+freemarker+spring security标签权限判断

    spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...

  5. spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />

    spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...

  6. spring基础---->spring自定义标签(一)

    Spring具有一个基于架构的扩展机制,可以使用xml文件定义和配置bean.本博客将介绍如何编写自定义XML bean的解析器,并用实例来加以说明.其实我一直相信 等你出现的时候我就知道是你. Sp ...

  7. spring自定义标签之 自我实现

     引言: 最近心情比较难以平静,周末的两天就跑出去散心了,西湖边上走走,看日落,还是不错的.回来博客上发现,在自定义标签上,最后一步实现忘记加上了.其实,人生的路程中,我们总是实现着自我的价值,让自己 ...

  8. spring自定义标签之 规范定义XSD

    引言: spring的配置文件中,一切的标签都是spring定义好的.<bean/>等等,有了定义的规范,才能让用户填写的正常可用.想写自定义标签,但首先需要了解XML Schema De ...

  9. [转]spring property标签中的 ref属性和ref 标签有什么不同

    spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...

  10. Spring 自定义标签配置

    前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Sp ...

随机推荐

  1. 从底层谈WebGIS 原理设计与实现(五):WebGIS中通过行列号来换算出多种瓦片的URL 之在线地图

    从底层谈WebGIS 原理设计与实现(五):WebGIS中通过行列号来换算出多种瓦片的URL 之在线地图 作者:naaoveGI…    文章来源:naaoveGIS    点击数:2063    更 ...

  2. 程序员教程-11章-Java程序设计

    自己是学java的,先看第十一章java吧. 列出章节目录,便于自己回忆内容. 11.1 Java语言概述 1 Java语言的特点 2 Java开发环境 11.2 Java语言基础 11.2.1 基本 ...

  3. iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 规范与部署

    沪江CCtalk视频地址:https://www.cctalk.com/v/15114923889450 规范与部署 懒人推动社会进步. 本篇中,我们会讲述三个知识点 定制书写规范 开发环境运行 如何 ...

  4. 7.Reverse Integer (INT; Overflow)

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:要注意溢出 ...

  5. Intersection of Two Linked Lists(LIST-2 POINTER)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  6. Appium原理初步--Android自动化测试学习历程

    章节:自动化基础篇——Appium原理初步(第七讲) 本期关键词: Appium.跨语言跨平台.Bootstrap 主要讲解内容及笔记: 一.what is appium 一种封装了uiautomat ...

  7. 转---tcp三次握手四次挥手syn fin......

    http://blog.chinaunix.net/uid-22312037-id-3575121.html转自 一.TCP报文格式        TCP/IP协议的详细信息参看<TCP/IP协 ...

  8. swift UITextfield 添加点击方法 - 简单实现

    1. 真正在任何系统上都有效的方法 1./// 城市选择 private lazy var cityTextfield:UITextField = { let tf = UITextField() t ...

  9. OC 线程操作2 - NSThread

        方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...

  10. 12-matlab简单读excel

    数据读入: clc; clear; AllNeedDate = xlsread('E:\a-建模\2018-5月校赛\2018年数学建模校内挑战赛题目\挑战赛A题\附件2:各城镇月度需求数据.xlsx ...