-
一、Springboot:請求入口
-
-
@SpringBootApplication
-
@EnableAspectJAutoProxy
-
@EnableScheduling
-
@EnableTransactionManagement
-
public class Application {
-
-
public static void main(String[] args) {
-
-
SpringApplication.run(Application.class, args);
-
-
}
-
}
1、@SpringBootApplication注解
-
@Target(ElementType.TYPE)
-
@Retention(RetentionPolicy.RUNTIME)
-
@Documented
-
@Inherited
-
@SpringBootConfiguration
-
@EnableAutoConfiguration
-
@ComponentScan(excludeFilters = {
-
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
-
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
-
public @interface SpringBootApplication {
-
……
-
}
復合注解:包含@EnableAutoConfiguration、@ComponentScan、@SpringBootConfiguration
a 、@EnableAutoConfiguration:也是復合注解、借助@Import將所有符合自動配置條件的bean定義加載到Spring ioc 中。幫助springboot應用將所有符合條件的@configuration配置都加載到當前spring ioc。
@Import注解的使用。這個注解幫助我們將多個配置文件(可能是按功能分,或是按業務分)導入到單個主配置中,以避免將所有配置寫在一個配置中
b、@ComponentScan:主要作用掃描當前包及其子包下被@Component,@Controller,@Service,@Repository注解標記的類並納入到spring容器中進行管理。是以前的<context:component-scan>(以前使用在xml中使用的標簽,用來掃描包配置的平行支持)。可通過@ComponentScan 的basepackage等屬性來指定掃描范圍。(@SpringBootApplication(scanBasePackages = "com.ucredit")。如果不指定默認spring框架實現,從聲明@ComponentScan所在的類的package進行掃描。所以springboot的啟動類最好放在root package下。
c、@SpringBootConfiguration:繼承自@Configuration,二者功能也一致,標注當前類是配置類,並會將當前類內聲明的一個或多個以@Bean注解標記的方法的實例納入到spring容器中,並且實例名就是方法名。@SpringBootConfiguration注解類相當於spring配置bean的xml文件。
d、@Configuration
Spring是給予IOC的,在4.0之前的版本,通常都是程序依賴上下文xml文件來管理bean,盡管有了掃描配置后相對簡單,然而java配置的方式不同於xml,通過注解能夠更簡單。下面我們通過這兩種方式比較下。
- xml中bean的定義
-
<beans>
-
<bean id="course" class="demo.Course">
-
<property name="module" ref="module"/>
-
</bean>
-
<bean id="module" class="demo.Module">
-
<property name="assignment" ref="assignment"/>
-
</bean>
-
<bean id="assignment" class="demo.Assignment" />
-
</beans>
- 注解配置類
-
@Configuration
-
public class AppContext {
-
@Bean
-
public Course course() {
-
Course course = new Course();
-
course.setModule( module());
-
return course;
-
}
-
@Bean
-
public Module module() {
-
Module module = new Module();
-
module.setAssignment(assignment());
-
return module;
-
}
-
@Bean
-
public Assignment assignment() {
-
return new Assignment();
-
}
-
}
@Configuration,該注解配置在類上,告知Spring這個類是一個擁有bean定義和依賴項的配置類。@Bean注釋用於定義Bean,該注解位於實例化bean並設置依賴項的方法上。方法名默認通beanId活默認名稱相同,該方法返回類型是Spring注冊的bean。總體來說就是告訴Spring容器加載這個配置,相對於xml,這個注解就是將*.xml配置進web.xml
e、@Import
-
@Target(ElementType.TYPE)
-
@Retention(RetentionPolicy.RUNTIME)
-
@Documented
-
public @interface Import {
-
-
/**
-
* {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
-
* or regular component classes to import.
-
*/
-
Class<?>[] value();
-
-
}
- @Import 與xml配置方式下的 作用一樣。支持導入的類型有:
一個或多個擁有 @Configuration 注解的配置類 - ImportSelector 接口的實現類
- ImportBeanDefinitionRegistrar 的實現類
1)、如果Import注解中Class為ImportSelector子類,通過invokeAwareMethods(selector)設置aware值,如果類型為 DeferredImportSelector則添加到deferredImportSelectors集合中,待前面的parser.parse(configCandidates)
方法中processDeferredImportSelectors()處理;如果不是,則執行selectImports方法,將獲取到的結果遞歸調用 processImports,解析selectImports得到的結果
2)、如果Import注解中Class為ImportBeanDefinitionRegistrar子類,則添加到importBeanDefinitionRegistrars中,注 意該部分的數據在執行完parser.parse(configCandidates)后調用this.reader.loadBeanDefinitions(configClasses)解 析,否則執行配置信息的解析操作。
-
public interface Car {
-
-
public void print();
-
}
-
@Component
-
public class Toyota implements Car {
-
-
@Override
-
public void print() {
-
// TODO Auto-generated method stub
-
System.out.println( "I am Toyota");
-
}
-
-
}
-
-
@Component
-
public class Volkswagen implements Car {
-
-
@Override
-
public void print() {
-
// TODO Auto-generated method stub
-
System.out.println( "I am Volkswagen");
-
}
-
-
}
-
-
@Configuration
-
public class JavaConfigA {
-
-
@Bean(name="volkswagen")
-
public Car getVolkswagen(){
-
return new Volkswagen();
-
}
-
}
-
-
@Configuration
-
public class JavaConfigB {
-
@Bean(name="toyota")
-
public Car getToyota(){
-
return new Toyota();
-
}
-
}
-
-
@Configuration
-
@Import({JavaConfigA.class,JavaConfigB.class})
-
public class ParentConfig {
-
//Any other bean definitions
-
}
-
public class ContextLoader {
-
-
public static void main (String args[]){
-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ParentConfig.class);
-
Car car = (Toyota)context.getBean("toyota");
-
car.print();
-
car = (Volkswagen)context.getBean("volkswagen");
-
car.print();
-
context.close();
-
}
-
}
-
-
2、@EnableAspectJAutoProxy 注解
表示開啟AOP代理自動配置,如果配@EnableAspectJAutoProxy表示使用cglib進行代理對象的生成;
3、@EnableScheduling
@EnableScheduling是通過@Import將Spring調度框架相關的bean定義都加載到IoC容器。