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类型
spring p 标签的更多相关文章
- <spring:message> 标签
可以使用<spring:message>标签结合 ResourceBundleMessageSource 的功能,在网页上显示 messages.properties 中的文字讯息,例如在 ...
- dubbo源码—dubbo自定义spring xml标签
dubbo为了和spring更好的集成,提供了一些xml配置标签,也就是自定义标签 spring自定义标签 spring自定义标签的方式如下: 设计配置属性和JavaBean 编写xsd文件,校验xm ...
- Spring component-scan 标签的实现
在以前文章Spring自定义标签实现中,曾说过,在sprin g 配置文件中,除了be an beans import 常用的标签意外,其他的标签都是遵循Spring 自定义标签的扩展机制进行实现功能 ...
- spring boot+freemarker+spring security标签权限判断
spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...
- spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />
spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...
- spring基础---->spring自定义标签(一)
Spring具有一个基于架构的扩展机制,可以使用xml文件定义和配置bean.本博客将介绍如何编写自定义XML bean的解析器,并用实例来加以说明.其实我一直相信 等你出现的时候我就知道是你. Sp ...
- spring自定义标签之 自我实现
引言: 最近心情比较难以平静,周末的两天就跑出去散心了,西湖边上走走,看日落,还是不错的.回来博客上发现,在自定义标签上,最后一步实现忘记加上了.其实,人生的路程中,我们总是实现着自我的价值,让自己 ...
- spring自定义标签之 规范定义XSD
引言: spring的配置文件中,一切的标签都是spring定义好的.<bean/>等等,有了定义的规范,才能让用户填写的正常可用.想写自定义标签,但首先需要了解XML Schema De ...
- [转]spring property标签中的 ref属性和ref 标签有什么不同
spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...
- Spring 自定义标签配置
前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Sp ...
随机推荐
- Python之关系字段
参考:https://blog.csdn.net/pugongying1988/article/details/72870264 关系字段:一对一,多对一,多对多 一对一: 现在有很多一对一辅导班, ...
- xcopy 复制目录及子目录
例:将a文件夹内的所有内容(包括子文件夹)复制到b文件夹 xcopy a\* b /y /e /i /q 说明: /y:不弹出“确认是否覆写已存在目标文件”的提示 /e:复制文件及子文件夹内所有内容, ...
- 解决PL/SQL导出cvs文件中文显示乱码
方法 1 导出csv格式文件 新建excel文件 比如 a.xls excel软件打开 选择菜单数据 -导入外部数据 unicode默认下一步 选择 逗号分隔符 点击确定导入完成 方法 2 导出成h ...
- 页面中 json 格式显示 数据
在页面中,有时候我们需要的不仅仅是将数据显示出来,而且要以以 json 的格式显示数据,如显示接口的时候 我们需要如下显示 这个时候,主要用到了 <pre> 标签 $.get(" ...
- Access denied for user 'root'@'localhost' (using password:YES)解决方法
Access denied for user 'root'@'localhost' (using password:YES)解决方法 在MySQL的使用过程中,我们可能会碰到“Access denie ...
- .Spark Streaming(上)--实时流计算Spark Streaming原理介
Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍 http://www.cnblogs.com/shishanyuan/p/474 ...
- chnagyong sql
select gid,count(distinct mid) from members group by gid mysql> SELECT IFNULL(NULL,); mysql> 1 ...
- RN中关于ListView的使用
1. ListView dataSource 介绍: ListView需要指定数据的来源.传入数据必须是数组,或者是字典里面嵌套数组 系统会根据你传入的数据自动生成section和row 每一个字典的 ...
- (转) Ringbuffer为什么这么快?
原文地址:http://ifeve.com/ringbuffer/ 最近,我们开源了LMAX Disruptor,它是我们的交易系统吞吐量快(LMAX是一个新型的交易平台,号称能够单线程每秒处理数百万 ...
- INI
.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理 ...