需要引入命名空间

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. HttpClientUtil 工具类

    /* * * * FileName: s.java * * Description:TODO(用一句话描述该文件做什么) * * Created: jiangzhanghong 2017年11月14日 ...

  2. js json转对象

    使用eval() 读取 for (var i=0;i< response.length; i++) { //alert(response[i].username) html=html+" ...

  3. cmd 获取 拖拽文件名

    1. @echo off & setlocal enableDelayedExpansion set a= set /p a=Please drag your txt file for spl ...

  4. Python合并列表,append()、extend()、+、+=

    在实际应用中涉及到了列表合并的问题. 在应用append()时,发现列表是以一个元素的形式追加到列表上的,最后查询后用的是extend()方法,下面是区别   1.append()  向列表尾部追加一 ...

  5. Easyui-datagrid显示时间的格式化代码

    {field: 'Time', title: '时间', formatter: function (value, row, index) { var date = new Date(value); v ...

  6. hover

    hover - Bing dictionary US[ˈhɒvə(r)] v.盘旋:徘徊:犹豫:巡弋 网络翱翔:悬停:盘旋于

  7. asp.net后台解析JSON,并将值赋给对象

    示例代码如下: using System; using System.Collections.Generic; using System.Web.Script.Serialization; publi ...

  8. discuz回贴通知插件实现-配置邮件服务器

    添加smtp服务器,填写相应的smtp服务器,发信人地址,用户名和密码.   填写发件人地址和收件人地址来测试邮件是否发送成功.

  9. MVC扩展HtmlHelper,加入RadioButtonList、CheckBoxList、DropdownList

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions ...

  10. mvc 封装控件使用mvcpager

    具体使用如下: 前台部分: @RenderPage("~/Views/Controls/_Pagebar.cshtml", new PageBar { pageIndex = Mo ...