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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [toc] 下面的章節介紹Spring的`AnnotationConfigApplicationContext`,這是Spring 3.0中的新功能。 這種多功能的`ApplicationContext`實現不僅可以接受`@Configuration`類作為輸入,還可以接受用`JSR-330`元數據注釋的普通`@Component`類。 `@Configuration`作為輸入,這個類注冊為bean定義,包含的所有`@Bean`也作為bean的定義. `@Component`作為輸入,這個類注冊為bean定義,包含的`@Autowired`作為依賴注入. ## 簡單的構造 與實例化`ClassPathXmlApplicationContext`時使用Spring XML文件作為輸入的方式大致相同,在實例化`AnnotationConfigApplicationContext`時,`@Configuration`類可用作輸入。 這允許完全無XML地使用Spring容器: ~~~ java public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); } ~~~ 上面提到過,不僅可以使用`@Configuration`作為輸入,也可以使用`@Component`作為輸入. ~~~java public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); } ~~~ 上面假定`MyServiceImpl`,使用`@Autowired`注入`Dependency1 和 Dependency2`. ## Building the container programmatically using register(Class<?>…) `AnnotationConfigApplicationContext`可以使用無參數構造函數實例化,然后使用`register()`方法進行配置。 以編程方式構建`AnnotationConfigApplicationContext`時,此方法特別有用。 ~~~java public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class, OtherConfig.class); ctx.register(AdditionalConfig.class); ctx.refresh(); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); } ~~~ ## Enabling component scanning with scan(String..) 為了啟用組件掃描的功能, 只需如下注解`@Configuration`類 ~~~ @Configuration @ComponentScan(basePackages = "com.acme") public class AppConfig { ... } ~~~ 有經驗的spring開發人員,會想到xml的配置 ~~~xml <beans> <context:component-scan base-package="com.acme"/> </beans> ~~~ `AnnotationConfigApplicationContext `支持掃描 ~~~ public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan("com.acme"); ctx.refresh(); MyService myService = ctx.getBean(MyService.class); } ~~~ ## Support for web applications with AnnotationConfigWebApplicationContext `AnnotationConfigApplicationContext`的`WebApplicationContext`變體可與`AnnotationConfigWebApplicationContext`一起使用。 在配置Spring `ContextLoaderListener` servlet偵聽器,Spring MVC `DispatcherServlet`等時,可以使用此實現。接下來是配置典型Spring MVC Web應用程序的`web.xml`片段。 請注意`contextClass `的context-param和init-param的使用: ~~~xml <web-app> <!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext --> <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param> <!-- Configuration locations must consist of one or more comma- or space-delimited fully-qualified @Configuration classes. Fully-qualified packages may also be specified for component-scanning --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>com.acme.AppConfig</param-value> </context-param> <!-- Bootstrap the root application context as usual using ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Declare a Spring MVC DispatcherServlet as usual --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext --> <init-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </init-param> <!-- Again, config locations must consist of one or more comma- or space-delimited and fully-qualified @Configuration classes --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>com.acme.web.MvcConfig</param-value> </init-param> </servlet> <!-- map all requests for /app/* to the dispatcher servlet --> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping> </web-app> ~~~
                  <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>

                              哎呀哎呀视频在线观看