您的位置:威尼斯官方网站 > 威尼斯正规官网 > Boot开发人员总是使用@Configuration

Boot开发人员总是使用@Configuration

发布时间:2019-10-07 19:02编辑:威尼斯正规官网浏览(142)

    无数Spring Boot开辟职员总是选用@Configuration,@EnableAutoConfiguration和@ComponentScan来标明它们的主类。 由于这一个注释平时一同利用(非常是假设您遵守上述最好试行),Spring Boot提供了一个有利的@SpringBootApplication代替形式。

    @SpringBootApplication注释也等于同有时间使用@Configuration,@EnableAutoConfiguration和@ComponentScan及其暗中认可属性(重复申明,SpringBoot是怎么管理的?):

    package com.example.myproject;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication /* same as */ //@Configuration //@EnableAutoConfiguration //@ComponentScanpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
    

    NOTE

    @SpringBootApplication alias 关联了解说: @EnableAutoConfiguration和@ComponentScan的习性。

    源码:

    /* * Copyright 2012-2015 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.boot.autoconfigure;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Indicates a {@link Configuration configuration} class that declares one or more * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience * annotation that is equivalent to declaring {@code @Configuration}, * {@code @EnableAutoConfiguration} and {@code @ComponentScan}. * * @author Phillip Webb * @since 1.2.0 */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@Configuration@EnableAutoConfiguration@ComponentScanpublic @interface SpringBootApplication { /** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */ Class<?>[] exclude() default {};}
    

    由此说,你也足以一贯本人定于申明,来封装一些常用的笺注。

    package com.restfiddle;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Created by jack on 2017/3/23. * * @author jack * @date 2017/03/23 */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@Configuration@EnableAutoConfiguration@ComponentScanpublic @interface RestBoot { Class<?>[] exclude() default {};}
    

    代码中得以那样用:

    /* * Copyright 2014 Ranjan Kumar * * 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 com.restfiddle;import org.springframework.boot.SpringApplication;/** * 应用入口类 main函数 *///@Configuration//@EnableAutoConfiguration//@ComponentScan({"com.restfiddle"})//@SpringBootApplication@RestBootpublic class RestFiddleApplication { public RestFiddleApplication() { } public static void main(String[] args) throws Exception { SpringApplication.run(RestFiddleApplication.class, args); }}
    

    本文由威尼斯官方网站发布于威尼斯正规官网,转载请注明出处:Boot开发人员总是使用@Configuration

    关键词: