<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 经过 bean 的 init-method 和 destroy-method="destroy" 为 bean 指定初始化和销毁方法 --> <bean id="car" class="com.atguigu.spring.beans.cycle.Car" init-method="init" destroy-method="destroy"> <property name="brand" value="Baoma"></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 的后置处理器: 不须要配置 id, IOC 容器自动识别是一个 BeanPostProcessor--> <bean class="com.atguigu.spring.beans.cycle.MyBeanPostProcessor"></bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.atguigu.spring.beans.spel.Address"> <!-- 使用 spel 为属性赋一个字面值 --> <property name="city" value="#{'Beijing'}"></property> <property name="street" value="Wudaokou"></property> </bean> <bean id="car" class="com.atguigu.spring.beans.spel.Car"> <property name="brand" value="Baoma"></property> <property name="price" value="500000"></property> <!-- 使用 SpEL 引用类的静态属性 --> <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property> </bean> <bean id="person" class="com.atguigu.spring.beans.spel.Person"> <!-- 使用 SpEl 来引用其余的 bean --> <property name="car" value="#{car}"></property> <property name="city" value="#{address.city}"></property> <!-- 在 SpEl 中使用运算符 --> <property name="info" value="#{car.price > 300000 ? '金领' : '白领'}"></property> <property name="name" value="Tom"></property> </bean> </beans>