zoukankan      html  css  js  c++  java
  • 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类型

  • 相关阅读:
    HDU-6801 2020HDU多校第三场T11 (生成函数)
    [HDU-6791] 2020HDU多校第三场T1(回文自动机)
    回文自动机 (PAM,Palindrome Automaton)
    字符串的Period(周期),Border
    「APIO2019」路灯 (K-D Tree / 树套树 / CDQ + 树状数组)
    「APIO2019」桥梁(询问分块+并查集)
    「APIO2019」奇怪装置
    「APIO2018」选圆圈(K-D Tree/CDQ+Set)
    堆小结
    【[HNOI/AHOI2018]毒瘤】
  • 原文地址:https://www.cnblogs.com/zno2/p/4856935.html
Copyright © 2011-2022 走看看