Springboot注解@ServletComponentScan和@ComponentScan(轉)


一、SpringBoot中使用Servlet
在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通過@WebServlet、@WebFilter、@WebListener注解自動注冊,無需其他代碼。

1.在入口Application類上加入注解@ServletComponentScan

package com.hui;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

2.新建Servlet類,繼承HttpServlet並且加入注解@WebServlet(name=“TestServlet”,urlPatterns="/test")

package com.hui.qiang;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="TestServlet",urlPatterns="/test")
public class TestServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        System.out.println("doGet");
    }
}

3.之后運行代碼,在瀏覽器地址欄輸入http://localhost:8080/test,若看到控制台打印doGet,則表示Servlet創建成功。
注意:
(1)如果不加@ServletComponentScan注解則會報404即找不到頁面,控制台也掃描不到我們配置的servlet:/test,即無法被映射
(2)如果Application類和Servlet類不在同一包下,則@ServletComponentScan需要添加相應的路徑,如Application類在包com.hui.xiao下,則寫為@ServletComponentScan(“com.hui.xiao”)或@ServletComponentScan(“com.hui”)

 

 

 

 

二、Spring, Spring Boot中的@ComponentScan注解用法介紹
@ComponentScan
如果你理解了ComponentScan,你就理解了Spring.
Spring是一個依賴注入(dependency injection)框架。所有的內容都是關於bean的定義及其依賴關系。
定義Spring Beans的第一步是使用正確的注解-@Component或@Service或@Repository.
但是,Spring不知道你定義了某個bean除非它知道從哪里可以找到這個bean.
ComponentScan做的事情就是告訴Spring從哪里找到bean

由你來定義哪些包需要被掃描。一旦你指定了,Spring將會將在被指定的包及其下級的包(sub packages)中尋找bean
下面分別介紹在Spring Boot項目和非Spring Boot項目(如簡單的JSP/Servlet或者Spring MVC應用)中如何定義ComponentScan

注:@ComponentScan的不同寫法
1.@ComponentScan({“com.xiao.hui”,“com.xiao.qiang”})或@ComponentScan(basePackages = {“com.xiao.hui”,“com.xiao.qiang”})
2.@ComponentScan(“com.xiao”)或@ComponentScan(value = “com.xiao”)或@ComponentScan(basePackages = { “com.xiao” })
3.@ComponentScan(basePackageClasses=要掃描類.class所在位置的包) 意思是要掃描哪個類所在的包,如@ComponentScan(basePackageClasses=hehe.class),這種寫法不如上面的那種寫法好有局限性

Spring Boot項目
總結:
1.SpringBoot在寫啟動類的時候如果不使用@ComponentScan指明對象掃描范圍,默認指掃描當前啟動類所在的包里的對象,如果你的其他包都在使用了@SpringBootApplication注解的主類所在的包及其下級包,則你什么都不用做,SpringBoot會自動幫你把其他包都掃描了。為了方便,我一般都把主類放在了所有類的上一級包中,如項目所有的class文件都放在了包com.beauty的下級包中,則把spring boot的主類放在包com.beauty下。
2.如果當前啟動類沒有包,則在啟動時會報錯:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package錯誤,因為啟動類不能直接放在main/java文件夾下,必須要建一個包把它放進去或者使用@ComponentScan指明要掃描的包。
3.如果你有一些bean所在的包,不在主類的包及其下級包,那么你需要手動加上@ComponentScan注解並指定那個bean所在的包。

舉個栗子,看下面定義的類:

package com.xiao.qiang.qianming;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = 
                SpringApplication.run(SpringbootApplication.class, args);

        for (String name : applicationContext.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }
}

類SpringbootApplication在com.xiao.qiang.qianming包下,這個類使用了@SpringBootApplication注解,該注解定義了Spring將自動掃描包com.xiao.qiang.qianming及其子包下的bean
如果你項目中所有的類都定義在上面的包及其子包下,那你不需要做任何事。
但假如你一個類定義在包com.xiao.hui下,則你需要將這個新包也納入掃描的范圍,有兩個方案可以達到這個目的。

