基于Java的Spring Boot 2配置示例

x33g5p2x  于2022-10-06 转载在 Spring  
字(5.6k)|赞(0)|评价(0)|浏览(701)

在这篇文章中,我们将快速讨论如何使用基于Java的配置开发一个简单的Spring boot 2应用程序。

我们使用@Configuration和@Bean注解来开发Spring boot 2的独立内存应用程序。注意,在这个例子中我们没有使用@Service@Component注解。(基于注解的配置)

@SpringBootApplication注解表示一个配置类,它声明了一个或多个@Beanmethods,还触发了自动配置和组件扫描。这是一个方便的注解,相当于声明了@Configuration、@EnableAutoConfiguration和@ComponentScan。
阅读更多关于Spring Boot的@SpringBootApplication注解 @SpringBootApplication注解与实例您可能还对Spring(非Spring boot)基于Java的配置实例感兴趣。

我们将建立一个简单的基于maven的Spring Boot项目。让我们先来看看这个例子中使用的工具和技术。

###使用的工具和技术

  • Spring Boot - 2.0.4.RELEASE
  • JDK - 1.8或更高版本
  • Spring Framework - 5.0.8 RELEASE
  • Maven - 3.2以上
  • IDE - Eclipse或Spring Tool Suite (STS)
    ###创建、导入和Spring Boot应用程序的项目结构

让我们使用Spring Initializr在http://start.spring.io/快速创建一个Spring Boot应用程序,它是一个在线Spring Boot应用程序生成器。下载项目并在你的IDE中导入。下面是项目的结构,供你参考。

pom.xml文件

请参考下面的maven Spring Boot启动器依赖项。

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>net.guides.springboot2</groupId>
  6. <artifactId>springboot2-annotation-config</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>springboot2-annotation-config</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.4.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

在下一步,我们将创建几个服务类,这样我们就可以使用基于Java的@Bean配置注解来创建Spring Bean。

###使用@Service注解创建几个服务

注意,我们还没有使用@Service或@Component注解。

MessageService.java

  1. public interface MessageService {
  2. public void sendMsg(String message);
  3. }

EmailService.java

  1. import org.springframework.stereotype.Service;
  2. public class EmailService implements MessageService{
  3. public void sendMsg(String message) {
  4. System.out.println(message);
  5. }
  6. }

SMSService.java

  1. import org.springframework.stereotype.Service;
  2. public class SMSService implements MessageService{
  3. public void sendMsg(String message) {
  4. System.out.println(message);
  5. }
  6. }

TwitterService.java

  1. import org.springframework.stereotype.Service;
  2. public class TwitterService implements MessageService{
  3. public void sendMsg(String message) {
  4. System.out.println(message);
  5. }
  6. }

UserService.java

  1. public interface UserService {
  2. public void processMsg(String message);
  3. }

UserServiceImpl.java

  1. package net.guides.springboot2.springboot2annotationconfig.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.stereotype.Service;
  5. public class UserServiceImpl implements UserService {
  6. @Autowired
  7. @Qualifier("TwitterService")
  8. private MessageService messageService;
  9. public void setMessageService(MessageService messageService) {
  10. this.messageService = messageService;
  11. }
  12. public void processMsg(String message) {
  13. messageService.sendMsg(message);
  14. }
  15. }

在下一步,我们将使用基于Bean Java的配置注解为上述服务类创建Spring Bean。

###配置Spring beans和运行应用程序
这个spring boot应用程序有一个名为SpringBootCrudRestApplication.java的入口点Java类,有public static void main(String[] args)方法,你可以运行它来启动应用程序。

在这个文件中,我们使用了@Bean注解,以编程方式创建Bean(使用new关键字)。

  1. package net.guides.springboot2.springboot2annotationconfig;
  2. package net.guides.springboot2.springboot2javaconfig;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.annotation.Bean;
  7. import net.guides.springboot2.springboot2javaconfig.service.EmailService;
  8. import net.guides.springboot2.springboot2javaconfig.service.MessageProcessor;
  9. import net.guides.springboot2.springboot2javaconfig.service.MessageProcessorImpl;
  10. import net.guides.springboot2.springboot2javaconfig.service.MessageService;
  11. import net.guides.springboot2.springboot2javaconfig.service.SMSService;
  12. import net.guides.springboot2.springboot2javaconfig.service.TwitterService;
  13. @SpringBootApplication
  14. public class Springboot2JavaConfigApplication {
  15. @Bean(name = "emailService")
  16. public MessageService emailService() {
  17. return new EmailService();
  18. }
  19. @Bean(name = "smsService")
  20. public MessageService smsService() {
  21. return new SMSService();
  22. }
  23. @Bean(name = "twitterService")
  24. public MessageService twitterService() {
  25. return new TwitterService();
  26. }
  27. @Bean
  28. public MessageProcessor messageProcessor() {
  29. return new MessageProcessorImpl(twitterService());
  30. }
  31. public static void main(String[] args) {
  32. ApplicationContext applicationContext = SpringApplication.run(Springboot2JavaConfigApplication.class, args);
  33. MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
  34. userService.processMsg("twitter message sending ");
  35. }
  36. }

输出

相关文章