<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 控制反轉 在使用new來創建對象的時候,我們可以主動的控制去創建什么樣的類,但是在使用工廠模式和IOC容器的時候,我們不能控制具體獲得了什么類,我們把創建類的權利交給了工廠或IOC,控制權發生了轉移,這個就叫做控制反轉.這樣就降低了耦合度. ![](https://box.kancloud.cn/3d3e0ca1bb98a8290b1da432fb3457af_1347x532.png) ## applicationContext.xml 在src下面創建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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <beans> <!--id:可以為任意值,但是在整個配置文件唯一.class:要實例化類的全限定名. scope可以選擇singleton或者prototype.singleton是單實例.配置文件只要一加載,就會創建對象,放在spring容器中,當所有人過來訪問spring容器要的時候(getBean()),所有人用的都是同一個對象. prototype是多實例,配置文件加載,不創建對象,當每個人過來getBean()的時候,getBean()一次創建一次放在容器. --> <bean id="user" class="com.like.domain.User" scope="prototype"/> </beans> </beans> ~~~ 代碼: ~~~ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //加載配置文件 User user = (User)applicationContext.getBean("user"); //獲取bean User user1 = context.getBean("user", User.class); //第二種方式,不用強轉 System.out.println(user); System.out.println(user1); ~~~ ## 對象銷毀時機 * singleton:當spring容器關閉的時候. * protype:長時間不用就自動被垃圾回收機制回收了. ## bean初始化方法和銷毀方法 指定兩個時機方法,可以隨意命名. ~~~ <bean id="user" class="com.like.domain.User" scope="prototype" init-method="init" destroy-method="destroy"/> ~~~ bean: ~~~ public class User { private String name; private Integer age; public void init() { System.out.println("init"); } public void destroy() { System.out.println("destroy"); } public User() { System.out.println("create"); } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } ~~~ 結果: ``` create //創建 init //初始化 銷毀時機不確定 ``` 如果是singleton:配置文件加載就執行初始化方法,容器關閉調用銷毀方法. ## import 標簽 用于導入外部的配置文件. ~~~ <import resource="web_bean.xml"/> ~~~
                  <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>

                              哎呀哎呀视频在线观看