方案1
定義@ComponentScan(“com.xiao”),這么做掃描的范圍擴大到整個父包com.xiao

@ComponentScan("com.xiao")
@SpringBootApplication
public class SpringbootIn10StepsApplication {

方案2
定義分別掃描兩個包

@ComponentScan({"com.xiao.hui","com.xiao.qiang"})
@SpringBootApplication
public class SpringbootIn10StepsApplication {

非Spring Boot項目
在非Spring Boot項目中,我們必須顯式地使用@ComponentScan注解定義被掃描的包,可以通過XML文件在應用上下文中定義或在Java代碼中對應用上下文定義

Java代碼方式:

@ComponentScan({"com.xiao.package1","com.xiao.package2"})
@Configuration
public class SpringConfiguration {

注:@Configuration 和@ComponentScan注解背后會做什么呢?
其實很簡單,@ComponentScan告訴Spring 哪個packages 的用注解標識的類 會被spring自動掃描並且裝入bean容器。
例如,如果你有個類用@Controller注解標識了,那么,如果不加上@ComponentScan,自動掃描該controller,那么該Controller就不會被spring掃描到,更不會裝入spring容器中,因此你配置的這個Controller也沒有意義。
類上的注解@Configuration 是最新的用注解配置spring,也就是說這是個配置文件,和原來xml配置是等效的,只不過現在用java代碼進行配置了 加上一個@Configuration注解就行了,是不是很方便,不需要那么繁瑣的xml配置了,這樣基於注解的配置,可讀性也大大增高了。

XML文件方式:
<context:component-scan base-package=“com.xiao.package1, com.xiao.package2” />
 

三、使用@ComponentScan自動掃描組件實例
包掃描會掃描只要標注了@Controller,@Service,@Repository,@Component這四個注解都會被掃描到容器中。
1、@Controller 控制器(注入服務)
用於標注控制層,相當於struts中的action層
2、@Service 服務(注入dao)
用於標注服務層,主要用來進行業務的邏輯處理
3、@Repository(實現dao訪問)
用於標注數據訪問層,也可以說用於標注數據訪問組件,即DAO組件.
4、@Component (把普通pojo實例化到spring容器中,相當於配置文件中的<bean id="" class=""/>)
泛指各種組件,就是說當我們的類不屬於各種歸類的時候(不屬於@Controller、@Services等的時候),我們就可以使用@Component來標注這個類。
案例:<context:component-scan base-package=”com.*”>
上面的這個例子是引入Component組件的例子,其中base-package表示為需要掃描的所有子包。
有一篇不錯的文章(Spring注解詳解):https://blog.csdn.net/xyh820/article/details/7303330/

 

 

 新增控制層的java類:TestController和HelloController

import org.springframework.stereotype.Controller;

@Controller
public class TestController {

}
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @RequestMapping(value="/hello", method = RequestMethod.GET)
    public String hello() {
         return "Hello, Spring Boot";
    }
}

新建一個業務邏輯層類:TestService

import org.springframework.stereotype.Service;

@Service
public class TestService {

}

新建一個Person:

public class Person {
    public Person(String string, int i) {
    }
}

主方法測試:
spring boot:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(value = "com.hui")
public class Application {

    public static void main(String[] args) {
        ApplicationContext applicationContext = 
                SpringApplication.run(Application.class, args);

        for (String name : applicationContext.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }
}

非spring boot:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.hui.entity.Person;

@Configuration
@ComponentScan(value = "com.hui")
public class ComponentTest {

    @Bean
    public Person getPerson() {
        return new Person("百度好帥", 10000);
    }
    
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(ComponentTest.class);
        String[] beanNames = applicationContext.getBeanDefinitionNames();
        for (String bean : beanNames) {
            System.out.println(bean);
        }
    }
}

運行Application:
控制台掃描到了/hello,即映射成功

 

 

 

 

 把Application注釋掉運行ComponentTest:

 

參考:
https://blog.csdn.net/Lamb_IT/article/details/80918704
https://jingyan.baidu.com/article/7908e85cc6930daf481ad2b6.html

————————————————
版權聲明:本文為CSDN博主「小強簽名設計」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/m0_37739193/article/details/85097477


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM