@SpringBootApplication-exclude和掃描並裝配其他包下的bean(@AliasFor)


1、exclude

不裝配指定bean

@SpringBootApplication(exclude={com.ebc.User.class})

 

2、scanBasePackages

package com.ebc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//scanBasePackages 后邊的值,是一個數組。即可指定多個不同的包
@SpringBootApplication(scanBasePackages = "com.yst")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

 

package com.yst;

import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@Configuration
public class WebConfiguration {
    /**
     * 瀏覽器地址欄中輸入:http://localhost:8080/hello-world,回車后,輸出:Hello,World
     * @return
     */
    @Bean
    public RouterFunction<ServerResponse> helloworld() {
        return route(GET("/hello"),request->ok().body(Mono.just("Hello,遙遠2"),String.class));
    }

    /**
     * 在spring boot應用啟動后回調
     * @param context
     * @return
     */
    @Bean
    public ApplicationRunner runner(WebServerApplicationContext context) {
        return args -> {
            System.out.println("當前WebServer實現類為:"+context.getWebServer().getClass().getName());
        };
    }
}

運行http://localhost:8080/hello

輸出:Hello,遙遠2

說明,成功。

 


免責聲明!

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



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