<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 Boot `@PostConstruct` > 原文: [http://zetcode.com/springboot/postconstruct/](http://zetcode.com/springboot/postconstruct/) Spring Boot `@PostConstruct`教程顯示了如何在 Spring 應用中使用`@PostConstruct`注解。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## `@PostConstruct` `@PostConstruct`是在依賴注入完成以執行任何初始化之后需要執行的方法上使用的注解。 ## Spring Boot `@PostConstruct`示例 以下應用演示了`@PostConstruct`的用法。 它使用注解創建兩個日志方法,在初始化它們的 bean 之后調用它們。 這些消息在應用運行后顯示。 應用本身向客戶端發送一條消息。 從配置文件中讀取文本消息。 ```java $ tree . ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── Application.java │ │ ├── controller │ │ │ └── MyController.java │ │ └── service │ │ ├── IMessageService.java │ │ └── MessageService.java │ └── resources │ ├── application.properties │ └── static │ └── index.html └── 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>SpringBootPostConstruct</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 這是 Maven `pom.xml`文件。 `spring-boot-starter-parent`是父 POM,它為使用 Maven 構建的應用提供依賴關系和插件管理。 `spring-boot-starter-web`是使用 Spring MVC 構建 Web(包括 RESTful)應用的入門工具。 它使用 Tomcat 作為默認的嵌入式容器。 `spring-boot-maven-plugin`將 Spring 應用打包到可執行的 JAR 或 WAR 歸檔文件中。 `application.properties` ```java my.msg=Hello there spring.main.banner-mode=off logging.level.org.springframework=ERROR ``` `application.properties`是 Spring Boot 中的主要配置文件。 我們設置了一個`message`屬性,該屬性將由應用返回給客戶端。 我們關閉 Spring 橫幅并減少 Spring 框架的日志記錄量。 `MyController.java` ```java package com.zetcode.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.zetcode.service.IMessageService; import javax.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @RestController public class MyController { private static final Logger logger = LoggerFactory.getLogger(MyController.class); @Autowired IMessageService messageService; @RequestMapping(value = "/getMessage") public String getMessage() { String message = messageService.getMessage(); return message; } @PostConstruct public void doLog() { logger.info("Info message in MyController"); } } ``` 這是`MyController`。 它向客戶端發送一條消息。 ```java @RequestMapping(value = "/getMessage") public String getMessage() { String message = messageService.getMessage(); return message; } ``` 從消息服務生成一條消息,并將其返回給客戶端。 ```java @PostConstruct public void doLog() { logger.info("Info message in MyController"); } ``` `doLog()`方法用`@PostConstruct`注解修飾。 在初始化`MyController` bean 之后調用該方法。 它記錄一條簡單的參考消息。 `IMessageService.java` ```java package com.zetcode.service; public interface IMessageService { public String getMessage(); } ``` `IMessageService`包含`getMessage()`合約方法。 `MessageService.java` ```java package com.zetcode.service; import javax.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class MessageService implements IMessageService { private static final Logger logger = LoggerFactory.getLogger(MessageService.class); @Value(value = "${my.msg}") private String message; @Override public String getMessage() { return message; } @PostConstruct public void doLog() { logger.info("Info message in MessageService"); } } ``` `MessageService`包含`getMessage()`方法的實現。 ```java @Value(value = "${my.msg}") private String message; ``` 從帶有`@Value`注解的`application.properties`文件中讀取返回給客戶端的消息,并將其設置為`message`字段。 ```java @Override public String getMessage() { return message; } ``` `getMessage()`返回消息字符串。 ```java @PostConstruct public void doLog() { logger.info("Info message in MessageService"); } ``` `MessageService`還包含用`@PostConstruct`裝飾的`doLog()`方法。 在 bean 初始化之后調用它。 `index.html` ```java <!DOCTYPE html> <html> <head> <title>Home page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <p> <a href="getMessage">Get Message</a> </p> </body> </html> ``` 這是主頁。 它包含一個獲取消息的鏈接。 `Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`是設置 Spring Boot 應用的入口。 `@SpringBootApplication`注解啟用自動配置和組件掃描。 ```java $ mvn -q spring-boot:run ... 2017-12-14 21:35:30.788 INFO 10665 --- [main] com.zetcode.service.MessageService : Info message in MessageService 2017-12-14 21:35:30.791 INFO 10665 --- [main] com.zetcode.controller.MyController : Info message in MyController ... ``` 應用運行后,我們可以在控制臺上看到這兩個日志消息。 在本教程中,我們展示了如何在 Spring 應用中使用`@PostConstruct`注解。 您可能也對相關教程感興趣: [Java Servlets 教程](/articles/javaservlet/), [Spring Boot `@Controller`教程](/springboot/controller/), [Spring Boot `@ExceptionHandler`教程](/springboot/exceptionhandler/), [Spring Boot 上傳文件](/springboot/uploadfile/), [Spring Boot `@PathVariable`教程](/springboot/pathvariable/), [Spring Boot `@RequestParam`教程](/springboot/requestparam/), [Spring Boot `@ResponseBody`教程](/springboot/responsebody/), [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>

                              哎呀哎呀视频在线观看