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

  • 相关阅读:
    ADB常用命令
    HttpClient4.x 使用cookie保持会话
    HttpClient发起Http/Https请求工具类
    MakeFile 文件的使用
    SQL连接查询和嵌套查询详解
    Linux中进程控制块PCB-------task_struct结构体结构
    Mysql数据库--语句整理/提升/进阶/高级使用技巧
    进程池分析
    mmap和shm共享内存的区别和联系
    IO多路复用模型之epoll实现机制
  • 原文地址:https://www.cnblogs.com/zno2/p/4856935.html
Copyright © 2020-2023  润新知