(1)使用set方法:
public class Book {
private String bookname;
public void setBookname(String bookname) {
this.bookname = bookname;
}
xml配置:
<bean id="book" class="com.example.propetys.Book">
<!-- 注入属性值 name属性:值是你定义的属性的名称,value属性:设置的具体的值 -->
<property name="bookname" value="九阳真经"></property>
</bean>
(2)有参数的构造
public class PropertDemo1 {
private String username;
public PropertDemo1(String username) {
this.username = username;
}
xml配置:
<bean id="demo" class="com.example.propetys.PropertDemo1">
<!-- 有参构造方法注入 -->
<constructor-arg name="username" value="liuguxiia"></constructor-arg>
</bean>
(3)使用接口注入
public Interface Dao{
public void delete(String name);
}
public class DaoImpl implements Dao{
private String name;
public void delete(String name){
this.name=name;
}
}
在spring框架里,只允许前两种方式。