Spring dependency injection method set injection

Tags related to: property

Tag attributes:

  name: the method used to specify the set when injected called by name (note the lowercase value name is the name of a set method)

  value: for providing basic data types and data type String

  ref: used to specify other bean. Its value is to appear in the spring of core Ioc container had bean object

 

Advantage: Creating the object is no clear requirements, you can directly use the default constructor

Drawbacks: if a member must have a value, it is possible to get an object when there is no set method execution

<bean id="accountService" class="com.xuefei.service.impl.AccountServiceImpl">
        <property name="name" value="小王"></property>
        <property name="age" value="22"></property>
        <property name="date" ref="now"></property>
    </bean>
    <bean id="now" class="java.util.Date"></bean>
package com.xuefei.service.impl;

import com.xuefei.service.AccountService;

import java.util.Date;

/**
 * 账户业务层实现类
 */
public class AccountServiceImpl implements AccountService {

    String name;
    Integer age;
    Date date;

    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.println ( "I" + name + "this year" + age + "years old!" + DATE); 
    } 

    public  void saveAccount () { 
    } 
}

 

Guess you like

Origin www.cnblogs.com/lililixuefei/p/11874850.html