- SpringBoot的@SpringBootApplicatio
- SpringBoot中@SpringBootApplicatio
- 微服务spring cloud---注解@SpringCloud
- ERROR 1552 --- [ main] o.s.b.d.L
- 2020-03-31 @SpringBootApplicatio
- 1) Springboot打包方式启动分析-自定义类加载器
- Java程序员力荐的PDF文档——深入浅出springboot,
- SpringBoot2.0文章汇总目录,java多线程教程文章汇
- 【SpringBoot2.0系列05】SpringBoot之整合
- 【SpringBoot2.0系列04】SpringBoot之使用
1. 首先说明下@SpringBootApplication注解
@SpringBootApplication的作用在于声明SpringBoot工程启动类,它包含三个含义:
@Configuration
@EnableAutoConfiguration
@ComponentScan
我这里主要说明@ComponentScan
,另外两个注解的用法可查看关于如何在其他包中写controller和简单介绍@SpringBootApplication
“组件扫描”注解的作用主要在于定义组件扫描的包路径,用法@ComponentScan(value = "com.example")
,这样SpringBoot项目在启动过程中会自动扫描com.example
包下所有的组件,即所有的@Component
,@Service
,@Controller
,@Repository
等,即@Component
及@Component
以下的所有注解。
如果value未定义,则只扫描当前类所在的包,即启动类所在的包。
@Service
,@Controller
,@Repository
都在@Component
注解下,可查看SpringBoot源码:
image.png
可以看出@Controller
本身就是@Component
,它是@Component
的“别名”
Controller注解源码:
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.stereotype;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* Indicates that an annotated class is a "Controller" (e.g. a web controller).
*
* <p>This annotation serves as a specialization of {@link Component @Component},
* allowing for implementation classes to be autodetected through classpath scanning.
* It is typically used in combination with annotated handler methods based on the
* {@link org.springframework.web.bind.annotation.RequestMapping} annotation.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 2.5
* @see Component
* @see org.springframework.web.bind.annotation.RequestMapping
* @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
2. 在启动类以外的包定义控制器
在了解了以上的关于@SpringBootApplication的作用后,如果想要在启动类所在包以外定义控制器的话需要在启动类重写@ComponentScan:
@MapperScan("com.example.back.mapper")
@SpringBootApplication
@ComponentScan(value = "com.example")
@Service
public class BackApplication {
public static ConfigurableApplicationContext context = null;
public static void main(String[] args) {
context =
SpringApplication.run(BackApplication.class, args);
}
}
其中BackApplication
类在com.example.back
包下,我需要新定义的控制器在com.example.mgr
下:

测试:

网友评论