1.Spring核心複習:控制反轉和動態代理web
a) IOC:由於以前使用對象必須建立,運用了Spring以後,就不用再建立,直接依賴注入就好了.spring
b) AOP: 就是不影響正常執行過程的先後加入額外的邏輯。好比權限,日誌等,該執行的業務邏輯正常執行知識能夠進行權限的判斷核日誌記錄。編程
2.Spring註解編程IOC:不用在配置文件裏面進行bean的配置,直接使用註解a)@Configuration:加了這個註解的類就至關於傳統的一個applicationContext-xxx.xml
@Bean:在標註了@Configuration的類裏面的方式上面打上@bean就至關於在applicationContext-xxx.xml配置的一個<bean id=」userDao」 class=」cn.itsource.dao.UserDao」>數組
Dao的名字默認就是方法名,若是想改方法名使用@Bean(「beanName」)springboot
測試:app
//2 註解方式不須要配置文件,那bean怎麼配置呢? 獲取容器的時候原來制定配置文件,如今制定配置類框架
ApplicationContext context = new AnnotationConfigApplicationContextspring-boot
(MainConfig.class);測試
UserDao userDao = context.getBean(UserDao.class);代理
System.out.println(userDao.loadUser(1L));
String[] beanNamesForType = context.getBeanNamesForType(UserDao.class);
for (String beanName : beanNamesForType) {
System.out.println(beanName);
}
a) @ComponentScan掃描bean
b) @ ComponentScans高級語法
c) @ComponentScans(value = {
@ComponentScan(
value = "cn.itsource"
// ,excludeFilters = { //排除 //excludeFilters = Filter[] 排除過濾器
// @ComponentScan.Filter(type = FilterType.ANNOTATION
// ,classes = {Controller.class})
// }
,includeFilters = {//includeFilters = Filter[] 包含過濾器
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {
Controller.class
})
}
,useDefaultFilters = false //關閉默認所有掃描includeFilters才能生效
)
})
d) @Import註解:做用就是一個配置類引用另外一個配置類的信息
e) FactoryBean方式註冊bean
@Configuration //告訴spring這是一個註解類
@Import({RedColor.class, GreenColor.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class MainConfig {
@Bean
public PersonFactoryBean personFactoryBean(){
return new PersonFactoryBean();
}
}
f) @Condition註解
總結:建立bean的方式
方式1:@ComponentScan+註解(@Controller+@Service+@Repository+@Compont)-本身建立的bean
方式2:@Bean 別人的bean
方式3:@Import(快速向容器中註冊一個bean)
1)@Import(要導入的組件),名稱就是累的全限定名
2)ImportSelector:導入選擇器,返回須要導入組件類的全限定名數組-springboot底層用的多
3)ImportBeanDefinitionRegistrar:經過bean定義註冊器手動項目spring中容器中註冊
方式4:FactoryBean的方式,返回的是getObject的類實例-和其餘框架集成是用的多
3.SpringBoot入門
a) 建立項目,到入依賴包
<parent>
artifactId>springboot_parent</artifactId>
<groupId>cn.itsource.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
b) 建立啓動類
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration //開啓自動配置功能的註解//@SpringBootApplication比@ EnableAutoConfiguration強
public class Example {
@RequestMapping("/hello")
public String home(){
return "Hello World!";
}
//啓動類
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);//啓動Springboot應用
}
}
頁面訪問:localhost:8080/hello
輸出 hello word.
a):pom文件理解
1.父模塊: spring-boot-starter-parent依賴spring-boot-dependencies
2.啓動器模塊: spring-boot-starter-web依賴spring-boot-starters模塊
b):@SpringBootApplication:主配置類註解@SpringBootConfiguration:Springboot配置類
@EnableAutoConfiguration:開啓自動配置註解功能
@ComponentScan;掃描包註解
啓動類執行的流程:@ SpringBootApplication-->
@EnableAutoConfiguration--> @Import({AutoConfigurationImportSelector.class})選擇器選擇是根據是哪個選擇器運行的
Springboot的執行流程
啓動:
每一個SpringBoot程序都有一個主入口,也就是main方法,main裏面調用SpringApplication.run()啓動整個spring-boot程序,該方法所在類須要使用@SpringBootApplication註解,以及@ImportResource註解(if need),@SpringBootApplication包括三個註解,功能以下:@EnableAutoConfiguration:SpringBoot根據應用所聲明的依賴來對Spring框架進行自動配置
@SpringBootConfiguration(內部爲@Configuration):被標註的類等於在spring的XML配置文件中(applicationContext.xml),裝配全部bean事務,提供了一個spring的上下文環境
@ComponentScan:組件掃描,可自動發現和裝配Bean,默認掃描SpringApplication的run方法裏的Booter.class所在的包路徑下文件,因此最好將該啓動類放到根包路徑下
1.首先進入run方法,在run方法中去建立了一個SpringApplication實例,在該構造方法內,咱們能夠發現其調用了一個初始化的initialize方法