Spring Boot注解 之 @ComponentScan

本文的示例代码参考ComponentScan

目录

  • 开始

  • @ComponentScan

  • @Component

  • @Controller

  • @Service @Repository

  • basePackages

开始

spring init -dweb --build gradle ComponentScan

更多参考 Spring Boot开发 之 开发环境

# cd ComponentScan
vim src/main/java/com/example/ComponentScan/HelloController.java
package com.example.ComponentScan;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class HelloController {
    @RequestMapping("/")
    public String hello() {
        return "HelloController";
    }
}

更多参考 @RequestBody and @ResponseBody annotations in Spring

  • 测试
./gradlew bootrun

curl localhost:8080 # 返回"HelloController"

@ComponentScan

@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan

vim src/main/java/com/example/ComponentScan/DemoApplication.java
package com.example.ComponentScan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • 测试
./gradlew bootrun

curl localhost:8080 # 返回"HelloController"

@Component

vim src/main/java/com/example/ComponentScan/HelloComponent.java
package com.example.ComponentScan;

import org.springframework.stereotype.Component;

@Component
public class HelloComponent {
    public void hello() {
        System.out.println("HelloComponent");
    }
}
vim src/main/java/com/example/ComponentScan/HelloController.java
# 省略了未修改代码
public class HelloController {
    @Autowired
    private HelloComponent component;

    @RequestMapping("/")
    public String hello() {
        component.hello();
        return "HelloController";
    }
}
  • 测试
./gradlew bootrun

curl localhost:8080 # 打印"HelloComponent" + 返回"HelloController"

@Controller

sed -i "" '/@ComponentScan/d' src/main/java/com/example/ComponentScan/DemoApplication.java
  • 测试
./gradlew bootrun

curl localhost:8080 # {"status":404,"error":"Not Found"}

@Service @Repository

| Annotation | Meaning                                             |
+------------+-----------------------------------------------------+
| @Component | generic stereotype for any Spring-managed component |
| @Repository| stereotype for persistence layer                    |
| @Service   | stereotype for service layer                        |
| @Controller| stereotype for presentation layer (spring-mvc)      |

更多参考 What's the difference between @Component, @Repository & @Service annotations in Spring?

basePackages

mkdir -p src/main/java/com/example/AnotherPackage
vim src/main/java/com/example/AnotherPackage/AnotherComponent.java
package com.example.AnotherPackage;

import org.springframework.stereotype.Component;

@Component
public class AnotherComponent {
    public void hello() {
        System.out.println("AnotherComponent");
    }
}
vim src/main/java/com/example/ComponentScan/HelloController.java
# 省略了未修改代码
public class HelloController {
    @Autowired
    private HelloComponent component;

    @Autowired
    private AnotherComponent anotherComponent;

    @RequestMapping("/")
    public String hello() {
        component.hello();
        anotherComponent.hello();
        return "HelloController";
    }
}
./gradlew bootrun
***************************
APPLICATION FAILED TO START
***************************

Description:

Field anotherComponent in com.example.ComponentScan.HelloController required a bean of type 'com.example.AnotherPackage.AnotherComponent' that could not be found.

Action:

Consider defining a bean of type 'com.example.AnotherPackage.AnotherComponent' in your configuration.

FAILURE: Build failed with an exception.
sed -i "" 's/@ComponentScan/@ComponentScan(basePackages = { "com.example.ComponentScan", "com.example.AnotherPackage" })/g' src/main/java/com/example/ComponentScan/DemoApplication.java
  • 测试
./gradlew bootrun

curl localhost:8080 # 打印"HelloComponent AnotherComponent" + 返回"HelloController"

参考

  • A Guide to Spring Framework Annotations

你可能感兴趣的:(Spring Boot注解 之 @ComponentScan)