首页 > 其他分享 >spring set-简单类型注入

spring set-简单类型注入

时间:2022-11-13 11:23:27浏览次数:60  
标签:set spring void private public birth type class 注入

举例说明:

Clazz类

package per.sxhzs.spring6.bean;

/**
 * 表示班级
 */
public class Clazz {
    private String name;

    @Override
    public String toString() {
        return "Clazz{" +
                "name='" + name + '\'' +
                '}';
    }

    public void setName(String name) {
        this.name = name;
    }
}

Season接口

package per.sxhzs.spring6.bean;

public enum Season {
    SPRING, SUMMER, AUTUMN, WINTER
}

SimpleValueType类

package per.sxhzs.spring6.bean;

import java.util.Date;

public class SimpleValueType {
    /*
    public static boolean isSimpleValueType(Class<?> type) {
		return (Void.class != type && void.class != type &&
				(ClassUtils.isPrimitiveOrWrapper(type) ||
				Enum.class.isAssignableFrom(type) ||
				CharSequence.class.isAssignableFrom(type) ||
				Number.class.isAssignableFrom(type) ||
				Date.class.isAssignableFrom(type) ||        java.util.Date是简单类型
				Temporal.class.isAssignableFrom(type) ||    Temporal是Java8提供的时间和时区类型
				URI.class == type ||
				URL.class == type ||
				Locale.class == type ||                     Locale是语言类,也是简单类型。
				Class.class == type));
	}
	*/
    private int age;
    private Integer age2;

    private boolean flag;
    private Boolean flag2;

    private char c;
    private Character c2;

    private Season season;

    private String username;

    private Class clazz;

    //  生日
    private Date birth;

    @Override
    public String toString() {
        return "SimpleValueType{" +
                "age=" + age +
                ", age2=" + age2 +
                ", flag=" + flag +
                ", flag2=" + flag2 +
                ", c=" + c +
                ", c2=" + c2 +
                ", season=" + season +
                ", username='" + username + '\'' +
                ", clazz=" + clazz +
                ", birth=" + birth +
                '}';
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setAge2(Integer age2) {
        this.age2 = age2;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void setFlag2(Boolean flag2) {
        this.flag2 = flag2;
    }

    public void setC(char c) {
        this.c = c;
    }

    public void setC2(Character c2) {
        this.c2 = c2;
    }

    public void setSeason(Season season) {
        this.season = season;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }
}

配置类

<!--测试那些类型是简单类型-->
    <bean id="svt" class="per.sxhzs.spring6.bean.SimpleValueType">
        <property name="season" value="SPRING"/>
        <property name="username" value="张三"/>
        <property name="age" value="18"/>
        <property name="age2" value="20"/>
        <property name="flag" value="false"/>
        <property name="flag2" value="true"/>
        <property name="c" value="男"/>
        <property name="c2" value="女"/>
        <property name="clazz" value="java.lang.String"/>
        <!--报错了,说这个"2002-02-09"字符串无法转换成Java.util.Date-->
<!--        <property name="birth" value="2002-02-09"/>-->
        <!--如果要把Date当作简单类型,使用value赋值的话,这个日期字符串格式有要求。-->
        <!--在实际开发中,我们一般不会把Date当作简单类型,虽然他是简单类型。一般会采用ref给Date类型的属性赋值。-->
        <property name="birth" value="Sat Nov 12 19:37:40 CST 2022"/>
    </bean>

测试类

@Test
    public void testSimpleTypeSet() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
//        User user = applicationContext.getBean("userBean", User.class);
//        System.out.println(user);

        SimpleValueType svt = applicationContext.getBean("svt", SimpleValueType.class);
        System.out.println(svt);
    }

运行结果

标签:set,spring,void,private,public,birth,type,class,注入
From: https://www.cnblogs.com/sxhxh/p/16885627.html

相关文章