Spring學習-day07

<?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>
相關文章
相關標籤/搜索