1.导入外部文件
1)编写db.properties配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/blog jdbc.user=root jdbc.password=root
2)编写applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 初始化c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> </beans>
3)编写测试代码,自己处理吧
2.SpEl表达式
SpEL使用#{}做为定界符,引用其他对象,属性,方法
支持运算符,正则,直接调用静态方法或属性
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <bean id="address" class="com.hous.spring.Address"> <property name="city" value="#{'suqian'}"></property> <property name="street" value="双河大道"></property> </bean> <bean id="car" class="com.hous.spring.Car"> <property name="brand" value="aodi"></property> <property name="price" value="300000"></property> <property name="tyerPerimeter" value="#{T(java.lang.Math).PI*20}"></property> </bean> <bean id="person" class="com.hous.spring.Person"> <property name="age" value="24"></property> <property name="car" value="#{car}"></property> <property name="info" value="#{car.price>20000 ? '程序员' : '搬砖的'}"></property> </bean> </beans>
IoC容器中bean的生命周期
1)通过构造器或工厂方法创建bean实例
2)为bean属性设置值或引用其他bean
3)调用bean的初始化方法
4)使用bean
5)当容器关闭时,调用bean的销毁方法
在bean的声明中设置init-method和destroy-method属性
为bean指定初始化和销毁方法
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <bean id="address" class="com.hous.spring.Address" init-method="init" destroy-method="destroy"> <property name="city" value="#{'suqian'}"></property> <property name="street" value="双河大道"></property> </bean> </beans>
package com.hous.spring; public class Address { private String city; private String street; /** * @return the city */ public String getCity() { return city; } /** * @param city * the city to set */ public void setCity(String city) { this.city = city; } /** * @return the street */ public String getStreet() { return street; } /** * @param street * the street to set */ public void setStreet(String street) { this.street = street; } public void init() { System.out.println("初始化bean。。。"); } public void destroy() { System.out.println("销毁bean。。。"); } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Address [city=" + city + ", street=" + street + "]"; } }
package com.hous.test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //1.创建spring的IoC容器 ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(cxt.getBean("address")); //关闭容器 cxt.close(); } }
Bean的后置处理器
xml文件中依赖注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <bean id="address" class="com.hous.spring.Address" init-method="init" destroy-method="destroy"> <property name="city" value="#{'suqian'}"></property> <property name="street" value="双河大道"></property> </bean> <!-- 实现BeanPostProcessor接口,并提供具体方法 Object postProcessAfterInitialization(Object bean, String beanName) 在init-method之前执行 Object postProcessBeforeInitialization(Object bean, String beanName) 在init-method之后执行 bean:bean实例本身 beanName: IOC容器配置bean的名字 返回值:是返回给用户的bean 注意: 以上方法可以修改返回的bean,甚至返回新的bean --> <bean class="com.hous.processor.MyBeanProcessor"></bean> </beans>
实现BeanPostProcessor接口
package com.hous.processor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization" + beanName + "," + bean); return bean; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization" + beanName + "," + bean); return bean; } }