1.Byname:通过get和set方法来进行bean的装配
2.ByType:通过类型进行bean的装配,如果在此文件中存在属性的类型超过一个的时候,使用此方法会出现异常**
在类路径下新建一个属性文件例如:db.properties
user=root
password=zhangyukang
driverclass=com.mysql.jdbc.Driver
url=jdbc:mysql:///hibernate
在IOC容器中配置:
1.引入外部的属性文件
2.引入从c3p0数据源
3.配置bean
4.注意在配置属性的时候可以使用’’$ {username}’’
测试获取数据库连接:
/**
* 使用属性文件的方式来连接数据库
* 注意点:在配置user的时候不要使用username,在spring中username有特殊的含义,配置的时候
* 会发现数据库连接抛出异常:Access denied for user 'Administrator'@'localhost'
* @throws SQLException
*/
@Test
public void testdataSource() throws SQLException{
DataSource bean = (DataSource) applicationContext.getBean("dataSource");
System.out.println(bean.getConnection());
}
在applicationContext.xml中配置bean的作用域可以使用scope属性:
1.通过静态的工厂方法来创建bean
2.通过实例的工厂方法来创建bean
3.通过BeanFactory来获取bean(是spring提供的接口)
如下:
**1.通过静态的工厂方法来创建bean
**
package zhang.spring.beans.Factory;
import java.util.HashMap;
import java.util.Map;
import zhang.spring.enitites.Car;
public class StaticStudentFactory {
private static Map map=new HashMap<>();
static{
map.put("baoma", new Car("baoma",10000));
map.put("lufu", new Car("lufu",20000));
}
public static Car getCar(String band){
return map.get(band);
}
}
**
在applicationContext.xml中配置如下:
2.通过实例的工厂方法来创建bean
package zhang.spring.beans.Factory;
import java.util.HashMap;
import java.util.Map;
import zhang.spring.enitites.Car;
/**
* 使用实例工厂类来获取bean
* @author Administrator
*
*/
public class InstantCarFactory {
private Map map=new HashMap<>();
public InstantCarFactory() {
map.put("baoma", new Car("baoma", 120));
map.put("luhu", new Car("baoma", 120));
map.put("bmo", new Car("bmo", 120));
}
public Car getCar(String band){
return map.get(band);
}
}
applicationContext.xml中的配置如下:
**3.通过BeanFactory来获取bean(是spring提供的接口)****
package zhang.spring.beans.Factory;
import org.springframework.beans.factory.FactoryBean;
import zhang.spring.beans.cycle.Customer;
public class CustomerFactoryBean implements FactoryBean {
@Override
public Customer getObject() throws Exception {
// TODO Auto-generated method stub
Customer customer=new Customer();
customer.setName("$$zhangyukang");
customer.setPassword("305316");
return customer;
}
@Override
public Class> getObjectType() {
// TODO Auto-generated method stub
return Customer.class;
}
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
return true;
}
public CustomerFactoryBean() {
// TODO Auto-generated constructor stub
}
}
以下面的customer类为例:
package zhang.spring.beans.cycle;
public class Customer {
private String name;
private String password;
public void init(){
System.out.println("bean init-------");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("set name :"+name);
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
System.out.println("set password:"+password);
this.password = password;
}
public void destory(){
System.out.println("bean destory--------");
}
@Override
public String toString() {
return "Customer [name=" + name + ", password=" + password + "]";
}
}
配置
测试
package zhang.spring.beans.cycle;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//创建ioc容器的时候,应为作用域是singleton,因此创建对应的容器中的bean,给bean的属性进行赋值
//在调用指定的生命周期方法init
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans-cycle.xml");
Customer customer = (Customer) applicationContext.getBean("customer1");
System.out.println(customer);
//关闭ioc容器,调用生命周期中的destory方法
applicationContext.close();
}
}
**
**
package zhang.spring.beans.cycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* bean的后置处理器:实现接口: BeanPostProcessor
* 重写对应的方法
* @author Administrator
*
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
//在bean中的init()方法执行之前调用
//bean:bean实例本身,beanName:在ioc容器中配置的bean的名字
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if("customer".equals(beanName)){
System.out.println("--------------------------------");
System.out.println("init()方法执行之后调用");
System.out.println("bean:"+bean);
System.out.println("beanName:"+beanName);
System.out.println("--------------------------------");
}
return bean;
}
//在bean中的init()方法执行之后调用
@Override
public Object postProcessBeforeInitialization(Object bean, String arg1) throws BeansException {
if("customer".equals(arg1)){
System.out.println("--------------------------------");
Customer customer =(Customer)bean;
customer.setName("章宇康");
System.out.println("init()方法执行之前调用");
System.out.println("--------------------------------");
}
return bean;
}
}