spring boot简单教程(2)-@SpringBootApplication详解

pom.xml
[html]
view plain
copy

  1. <parent>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-parent</artifactId>  
  4.     <version>1.5.2.RELEASE</version>  
  5. </parent>  
  6. <dependencies>  
  7.     <dependency>  
  8.         <groupId>org.springframework.boot</groupId>  
  9.         <artifactId>spring-boot-starter-web</artifactId>  
  10.     </dependency>  
  11. </dependencies>  

Run.java
[java]
view plain
copy

  1. package hello;  
  2.   
  3. import org.springframework.boot.*;  
  4. import org.springframework.boot.autoconfigure.*;  
  5.   
  6. @SpringBootApplication  
  7. public class Run{  
  8.     public static void main(String[] args) throws Exception {  
  9.         SpringApplication.run(Run.class, args);  
  10.     }  
  11. }  

这个Run是一个单独的项目启动类,这里不应该有业务功能,上一篇的hello world业务代码应该写在一个单独的@Controller里面,和上一篇相比,这里用@SpringBootApplication替换了@EnableAutoConfiguration。

@SpringBootApplication
@EnableAutoConfiguration:只是实现自动配置一个功能,具体参考上一篇
@SpringBootApplication:是一个组合注解,包括@EnableAutoConfiguration及其他多个注解,是一个项目的启动注解
在eclipse的代码中 按 crtl+左键 点击@SpringBootApplication注解可以查看他的源码,如下

[html]
view plain
copy

  1. @Target(ElementType.TYPE)  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. @Documented  
  4. @Inherited  
  5. @SpringBootConfiguration  
  6. @EnableAutoConfiguration  
  7. @ComponentScan(excludeFilters = {  
  8.         @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),  
  9.         @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })  
  10. public @interface SpringBootApplication  

前四个注解:是元注解,用来修饰当前注解,就像public类的修饰词,没有实际功能,如果不打算写自定义注解,不需要了解
后三个注解:是真正起作用的注解,包括
@SpringBootConfiguration:当前类是一个配置类,就像xml配置文件,而现在是用java配置文件,效果是一样的
@EnableAutoConfiguration:上篇已经讲了
@ComponentScan:用注解配置实现自动扫描,默认会扫描当前包和所有子包,和xml配置自动扫描效果一样,@Filter是排除了两个系统类


@SpringBootConfiguration和@Bean


[java]
view plain
copy

  1. package hello;  
  2.   
  3. import org.springframework.boot.SpringBootConfiguration;  
  4. import org.springframework.context.annotation.Bean;  
  5.   
  6. @SpringBootConfiguration  
  7. public class Config {  
  8.     @Bean  
  9.     public String hello(){  
  10.         return “Hello World”;  
  11.     }  
  12. }  

@SpringBootConfiguration:说明这是一个配置文件类,它会被@ComponentScan扫描到

@Bean:就是在spring容器中声明了一个bean,赋值为hello world,String方法类型就是bean的类型,hello方法名是bean的id

如果是用xml配置文件来声明bean,如下图

[html]
view plain
copy

  1. <bean id=“hello” class=“String”></bean>  

SampleController.java

[java]
view plain
copy

  1. package hello;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.*;  
  5. import org.springframework.web.bind.annotation.*;  
  6.   
  7. @Controller  
  8. public class SampleController {  
  9.     @Autowired  
  10.     String hello;  
  11.       
  12.     @RequestMapping(value=“/”)  
  13.     @ResponseBody  
  14.     String test(){  
  15.       return hello;  
  16.     }  
  17.   
  18.      
  19. }  

把hello world业务功能独立出来,在这里注入了spring容器中的那个String类型的Bean,并且打印到页面

运行项目

现在的项目结构如下,共三个文件

《spring boot简单教程(2)-@SpringBootApplication详解》

通过Run.java的main方法启动项目,访问http://localhost:8080/

《spring boot简单教程(2)-@SpringBootApplication详解》

    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/ztx114/article/details/78082190
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