spring学习之DI(依赖注入)构造和Set方式

构造方式

	//在类中使用有参构造
	private String name;
    public UserMapperImpl(String name) {
        this.name = name;
    }
<bean id="userMapper" class="top.chenyp.mapper.impl.UserMapperImpl">
        <constructor-arg name="name" value="name"/>
    </bean>

Set方式

public class UserMapperImpl2 implements UserMapper {

    private String name;

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

    @Override
    public void findById() {
        System.out.println("调用成功"+name);
    }
}
<!--    set方式-->
    <bean id="userMapper" class="top.chenyp.mapper.impl.UserMapperImpl2">
        <property name="name" value="test"/>
    </bean>

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:白松林 返回首页