• 投稿

基于Java的Spring Boot 2配置示例

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

在这篇文章中,我们将快速讨论如何使用基于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 ");
    }
}

输出

  • 关注
  • 举报

相关文章

    查看更多
    微信公众号

    热门标签

    更多
    Java query python Node 开发语言 request Util 数据库 Table 后端 算法 Logger Message Element Parser

    最新文章

    更多
    • 2023 Spring常见面试题50道
      浏览(1475) 发布于 2023-01-15
    • 2022 Spring精选面试题50道
      浏览(1148) 发布于 2023-01-15
    • 在Linux中安装和运行Spring Boot服务指南
      浏览(640) 发布于 2022-10-25
    • Spring Data JPA@ManyToOne注解
      浏览(1133) 发布于 2022-10-25
    • 在Spring Boot中访问命令行参数
      浏览(864) 发布于 2022-10-25

    目录

    • pom.xml文件
      • MessageService.java
      • EmailService.java
      • SMSService.java
      • TwitterService.java
      • UserService.java
      • UserServiceImpl.java
    • 输出

    4617作文网梦里一直在哭周公解梦周末可交易股票吗周公解梦梦见盖房为科技公司起名大全免费姓名测试起名网权女孩起名字下周有哪些新股上市交易周公解梦黑血脐带绕颈两周容易顺产吗cad算量命令鼠年吴姓起名大全周易旅卦关于简约的店铺起名起名字缺土用什么字周公解梦梦见烧纸钱姓氏曾起名字周易测试姻缘荣字起公司名周易怎么看财运店铺起名叫什么门业好女宝起名大全2019洋气景洪成人高考高起专报考名单起名怎么查生辰八字潮汕起名字大全测名字起是什么好不好艾公司起名字周公解梦塔罗牌国内周易起国内周易起名网阿果达起名网周易解梦大全查询淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻让美丽中国“从细节出发”清明节放假3天调休1天男子给前妻转账 现任妻子起诉要回网友建议重庆地铁不准乘客携带菜筐月嫂回应掌掴婴儿是在赶虫子重庆警方辟谣“男子杀人焚尸”国产伟哥去年销售近13亿新的一天从800个哈欠开始男孩疑遭霸凌 家长讨说法被踢出群高中生被打伤下体休学 邯郸通报男子持台球杆殴打2名女店员被抓19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警两大学生合买彩票中奖一人不认账德国打算提及普京时仅用姓名山西省委原副书记商黎光被逮捕武汉大学樱花即将进入盛花期今日春分张家界的山上“长”满了韩国人?特朗普谈“凯特王妃P图照”王树国3次鞠躬告别西交大师生白宫:哈马斯三号人物被杀代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了房客欠租失踪 房东直发愁倪萍分享减重40斤方法“重生之我在北大当嫡校长”槽头肉企业被曝光前生意红火手机成瘾是影响睡眠质量重要因素考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼网友洛杉矶偶遇贾玲呼北高速交通事故已致14人死亡西双版纳热带植物园回应蜉蝣大爆发男孩8年未见母亲被告知被遗忘张立群任西安交通大学校长恒大被罚41.75亿到底怎么缴沈阳一轿车冲入人行道致3死2伤奥运男篮美国塞尔维亚同组周杰伦一审败诉网易国标起草人:淀粉肠是低配版火腿肠外国人感慨凌晨的中国很安全男子被流浪猫绊倒 投喂者赔24万杨倩无缘巴黎奥运男子被猫抓伤后确诊“猫抓病”春分“立蛋”成功率更高?记者:伊万改变了国足氛围奥巴马现身唐宁街 黑色着装引猜测

    4617作文网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化