Spring Boot

核心功能

1.可直接运行jar包
2.内嵌servlet容器(如tomcat)
3.提供很多starter pom简化maven依赖加载
4.自动配置spring
5.具有生产级的应用监控
6.基于注解,而不是自动生成代码,无xml配置

优点

1·可快速构建项目并运行起来
2·与大多主流框架无配置集成
3·内嵌servlet容器
4.应用监控
5·极大提高开发效率
6·与云计算天然集成

创建springboot项目

使用maven直接创建:添加公共依赖,插件

<parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.3.0.M1</version> 
        <relativePath/> 
    </parent>

添加starter pom

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

添加插件

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

程碑版的Spring Boot配置,若使用的是正式版则不需要下面的配置。

<repositories> 
        <repository> 
            <id>spring-snapshots</id> 
            <name>Spring Snapshots</name> 
            <url>https://repo.spring.io/snapshot</url> 
            <snapshots> 
                <enabled>true</enabled> 
                </snapshots> 
        </repository> 
        <repository> 
            <id>spring-milestones</id> 
            <name>Spring Milestones</name> 
            <url>https://repo.spring.io/milestone</url> 
            <snapshots> 
                <enabled>false</enabled> 
            </snapshots> 
        </repository> 
    </repositories> 
    <pluginRepositories> 
        <pluginRepository> 
            <id>spring-snapshots</id> 
            <name>Spring Snapshots</name> 
            <url>https://repo.spring.io/snapshot</url> 
            <snapshots> 
                <enabled>true</enabled> 
            </snapshots> 
        </pluginRepository> 
        <pluginRepository> 
            <id>spring-milestones</id> 
            <name>Spring Milestones</name> 
            <url>https://repo.spring.io/milestone</url> 
            <snapshots> 
                <enabled>false</enabled> 
            </snapshots> 
        </pluginRepository> 
    </pluginRepositories> 

直接使用idea 创建

新建Spring Initializr项目

1·新建
新建Spring Initializr项目
2·填写项目信息
在这里插入图片描述
3·选择使用技术
在这里插入图片描述
4·填写项目名
在这里插入图片描述

5·项目设置为Maven项目,若无提示,可忽略该步骤
在这里插入图片描述
等待加载
6·项目结构及依赖树如图5-15所示。
在这里插入图片描述

springboot全局配置文件

application.properties

book.author=wangyunfei 
book.name =spring boot 

Bean

package com.wisely.ch6_2_3.config; 
  
import org.springframework.boot.context.properties.ConfigurationProperties; //加载配置文件
  
@Component 
@ConfigurationProperties(prefix = "author") //1 
public class AuthorSettings { 
    private String name; 
    private Long age; 
  
    public String getName() { 
        return name; 
    } 
  
    public void setName(String name) { 
        this.name = name; 
    } 
  
    public Long getAge() { 
        return age; 
    } 
  
    public void setAge(Long age) { 
        this.age = age; 
    } 
}

测试

@RestController 
@SpringBootApplication 
public class Ch623Application { 
    
    @Autowired 
    private AuthorSettings authorSettings; //1 
    
    @RequestMapping("/") 
    public String index(){ 
        return "author name is "+ authorSettings.getName()+" and author age is "+authorSettings.getAge(); 
    } 
  
    public static void main(String[] args) { 
        SpringApplication.run(Ch623Application.class, args); 
    } 
} 

运行,访问:http://localhost:8080/,效果如图6-5所示。在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值