Spring (three) set injection

Set injection requires Bean to provide a default constructor and corresponding Setter methods for properties that need to be injected.

Spring first calls the Bean's default constructor to instantiate the Bean object, and then calls the Setter method to inject property values ​​through reflection.

Assuming that the Bean explicitly defines a constructor with parameters, you need to provide a default constructor with no parameters at the same time, otherwise an exception will be thrown when using attribute injection.

public class UserServiceImpl2 implements IUserService {
    
    
    private String name;
    private Integer age;
    private Date birthday;

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

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

    public void setBirthday(Date birthday) {
    
    
        this.birthday = birthday;
    }

    public void queryAll() {
    
    
        System.out.println(name+","+age+","+birthday+".");
    }
}
   <bean id="UserService2" class="com.imis.service.impl.UserServiceImpl2">
       <property name="age" value="18"></property>
        <property name="name" value="mike"></property>
        <property name="birthday" ref="now"></property>
    </bean>

Guess you like

Origin blog.csdn.net/weixin_45925906/article/details/112745314