**一.自动装配bean: autowire
1.Byname:通过get和set方法来进行bean的装配
2.ByType:通过类型进行bean的装配,如果在此文件中存在属性的类型超过一个的时候,使用此方法会出现异常**
<bean id="address2" class="zhang.spring.enities.autowire.Address" p:city="beijing" p:street="beijingtiananmen"></bean>
<bean id="company" class="zhang.spring.enities.autowire.Company" p:boss="zzhangyukang" p:name="beijingIt"></bean>
<bean id="person" class="zhang.spring.enities.autowire.Person" p:name="zhangyukang"
autowire="byType" ></bean>
二.在IOC容器中使用属性文件.
在类路径下新建一个属性文件例如:db.properties
user=root
password=zhangyukang
driverclass=com.mysql.jdbc.Driver
url=jdbc:mysql:///hibernate
在IOC容器中配置:
1.引入外部的属性文件
2.引入从c3p0数据源
3.配置bean
4.注意在配置属性的时候可以使用’’$ {username}’’
<context:property-placeholder location="classpath:db.properties" />
<!-- 使用外部的属性 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
测试获取数据库连接:
/**
* 使用属性文件的方式来连接数据库
* 注意点:在配置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());
}
三:IOC容器中的bean的作用域:
在applicationContext.xml中配置bean的作用域可以使用scope属性:
<!-- bean 的作用域:singleton:单例的在IOC容器被创建的时候bean被创建(作用域默认是singleton)
prototype:原型:在IOC被创建的时候,bean对象不会被创建,在需要获取bean的时候被创建
-->
<bean id="student" scope="prototype" class="zhang.spring.enities.spel.Student" autowire="byName">
<property name="name" value="zhangyukang"></property>
<property name="info" value="#{score.chinese>100?'优秀':'中等'}"></property>
<property name="sum" value="#{score.chinese+score.english+score.math}"></property>
</bean>
四.IOC容器中利用工厂方法创建bean的方法:
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<String,Car> 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中配置如下:
<!-- 使用静态的bean工厂来创建对象 -->
<bean id="staticStudentFactory" class="zhang.spring.beans.Factory.StaticStudentFactory"
factory-method="getCar">
<constructor-arg value="baoma"></constructor-arg>
</bean>
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<String,Car> 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中的配置如下:
<!-- 使用实例的bean工厂创建对象 -->
<bean id="instantCarFactory" class="zhang.spring.beans.Factory.InstantCarFactory">
</bean>
<bean id="car" factory-bean="instantCarFactory" factory-method="getCar">
<constructor-arg value="bmo"></constructor-arg>
</bean>
**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<Customer> {
@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
}
}
<bean id="customerFactoryBean" class="zhang.spring.beans.Factory.CustomerFactoryBean">
</bean>
五.bean的生命周期方法.
以下面的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();
}
}
**
六.bean的后置处理器:
**
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;
}
}
<!-- 配置bean的后置处理器 -->
<bean class="zhang.spring.beans.cycle.MyBeanPostProcessor"></bean>