基于Java的Spring Boot 2配置示例

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

在这篇文章中,我们将快速讨论如何使用基于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启动器依赖项。

<?xmlversion="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>net.guides.springboot2</groupId>
    <artifactId>springboot2-annotation-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot2-annotation-config</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
        </plugins>
    </build>
   </project>

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

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

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

MessageService.java

public interface MessageService {
    public void sendMsg(String message);
}

EmailService.java

import org.springframework.stereotype.Service;

public class EmailService implements MessageService{
    public void sendMsg(String message) {
        System.out.println(message);
    }
}

SMSService.java

import org.springframework.stereotype.Service;

public class SMSService implements MessageService{

    public void sendMsg(String message) {
        System.out.println(message);
    }
}

TwitterService.java

import org.springframework.stereotype.Service;

public class TwitterService implements MessageService{

    public void sendMsg(String message) {
        System.out.println(message);
    }
}

UserService.java

public interface UserService {
    public void processMsg(String message);
}

UserServiceImpl.java

package net.guides.springboot2.springboot2annotationconfig.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

public class UserServiceImpl implements UserService {

    @Autowired
    @Qualifier("TwitterService")
    private MessageService messageService;

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void processMsg(String message) {
        messageService.sendMsg(message);
    }
}

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

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

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

package net.guides.springboot2.springboot2annotationconfig;

package net.guides.springboot2.springboot2javaconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import net.guides.springboot2.springboot2javaconfig.service.EmailService;
import net.guides.springboot2.springboot2javaconfig.service.MessageProcessor;
import net.guides.springboot2.springboot2javaconfig.service.MessageProcessorImpl;
import net.guides.springboot2.springboot2javaconfig.service.MessageService;
import net.guides.springboot2.springboot2javaconfig.service.SMSService;
import net.guides.springboot2.springboot2javaconfig.service.TwitterService;

@SpringBootApplication
public class Springboot2JavaConfigApplication {

    @Bean(name = "emailService")
    public MessageService emailService() {
        return new EmailService();
    }

    @Bean(name = "smsService")
    public MessageService smsService() {
        return new SMSService();
    }

    @Bean(name = "twitterService")
    public MessageService twitterService() {
        return new TwitterService();
    }

    @Bean
    public MessageProcessor messageProcessor() {
        return new MessageProcessorImpl(twitterService());
    }

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(Springboot2JavaConfigApplication.class, args);
        MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
        userService.processMsg("twitter message sending ");
    }
}

输出

相关文章

微信公众号

最新文章

更多