<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 原型作用域 bean > 原文: [http://zetcode.com/spring/prototypescope/](http://zetcode.com/spring/prototypescope/) Spring Prototype 作用域 bean 教程展示了如何在 Spring 應用中使用 Prototype 作用域 bean。 Spring 是用于創建企業應用的流行 Java 應用框架。 ## Spring 原型 bean 原型 bean 每次針對該 bean 發出新請求時都會創建。 其他 bean 范圍是:單例,請求,會話,全局會話和應用。 ## Spring 原型 bean 示例 該應用將創建兩個原型范圍內的 bean,并檢查它們是否相同。 該應用是經典的 Spring 5 控制臺應用。 ```java ────src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ │ │ │ └───bean │ │ Message.java │ │ │ └───resources │ logback.xml │ my-beans.xml │ └───test └───java ``` 這是項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>prototypescopedbean</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <spring-version>5.1.3.RELEASE</spring-version> </properties> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <mainClass>com.zetcode.Application</mainClass> </configuration> </plugin> </plugins> </build> </project> ``` 在`pom.xml`文件中,我們具有基本的 Spring 依賴項`spring-core`和`spring-context`和日志記錄`logback-classic`依賴項。 `exec-maven-plugin`用于在命令行上從 Maven 執行 Spring 應用。 `resources/my-beans.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.zetcode"/> </beans> ``` 通過`context:component-scan`標簽,我們指示 Spring 在`com.zetcode`包中查找 bean。 它將找到我們唯一的`Message` bean,并用`@Component`裝飾。 `resources/logback.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <configuration> <logger name="org.springframework" level="ERROR"/> <logger name="com.zetcode" level="INFO"/> <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <Pattern>%d{HH:mm:ss.SSS} [%thread] %blue(%-5level) %magenta(%logger{36}) - %msg %n </Pattern> </encoder> </appender> <root> <level value="INFO" /> <appender-ref ref="consoleAppender" /> </root> </configuration> ``` `logback.xml`是 Logback 日志庫的配置文件。 `com/zetcode/bean/Message.java` ```java package com.zetcode.bean; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("prototype") public class Message { private String message; public String getMessage() { return message; } } ``` `Message`是由 Spring 容器管理的 Spring bean。 它具有原型范圍。 ```java @Component @Scope("prototype") public class Message { ``` `@Scope("prototype")`將 bean 的范圍設置為原型。 默認值為單例。 `com/zetcode/Application.java` ```java package com.zetcode; import com.zetcode.bean.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.GenericXmlApplicationContext; public class Application { private static final Logger logger = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { var ctx = new GenericXmlApplicationContext("my-beans.xml"); var beanA = ctx.getBean(Message.class); var beanB = ctx.getBean(Message.class); if (beanA.equals(beanB)) { logger.info("The beans are identical"); } else { logger.info("The beans are not identical"); } ctx.close(); } } ``` 這是主要的應用類。 ```java var ctx = new GenericXmlApplicationContext("my-beans.xml"); ``` 我們使用`GenericXmlApplicationContext`從`my-beans.xml`文件創建 Spring 應用上下文。 ```java var bean1 = ctx.getBean(Message.class); var bean2 = ctx.getBean(Message.class); app.run(bean1, bean2); ``` 我們從應用上下文中獲得兩個 bean,并將它們傳遞給`run()`方法進行比較。 ```java logger.info(a.getMessage()); ``` 我們從 bean 中讀取消息。 ```java if (a.equals(b)) { logger.info("The beans are the same"); } else { logger.info("The beans are not the same"); } ``` 我們測試兩個豆是否相同。 ```java $ mvn -q exec:java 21:26:03.089 [com.zetcode.Application.main()] INFO com.zetcode.Application - The beans are not identical ``` 我們運行該應用。 將`Message` bean 的范圍更改為單例并比較結果。 在本教程中,我們使用了原型 Spring bean。 您可能也對這些相關教程感興趣:經典的 Spring 應用中的 [`JdbcTemplate`](/articles/springjdbctemplate/) , [Spring 單例范圍 bean](/spring/singletonscope/) , [Spring `ClassPathResource`教程](/spring/classpathresource/), [Spring 注入列表 XML 教程](/spring/injectlistxml/), [Spring `BeanDefinitionBuilder`教程](/spring/beandefinitionbuilder/), [Spring HikariCP 教程](/articles/springhikaricp/)和 [Java 教程](/lang/java/)。
                  <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>

                              哎呀哎呀视频在线观看