springmvc bean对象属性注入方法

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userService" class="com.howhy.impl.IUserServiceImpl">
    </bean>
    <bean id="userDao" class="com.howhy.impl.IUserDaoImpl"></bean>
    <bean id="birth" class="java.util.Date"></bean>
    <!-->属性注入第一种方法构造函数
    public class IndexService {
    private String name;
    private Integer age;
    private Date date;
    public IndexService(String name,Integer age,Date date){
        this.name=name;
        this.age=age;
        this.date=date;
    }
    public void say(){
        System.out.printf("name:"+name+"age:"+age+"date:"+date);
    }
    <-->
<!--    <bean id="indexService" class="com.howhy.service.IndexService">-->
<!--        <constructor-arg type="java.lang.String" value="howhy" name="name"></constructor-arg>-->
<!--        <constructor-arg value="12" name="age"></constructor-arg>-->
<!--        <constructor-arg name="date" ref="birth"></constructor-arg>-->
<!--    </bean>-->
    <!-->属性注入第二种方法属性set方法
   public class IndexService {
    private String name;
    private Integer age;
    private Date date;
    private String[] strArr;
    private List<String> strList;
    private Set<String> strSet;
    private Map<String,String> mapStr;
    private Properties props;
    public void setStrArr(String[] strArr) {
        this.strArr = strArr;
    }

    public void setStrList(List<String> strList) {
        this.strList = strList;
    }

    public void setStrSet(Set<String> strSet) {
        this.strSet = strSet;
    }

    public void setMapStr(Map<String, String> mapStr) {
        this.mapStr = mapStr;
    }

    public void setProps(Properties props) {
        this.props = props;
    }



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

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

    public void setDate(Date date) {
        this.date = date;
    }

    public void say(){
        System.out.printf("name:"+name+"age:"+age+"date:"+date);
    }
}
    <-->
    <bean id="indexService1" class="com.howhy.service.IndexService">
        <property name="name" value="user"></property>
        <property name="age" value="13"></property>
        <property name="date" ref="birth"></property>
        <property name="strArr">
            <array>
                <value>12</value>
                <value>12</value>
            </array>
        </property>
        <property name="strList">
            <list>
                <value>12l</value>
                <value>12l</value>
            </list>
        </property>
        <property name="strSet">
            <set>
                <value>12s</value>
                <value>12s</value>
            </set>
        </property>
        <property name="mapStr">
           <map>
               <entry key="aa" value="33"></entry>
           </map>
        </property>
        <property name="props">
            <props>
                <prop key="aa">123</prop>
            </props>
        </property>
    </bean>
</beans>

 

posted @ 2020-08-08 22:27  howhy  阅读(629)  评论(0)    收藏  举报