美文网首页
SpringBoot的@SpringBootApplicatio

SpringBoot的@SpringBootApplicatio

作者: dongzhensong | 来源:发表于2020-03-10 10:19 被阅读0次

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下:

image.png

测试:


image.png

相关文章

网友评论

      本文标题:SpringBoot的@SpringBootApplicatio

      本文链接:https://www.haomeiwen.com/subject/guuidhtx.html