(1)使用set方法:

public class Book {
  private String bookname;
public void setBookname(String bookname) {
    this.bookname = bookname;
}

xml配置:

<bean ></property>
</bean>

(2)有参数的构造

public class PropertDemo1 {
    private String username;
    public PropertDemo1(String username) {
        this.username = username;
    }

xml配置:

<bean ></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框架里,只允许前两种方式。

相关文章: