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方法