springboot学习(一)搭建helloword

跟着网上的大牛学习一下springboot开发。
这里使用开发工具为idea,选择spring initializr来构建Maven项目:
这里写图片描述
修改Group及Aritfact:
这里写图片描述
选择web,左侧可以根据需要,勾选所需要依赖,由于今天刚开始搭建,暂时只选择web,之后一路next,idea会自动帮我们下载依赖,搭建工程
这里写图片描述
项目结构:
这里写图片描述
pom文件自动生成的依赖:

<?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.zhr</groupId>
    <artifactId>springdemo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>springdemo1</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-tomcat</artifactId>
            <scope>provided</scope>
        </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>

项目所依赖jar包:
这里写图片描述
接下来创建requstmapping:
这里写图片描述
接下来启动项目,在pom文件中我们使用了内置的tomcat,因此直接运行main方法即可:
这里写图片描述
运行后发现,localhost:8080,出现404页面,内置tomcat并没有随之启动,检查发现问题出现在pom文件中,需要将provided注释掉,tomcat才能启动成功。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--<scope>provided</scope>-->  
        </dependency>

注释之后,重新运行main方法,启动成功:
这里写图片描述
访问localhost:8080:
这里写图片描述
接下来,看一下使用到的几个标签。其中@RequestMapping @RestController作用和springmvc中作用一致,@SpringBootApplication注解,先看一下源码:

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    @AliasFor(
        annotation = EnableAutoConfiguration.class,
        attribute = "exclude"
    )
    Class<?>[] exclude() default {};

    @AliasFor(
        annotation = EnableAutoConfiguration.class,
        attribute = "excludeName"
    )
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};
}

通过源码,可以发现,@SpringBootApplication 被 @Configuration、@EnableAutoConfiguration、@ComponentScan 注解所修饰,换句话说,@SpringBootApplication相当于默认使用这三个注解。这三个注解通过网上的一些资料总结如下。

@Configuration:表示这个类中定义了Bean,会把这个类中bean加载到spring IOC中,等同于@Service,@Component等注解

@ComponentScan:如果不设置basePackage的话 默认会扫描包的所有类,所以最好还是写上basePackage ,减少加载时间。默认扫描*/.class路径 比如这个注解在com.wuhulala 下面 ,那么会扫描这个包下的所有类还有子包的所有类,比如com.wuhulala.service包的应用

@EnableAutoConfiguration:spring boot特有注解。会在你开启某些功能的时候自动配置 ,这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用,并对Spring进行相应地设置。如果发现启用了不想要的自动配置项,你可以使用@EnableAutoConfiguration注解的exclude属性禁用它们:

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

如果该类不在classpath中,你可以使用该注解的excludeName属性,并指定全限定名来达到相同效果。

@EnableAutoConfiguration(excludeName=”com.xxx.xxx.xx.class”)

@SpringBootApplication注解也提供了用于自定义@EnableAutoConfiguration和@ComponentScan属性的别名(aliases)。可以使用它们相关的属性设置 ,@SpringBootApplication(exclude=SpringBoot1Application.class,excludeName=”“,scanBasePackageClasses=SpringBoot1Application.class,scanBasePackages=”“)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值