一:指定bean的依赖关系
例如examplebean对象依赖examplebean1对象,那么在创建examplebean对象之前就
需要先创建examplebean1对象。
1:创建Examplebean1类:
1 /** 2 * 3 */ 4 package com.hlcui.dao; 5 6 /** 7 * @author Administrator 8 * 9 */ 10 public class ExampleBean1 { 11 public ExampleBean1() { 12 System.out.println("实例化ExampleBean1..."); 13 } 14 }
2:在spring容器配置文件中配置ExampleBean1对象,并且指定bean的依赖关系
depends-on="ExampleBean1"
1 <!-- 实例化ExampleBean对象 --> 2 <bean id="exampleBean" class="com.hlcui.dao.ExampleBean" lazy-init="true" 3 init-method="init" destroy-method="destroy" scope="singleton" depends-on="ExampleBean1"></bean> 4 5 <!-- 实例化ExampleBean1对象 --> 6 <bean id="ExampleBean1" class="com.hlcui.dao.ExampleBean1" 7 lazy-init="true"></bean>
3:运行测试方法:
1 @Test 2 /**测试bean的依赖关系*/ 3 public void testNewExampleBean() { 4 ApplicationContext ac = getApplicationContext(); 5 ExampleBean eb1 = ac.getBean("exampleBean", ExampleBean.class); 6 System.out.println(eb1); 7 }
通过结果可以看出先实例化依赖对象,再创建对象。
二:spring实现setter注入到jdbcDatasource数据库连接参数
1:导入jar包,在spring原有的支持包的基础上导入,oracle数据库的驱动包
2:创建JDBCDataSource类
1 /** 2 * 3 */ 4 package com.hlcui.dao; 5 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.sql.SQLException; 9 10 /** 11 * @author Administrator 12 * 13 */ 14 public class JDBCDataSource { 15 16 private String driverClass; 17 18 private String url; 19 20 private String username; 21 22 private String password; 23 24 public String getDriverClass() { 25 return driverClass; 26 } 27 28 public void setDriverClass(String driverClass) { 29 try { 30 Class.forName(driverClass); // 注册数据库驱动 31 this.driverClass = driverClass; 32 } catch (ClassNotFoundException e) { 33 e.printStackTrace(); 34 } 35 36 } 37 38 public String getUrl() { 39 return url; 40 } 41 42 public void setUrl(String url) { 43 this.url = url; 44 } 45 46 public String getUsername() { 47 return username; 48 } 49 50 public void setUsername(String username) { 51 this.username = username; 52 } 53 54 public String getPassword() { 55 return password; 56 } 57 58 public void setPassword(String password) { 59 this.password = password; 60 } 61 62 // 获取数据库连接 63 public Connection getConn() throws SQLException { 64 return DriverManager.getConnection(url, username, password); 65 } 66 67 // 关闭数据库连接 68 public void closeConn(Connection conn) { 69 if (null != conn) { 70 try { 71 conn.close(); 72 } catch (SQLException e) { 73 e.printStackTrace(); 74 } 75 } 76 } 77 }
3:配置bean对象
核心代码如下:
1 <!-- 配置jdbc数据源 --> 2 <bean id="jdbcDatasource" class="com.hlcui.dao.JDBCDataSource"> 3 <property name="driverClass" value="oracle.jdbc.OracleDriver"></property> 4 <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> 5 <property name="username" value="system"></property> 6 <property name="password" value="orcl"></property> 7 </bean>
4:测试方法以及运行结果
1 @Test 2 /**测试获取jdbc连接*/ 3 public void testJdbcConnection() { 4 try { 5 ApplicationContext ac = getApplicationContext(); 6 JDBCDataSource ds = ac.getBean("jdbcDatasource", 7 JDBCDataSource.class); 8 System.out.println(ds.getConn()); 9 } catch (Exception e) { 10 e.printStackTrace(); 11 } 12 13 }
从运行结果可以看出,得到了jdbc连接对象。
三:spring实现构造器注入参数
1:创建业务实体类对象User
1 /** 2 * 3 */ 4 package com.hlcui.dto; 5 6 /** 7 * @author Administrator 8 * 9 */ 10 public class User { 11 private int id; 12 private String name; 13 private String pwd; 14 private String phone; 15 public User(int id, String name, String pwd, String phone) { 16 super(); 17 this.id = id; 18 this.name = name; 19 this.pwd = pwd; 20 this.phone = phone; 21 } 22 public User(String name, String pwd, String phone) { 23 super(); 24 this.name = name; 25 this.pwd = pwd; 26 this.phone = phone; 27 } 28 public int getId() { 29 return id; 30 } 31 public void setId(int id) { 32 this.id = id; 33 } 34 public String getName() { 35 return name; 36 } 37 public void setName(String name) { 38 this.name = name; 39 } 40 public String getPwd() { 41 return pwd; 42 } 43 public void setPwd(String pwd) { 44 this.pwd = pwd; 45 } 46 public String getPhone() { 47 return phone; 48 } 49 public void setPhone(String phone) { 50 this.phone = phone; 51 } 52 @Override 53 public int hashCode() { 54 final int prime = 31; 55 int result = 1; 56 result = prime * result + id; 57 result = prime * result + ((name == null) ? 0 : name.hashCode()); 58 result = prime * result + ((phone == null) ? 0 : phone.hashCode()); 59 result = prime * result + ((pwd == null) ? 0 : pwd.hashCode()); 60 return result; 61 } 62 @Override 63 public boolean equals(Object obj) { 64 if (this == obj) 65 return true; 66 if (obj == null) 67 return false; 68 if (getClass() != obj.getClass()) 69 return false; 70 User other = (User) obj; 71 if (id != other.id) 72 return false; 73 if (name == null) { 74 if (other.name != null) 75 return false; 76 } else if (!name.equals(other.name)) 77 return false; 78 if (phone == null) { 79 if (other.phone != null) 80 return false; 81 } else if (!phone.equals(other.phone)) 82 return false; 83 if (pwd == null) { 84 if (other.pwd != null) 85 return false; 86 } else if (!pwd.equals(other.pwd)) 87 return false; 88 return true; 89 } 90 @Override 91 public String toString() { 92 return "User [> phone 93 + ", pwd=" + pwd + "]"; 94 } 95 96 }