直接看实例:
Users.java
package cn.iborder.entity;import cn.iborder.logging.MyLog;public class Users { private Integer id; private String username; private String password; private String name; public Users(){ System.out.println(MyLog.showLog(this)+"无参构造函数,Users对象创建"+this); } public Users(Integer id,String username,String password,String name){ System.out.println(MyLog.showLog(this)+"有参构造函数,Users对象创建"+this); this.id = id; this.username = username; this.password = password; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Users [id=" + id + ", username=" + username + ", password=" + password + ", name=" + name + "]"; } public void init() { System.out.println(MyLog.showLog(this)+"对象创建时调用"+this); } public void destory() { System.out.println(MyLog.showLog(this)+"对象销毁时调用"+this); } }
BeanFactory.java
package cn.iborder.factory;import cn.iborder.entity.Users;public class BeanFactory { public Users getInstance() { return new Users(3, "baidu", "baidupass", "baiduname"); } public static Users getStaticInstance() { return new Users(4, "sina", "sinapass", "sinaname"); }}
applicationContext.xml
applicationContext.xml中给bean对象属性注入值(依赖注入)
1、通过构造函数注入
具体看上面applicationContext.xml配置文件
2、通过setter方法注入值
具体看上面applicationContext.xml配置文件
3、P名称空间注入值
示例
4、自动装配(不推荐使用)
5、注解