Spring_DI依赖注入基础知识
文章目录
设值注入
public class HelloWorld1{
private String name;
//必须要有set方法才 xml才可以访问到
public void setName(String name) {
this.name = name;
}
}
spring.xml
<bean id="helloworld" class="com.spring.spring_bean.HelloWorld1">
<property name="name" value="chi123"></property>
</bean>
总结:设值注入是由set方法来进行的 并不是以他的属性 如果有属性而没有set方法的话程序会报出 : 查找不到name这个属性值
name的值为 chi123
构造方法注入constructor
public class HelloWorld1{
private String name;
private int age;
//必须要有set方法才 xml才可以访问到
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
spring.xml
<bean id="helloworld" class="com.spring.spring_bean.HelloWorld1">
//下面注入值是按照 构造方法中的构造参数的顺序进行的 从左往右进行 可以设置index 还有 type
<constructor-arg value="吴亦凡1"></constructor-arg>
<constructor-arg value="12"></constructor-arg>
</bean>```
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。