其他分享
首页 > 其他分享> > spring心得

spring心得

作者:互联网

属性注入 autowire
1.xml autowire="byName"


点击查看代码
public class User {
    @Value(value = "abc")
    private String name;
    public void add(){
        System.out.println("add....");
    }

    public String getName() {
        return name;
    }

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

    private Dog dog;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
}
因为按byName规则找对应set方法,真正的setCat就会执行,对象初始化,Cat就有值了。 2.注解 @Autowire
点击查看代码
public class User {
    @Value(value = "abc")
    private String name;

    public void add() {
        System.out.println("add....");
    }

    @Autowired
    private Cat cat;
}
@Autowired与@Resource异同: @Autowired 默认byType ,想用 byName @Qualifier联合使用 @Resource 默认byName ,先按name匹配。匹配不上按type

标签:name,spring,void,private,cat,Cat,心得,public
来源: https://www.cnblogs.com/adai-study-1030/p/16523804.html