Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | About

2.3. SpringApplication

		

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ComponentScan({"cn.netkiller.controller"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
		
		

2.3.1. @SpringBootApplication

@SpringBootApplication 是 @Configuration, @EnableAutoConfiguration 跟 @ComponentScan 的集合。

			
@SpringBootApplication
			
			
2.3.1.1. 排除 @EnableAutoConfiguration 加載項
				
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)				
				
				

2.3.2. @EnableAutoConfiguration

exclude 排除配置,下面例子是排除 DataSource配置

			
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
			
			

2.3.3. @ComponentScan

@ComponentScan 注入會掃瞄 @Controller 與 @RestController

			
@ComponentScan
@ComponentScan({"cn.netkiller.controller"})
@ComponentScan({"cn.netkiller.controller", "cn.netkiller.rest"})
			
			

2.3.4. @EntityScan 實體掃瞄

			
@EntityScan("common.domain")
			
			

2.3.5. @EnableJpaRepositories

掃瞄 Jpa 倉庫

			
@EnableJpaRepositories("common.domain")			
			
			

2.3.6. CharacterEncodingFilter

			
	public @Bean Filter characterEncodingFilter() {
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("UTF-8");
		characterEncodingFilter.setForceEncoding(true);
		return characterEncodingFilter;
	}			
			
			

2.3.7. 隱藏 Banner

隱藏 Spring Boot Banner

				. ____ _ __ _ _
				/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
				( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
				\\/ ___)| |_)| | | | | || (_| | ) ) ) )
				' |____| .__|_| |_|_| |_\__, | / / / /
				=========|_|==============|___/=/_/_/_/
				:: Spring Boot :: (v1.3.0.RELEASE)
			
			
public static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application.class);
    app.setShowBanner(false);
    app.run(args);
}
			
			

2.3.8. CORS 跨域請求

			
    public @Bean WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api/**");
            }
        };
    }			
			
			
			
 	@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedOrigins("*");
            }
        };
    }			
			
			

2.3.9. 實體與倉庫掃瞄

			
@EntityScan(basePackages = { "cn.netkiller.model" }) 
@EnableJpaRepositories(basePackages = { "cn.netkiller.repository" })			
			
			

2.3.10. 列出 Beans

			
package cn.netkiller;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
@EnableMongoRepositories
@EnableJpaRepositories
@EnableScheduling
public class Application {

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

		ApplicationContext ctx = SpringApplication.run(Application.class, args);

		System.out.println("Let's inspect the beans provided by Spring Boot:");

		String[] beanNames = ctx.getBeanDefinitionNames();
		Arrays.sort(beanNames);
		for (String beanName : beanNames) {
			System.out.println(beanName);
		}

	}
}