当前位置 博文首页 > zhouyu的博客:Spring使用xml配置,想要创建对象时注入想要的日

    zhouyu的博客:Spring使用xml配置,想要创建对象时注入想要的日

    作者:[db:作者] 时间:2021-08-21 13:07

    Spring使用xml配置,想要创建对象时注入想要的日期方法:

    <!-- Spring使用xml配置,想要创建时注入想要的日期方法:-->
    <bean id="accountService" class="org.zy.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="test"></constructor-arg>
        <constructor-arg name="age" value="1"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>
    <!-- 配置一个日期对象 -->
    <bean id="now" factory-bean="now1" factory-method="parse">
        <constructor-arg name="source" value="2001-1-1 22:11:11"></constructor-arg>
    </bean>
    <bean id="now1" class="java.text.SimpleDateFormat" >
        <constructor-arg name="pattern" value="yyyy-MM-dd HH:mm:ss"></constructor-arg>
    </bean>
    
    package org.zy.service.impl;
    
    import org.zy.service.IAccountService;
    
    import java.util.Date;
    
    /**
     * 
     */
    public class AccountServiceImpl implements IAccountService {
        private String name;
        private Integer age;
        private Date birthday;
    
        public AccountServiceImpl(String name, Integer age, Date birthday){
            this.name = name;
            this.age = age;
            this.birthday = birthday;
    
        }
        public void saveAccount(){
            System.out.println("调用了..." + name + "," + age + "," + birthday);
        }
    }
    
    
    cs