<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Spring 概要文件 XML 教程 > 原文: [http://zetcode.com/spring/profilexml/](http://zetcode.com/spring/profilexml/) Spring 概要文件 XML 教程顯示了如何為 Spring 應用配置 XML 概要文件。 Spring 是用于創建企業應用的流行 Java 應用框架。 ## Spring 簡介 概要文件是為特定環境(例如開發或生產)聲明的一組命名 Bean。 可以使用 XML 或使用注解來配置配置文件。 ## Spring 配置文件示例 該應用定義了兩個配置文件:生產和開發。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ Application.java │ └───resources │ database-dev.properties │ database-prod.properties │ logback.xml └───test └───java ``` 這是項目結構。 我們有兩個屬性文件:`database-dev.properties`和`database-prod.properties`。 `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>profileex</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-dev.properties` ```java db.url=jdbc:h2:mem:testdb db.username=testuser db.password=s$cret ``` 這些是開發屬性。 `resources/database-prod.properties` ```java db.url=jdbc:postgresql://localhost/mydb db.username=user7 db.password=s$cret ``` 這些是生產屬性。 `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-${spring.profiles.active}.properties"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean> </beans> ``` `my-beans.xml`包含`dataSource` bean。 `<context:property-placeholder>`標簽基于活動的 Spring 配置文件為數據源配置屬性。 `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) { System.setProperty("spring.profiles.active", "prod"); 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 并打印其屬性。 激活屬性是通過`System.setProperty()`方法設置的。 另外,我們可以將屬性設置為 VM 變量`-Dspring.profiles.active=prod`或在 IDE 設置中。 ```java $ mvn -q -Dspring.profiles.active=dev exec:java 20:30:45.832 INFO com.zetcode.Application - Url: jdbc:h2:mem:testdb 20:30:45.832 INFO com.zetcode.Application - User name: testuser 20:30:45.832 INFO com.zetcode.Application - Password: s$cret ``` 我們在命令行上設置了配置文件的情況下運行應用 在本教程中,我們為 Spring 應用創建了開發和生產數據。 您可能也對這些相關教程感興趣: [Spring `@Bean`注解教程](/spring/bean/), [Spring 單例范圍 bean](/spring/singletonscope/) , [Spring `@ComponentScan`教程](/spring/componentscan/), [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>

                              哎呀哎呀视频在线观看