<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ~~~ <!-- Spring IOC 容器對 Bean 的生命周期進行管理的過程:(demo7) 1.通過構造器或工廠方法創建 Bean 實例 2.為 Bean 的屬性設置值和對其他 Bean 的引用 3.調用 Bean 的初始化方法 4.Bean 可以使用了 5.當容器關閉時, 調用 Bean 的銷毀方法 在 Bean 的聲明里設置 init-method 和 destroy-method 屬性, 為 Bean 指定初始化和銷毀方法. 注意:需要調用close方法 需要使用ApplicationContext 子接口的方法,聲明對象需要換成實現類。 --> ~~~ ~~~ //beans.xml <bean id="pet1" class="cn.li.lesson1.Pet" init-method="init" destroy-method="destroy"> <property name="name" value="老虎"></property> </bean> ~~~ ~~~ //pet類 package cn.li.lesson1; public class Pet { private String name; public Pet() { super(); System.out.println("空參構造器被調用"); } public String getName() { return name; } public void setName(String name) { System.out.println("set方法被調用"); this.name = name; } public void init(){ System.out.println("初始化方法"); } public void destroy(){ System.out.println("銷毀方法"); } @Override public String toString() { return "Pet [name=" + name + "]"; } } ~~~ ~~~ //測試類: public class MainTest { public static void main(String[] args) { // TODO Auto-generated method stub ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("cn/li/lesson1/beans.xml"); Pet bean = (Pet) ac.getBean("pet1"); ac.close(); } } ~~~ ![](https://box.kancloud.cn/d586c670f400728b30c2476c523c597d_201x147.png) ~~~ 更加細粒度的定制bean的聲明周期方法。 spring中提供了一個Bean后置處理器,允許在調用初始化方法前后對bean進行額外的處理。 Bean后置處理器對IOC容器里所有的Bean實例逐一處理. 作用:檢測bean屬性的正確性,或者根據特定的標準更改bean的屬性值。 實現: 需要創建一個類 實現BeanPostProcessor接口,實現兩個方法。在初始化方法被調用前后分別執行. 然后需要將當前類 注冊到springIOC容器中。 1.通過構造器或工廠方法創建 Bean 實例 2.為 Bean 的屬性設置值和對其他 Bean 的引用 3.將 Bean 實例傳遞給 Bean 后置處理器的 postProcessBeforeInitialization 方法 4.調用 Bean 的初始化方法 5.將 Bean 實例傳遞給 Bean 后置處理器的 postProcessAfterInitialization方法 6.Bean 可以使用了 7.當容器關閉時, 調用 Bean 的銷毀方法 ~~~ ~~~ //MyBeanPostProcessor類 package cn.li.lesson3; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { //調用init方法后進行處理和增強 @Override public Object postProcessAfterInitialization(Object obj, String id) throws BeansException { // TODO Auto-generated method stub System.out.println("我是bean后置處理器的方法。init之后執行"); if(id.equals("p1")){ Person p=new Person(); p.setName("小四"); return p; } return obj; } //調用init方法前進行處理和增強 @Override public Object postProcessBeforeInitialization(Object obj, String id) throws BeansException { // TODO Auto-generated method stub System.out.println("我是bean后置處理器的方法。init之前執行"); return obj; } } ~~~ ~~~ //beans.xml <bean id="person" class="cn.li.lesson3.Person" init-method="init" destroy-method="destroy"> <property name="name" value="趙鐵柱"></property> </bean> ~~~ ~~~ //Person類 package cn.li.lesson3; public class Person { private String name; public Person() { super(); // TODO Auto-generated constructor stub System.out.println("person構造器"); } public String getName() { return name; } public void setName(String name) { this.name = name; System.out.println("set方法被調用"); } @Override public String toString() { return "Person [name=" + name + "]"; } public void init(){ System.out.println("bean初始化"); } public void destroy(){ System.out.println("銷毀方法"); } } ~~~ ~~~ //測試類 public class MainTest { public static void main(String[] args) { // TODO Auto-generated method stub ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("cn/li/lesson3/beans.xml"); Person p = (Person) ac.getBean("person"); //System.out.println(p); ac.close(); } } ~~~ ![](https://box.kancloud.cn/6187c65e351374e076a0021a4a75a6a5_443x194.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看