spring屬性的三種註入方法
(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框架裏,只允許前兩種方式。
spring屬性的三種註入方法