spring p 标签

需要引入命名空间

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类型

时间: 2024-08-09 10:33:07

spring p 标签的相关文章

Spring 自定义标签配置

前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Spring标签的自定义配置为以后实现分布式服务框架做技术储备. 技术分析:Spring的标签配置是通过XML来实现的,通过XSD(xml Schema Definition)来定义元素,属性,数据类型等. Spring自定义标签解析 1.Spring启动时通过扫描根目录下的META-INF文件下的sp

spring 自定义标签 学习

自定义配置文件到spring 中,有时候想做一些数据结构的配置化信息,根据业务做一个扩展. 首先: 在项目的META-INF目录下新建两个文件spring.handlers,和spring.shcemas Spring.handlers在类org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver中已经写死了要取mapping的handlerMappingsLocation的路径 public static fina

spring 自定义标签 学习二

在上篇中写的只支持写属性,不支持标签property的写法,但是如果有时候我们还想做成支持 property的用法,则可以在xsd中增加spring 自带的xsd引用 修改xsd文件如下: <?xml version="1.0"encoding="UTF-8"?> <xsd:schema xmlns="http://www.ruishenh.com/custom/myTest" xmlns:xsd="http://ww

(转载)Spring自定义标签的原理

Spring自定义标签的原理 XML通常通过DTD.XSD定义,但DTD的表达能力较弱,XSD定义则能力比较强,能够定义类型,出现次数等.自定义标签需要XSD支持,在实现时使用Namespace扩展来支持自定义标签. <bean id="beanId" class="com.xxx.xxxx.Xxxxx"> <property name="property1"> <value>XXXX</value>

Spring Security 4 安全视图片段 使用标签(Spring Security 标签)

上一篇文章:Spring Security 4 退出 示例(带源码) 下一篇文章: Spring Security 4 基于角色的登录例子(带源码) 原文地址:http://websystique.com/spring-security/spring-security-4-secure-view-layer-using-taglibs/ [剩余文章,将尽快翻译完毕,敬请期待. 翻译by 明明如月 QQ 605283073] 本教程向你展示怎样创建安全视图层,Spring MVC web 应用中,

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

dubbo为了和spring更好的集成,提供了一些xml配置标签,也就是自定义标签 spring自定义标签 spring自定义标签的方式如下: 设计配置属性和JavaBean 编写xsd文件,校验xml属性和便于编辑器提示 编写NamespaceHandler和BeanDefinitionParser解析xml对应的标签 编写spring.handlers和spring.schemas串联起所有部件,放在META_INF下面 在xml中引入对应的标签就可以使用 dubbo自定义标签 dubbo对

Spring 自定义标签

Spring 工作流程是先加载解析xml配置文件:配置文件中存在默认的标签,也可以自定义标签.解析默认标签调用: 1 private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) { 2 if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) { 3 importBeanDefinitionResource(ele); 4 } 5 else if

Spring IOC 标签的解析

Spring IOC 标签的解析 上一篇文章说了Spring中的标签包括默认标签和自定义标签两种,本节继续来研究默认标签的解析,parseBeanDefinitions方法为解析标签的总入口 protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) { if (delegate.isDefaultNamespace(root)) { NodeList nl = root.get

spring源码剖析(二)Spring默认标签解析及注册实现

在使用spring的时候,我也经常会使用到bean标签,beans标签,import标签,aop标签等. 下面主要为读者介绍spring的默认的自带标签的解析流程. 验证模式(DTD&XSD) dtd基本已被淘汰,现在spring的验证模式基本都是采用xsd文件作为xml文档的验证模式,通过xsd文件可以检查该xml是否符合规范,是否有效.在使用xsd文件对xml文档进行校验的时候,除了要名称空间外(xmlns="http://www.springframework.org/schema

Spring P标签的使用

Spring的p标签是基于XML Schema的配置方式,目的是为了简化配置方式.由于Spring的p标签是spring内置的,只要在xml头部申明下就可以调用.如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org