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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring `context:property-placeholder`教程 > 原文: [http://zetcode.com/spring/propertyplaceholder/](http://zetcode.com/spring/propertyplaceholder/) Spring `context:property-placeholder`教程展示了如何使用`context:property-placeholder`標記來外部化 Spring 應用中的屬性。 Spring 是用于創建企業應用的流行 Java 應用框架。 ## Spring 的`context:property-placeholder` `context:property-placeholder`標簽用于外部化單獨文件中的屬性。 它會自動配置`PropertyPlaceholderConfigurer`,從而替換`${}`占位符,這些占位符針對指定的屬性文件(作為 Spring 資源位置)進行解析。 ## Spring `context:property-placeholder`示例 該應用使用`context:property-placeholder`來配置數據源的屬性。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ Application.java │ └───resources │ database.properties │ 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>propertyplaceholder</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> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</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`,`spring-jdbc`和日志記錄`logback-classic`依賴項。 `exec-maven-plugin`用于在命令行上從 Maven 執行 Spring 應用。 `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} %blue(%-5level) %magenta(%logger{36}) - %msg %n </Pattern> </encoder> </appender> <root> <level value="INFO" /> <appender-ref ref="consoleAppender" /> </root> </configuration> ``` `logback.xml`是 Logback 日志庫的配置文件。 `resources/database.properties` ```java db.url=jdbc:h2:mem:testdb db.username=testuser db.password=s$cret ``` 這些值在`database.properties`文件中外部化。 這種方法比將值直接放入 XML 文件中更為靈活。 `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:property-placeholder location="classpath:database.properties"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean> </beans> ``` `context:property-placeholder`指定屬性文件的位置; 在我們的例子中,它是任何類路徑目錄中的`database.properties`文件。 ```java <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean> ``` 定義了`dataSource` bean。 它通過`${}`語法從屬性文件中獲取其值。 `com/zetcode/Application.java` ```java package com.zetcode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.GenericXmlApplicationContext; import org.springframework.jdbc.datasource.SimpleDriverDataSource; 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 dataSource = (SimpleDriverDataSource) ctx.getBean("dataSource"); logger.info("Url: {}", dataSource.getUrl()); logger.info("User name: {}", dataSource.getUsername()); logger.info("Password: {}", dataSource.getPassword()); ctx.close(); } } ``` 這是主要的應用類。 它檢索`dataSource` bean 并打印其屬性。 ```java $ mvn -q exec:java 11:27:43.790 INFO com.zetcode.Application - Url: jdbc:h2:mem:testdb 11:27:43.790 INFO com.zetcode.Application - User name: testuser 11:27:43.790 INFO com.zetcode.Application - Password: s$cret ``` 我們運行該應用。 在本教程中,我們展示了如何使用`context:property-placeholder`來外部化屬性。 您可能也對這些相關教程感興趣: [Spring `@Qualifier`注解教程](/spring/qualifier/), [Spring 單例范圍 bean](/spring/singletonscope/) , [Spring C-命名空間教程](/spring/cnamespace/), [Spring `BeanDefinitionBuilder`教程](/spring/beandefinitionbuilder/), [Spring bean 引用教程](/spring/beanreference/)和 [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>

                              哎呀哎呀视频在线观看