1.JavaConfig理论:
1.1什么是JavaConfig:它区别于以往的xml和注解的形式管理bean,它是以java代码的方式对bean进行管理。
1.2为什么要学习javaConfig理论:因为它是SpringBoot的基础,SpringBoot底层基于它。
1.3Spring发展史:
1.x阶段:xml配置
2.x阶段:注解配置(标记注解,扫描注解)
3.x-4.x阶段:javaConfig&SpringBoot
2.JavaConfig操作:
2.1基本概念:
配置类:@Configuration代替了xml配置
@Bean代替了
@Configuration //相当于原来的applicationContext-x.xml
public class IocConfig {
/**
* <bean id="myBean" class="cn.itsource._04iocjavaconfig_.MyBean"></bean>
* @return
*/
@Bean
public MyBean myBean(){
return new MyBean();
}
}
2.2bean扫描:
@ComponentScan:单包扫描
@Configuration //相当于原来的applicationContext-x.xml
@ComponentScan("com.penny.componentScan")
public class IocConfig {
//@Bean
}
@ComponentScans:多包扫描
//2 配置多个包
//2.1 加多个@ComponentScan
//@ComponentScan("cn.itsource._05componentScan_.controller")
//@ComponentScan("cn.itsource._05componentScan_.service")
//2.2 @ComponentScans
/*@ComponentScans(value = {
@ComponentScan("cn.itsource._05componentScan_.controller"),
@ComponentScan("cn.itsource._05componentScan_.service")
})*/
public class IocConfig {
//@Bean
}
excludeFilters&includeFilters:排除或包含:
//3 排除包里面某些类的bean,只包含某类注解的bean 其他的三个注解本身也是一个@Component
@ComponentScans(value = {
// @ComponentScan(value = "cn.itsource._05componentScan_",excludeFilters = {
// //排除加了Component注解的bean
// @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
// })
//包含
@ComponentScan(
value = "cn.itsource._05componentScan_",
includeFilters = {
//排除加了Component注解的bean
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Service.class})
}
,useDefaultFilters = false //关闭默认全部扫描includeFilters才生效
)
})
public class IocConfig {
//@Bean
}
2.3bean详情:
@Scope(“singleton”) //singleton单例(默认值) prototype多例
@Lazy //懒加载(默认),用到的时候才去创建对象,并且只对单例模式有效,因为多例是你要的时候才给你创建
2.3.1注入方式:
方式1:创建对象直接设置值(不推荐)
@Configuration
public class MainConfig {
@Autowired
private OtherBean otherBean;
public MyBean xxx(){
// 方式1:创建对象直接设置值,没有在spring中,不较注入
OtherBean otherBean = new OtherBean();
MyBean myBean = new MyBean();
myBean.setOtherBean(otherBean);
return myBean;
}
@Bean
public OtherBean otherBean(){
return new OtherBean();
}
}
方式2:直接调用方法(不推荐)
@Configuration
public class MainConfig {
@Autowired
private OtherBean otherBean;
public MyBean xxx(){
MyBean myBean = new MyBean();
myBean.setOtherBean(otherBean());
return myBean;
}
@Bean
public OtherBean otherBean(){
return new OtherBean();
}
}
方式3:对注入的bean进行设置值(不推荐)
@Configuration
public class MainConfig {
@Autowired
private OtherBean otherBean;
public MyBean xxx(){
MyBean myBean = new MyBean();
myBean.setOtherBean(otherBean);
return myBean;
}
@Bean
public OtherBean otherBean(){
return new OtherBean();
}
}
方式4 通过构造函数进行注入 (推荐使用)
@Configuration
public class MainConfig {
@Autowired
private OtherBean otherBean;
@Bean
public MyBean yyy(OtherBean otherBean){
return new MyBean(otherBean);
}
@Bean
public OtherBean otherBean(){
return new OtherBean();
}
}
2.4根据条件注册bean-condition:
@Conditional标签区分系统(该标签可加载方法或者类上):
// 按照条件来注册bean,区分操作系统 windows linux
@Configuration
@Conditional(value = WindowsCondition.class) //提升为类级别,里面的方法都会继承,类级别满足了才会判断方法级别
public class MainConfig {
@Bean //windows
//@Conditional(value = WindowsCondition.class)
public MyBean myBeanForWindows(){
return new MyBean();
}
@Bean //linux //继承了父类的条件并且共存
@Conditional(value = LinuxCondition.class)
public MyBean myBeanForLinux(){
return new MyBean();
}
}
Windows系统:
public class WindowsCondition implements Condition {
//matches 匹配 如果是true表示注册,否则不注册
// 1 conditionContext条件上下文
// 2 AnnotatedTypeMetadata
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//获取类加载器
ClassLoader classLoader = conditionContext.getClassLoader();
//获取spring的容器
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
//获取bean定义注册器
BeanDefinitionRegistry registry = conditionContext.getRegistry();
//获取环境
Environment environment = conditionContext.getEnvironment();
String osName = environment.getProperty("os.name");
System.out.println(osName+"jjjj");
return osName.contains("Windows");
}
}
Linux系统:
public class LinuxCondition implements Condition {
//matches 匹配
// 1 conditionContext条件上下文
// 2 AnnotatedTypeMetadata
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//获取类加载器
ClassLoader classLoader = conditionContext.getClassLoader();
//获取spring的容器
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
//获取bean定义注册器
BeanDefinitionRegistry registry = conditionContext.getRegistry();
//获取环境
Environment environment = conditionContext.getEnvironment();
String osName = environment.getProperty("os.name");
System.out.println(osName+"iiiii");
return osName.contains("Linux");
}
}
MyBean:
public class MyBean {
public MyBean() {
System.out.println("create .....");
}
public MyBean(OtherBean otherBean) {
this.otherBean = otherBean;
}
private OtherBean otherBean;
public OtherBean getOtherBean() {
return otherBean;
}
public void setOtherBean(OtherBean otherBean) {
this.otherBean = otherBean;
}
}
2.5import导入bean:
1.直接导入:
// 通过import导入
// 1 导入特定类的对象
// 2 通过ImportSelector
// 3 ImportBeanDefinitionRegistra
@Configuration
@Import({GreenColor.class,YellowColor.class})
public class MainConfig {
@bean....
}
2.ImportSelector选择器导入
public class MyImportSelector implements ImportSelector {
//你要注册类全限定名数组
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
//此处传入全限定类名
return new String[]{"cn.itsource._08import_.GrayColor","cn.itsource._08import_.PurpleColor"};
}
}
3.ImportBeanDefinitionRegistrar注册器导入
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
BeanDefinitionRegistry beanDefinitionRegistry) {
beanDefinitionRegistry.registerBeanDefinition("xxxColor",new RootBeanDefinition(XxxColor.class));
}
}
最后通过MainConfig将直接导入与选择器,注册器的一同导入
@Configuration
@Import({GreenColor.class,YellowColor.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
public class MainConfig {
}
2.6使用FactoryBean注册组件:
此处以注册Person.class为例:
public class PersonFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
return new Person();
}
@Override
public Class<?> getObjectType() {
return Person.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
@Configuration
public class MainConfig {
//直接通过bean名称获取的是Factorybean所创建的对象而不是PersonFactoryBean
//如果想要获取PersonFactoryBean,通过&name
@Bean
public PersonFactoryBean person(){
return new PersonFactoryBean();
}
}
3.SpringBoot
3.1步骤分析:
一 创建项目
parent
dependency
二 创建springboot项目并且启动
1)任意类加上@SpringBootApplication
2)Main函数启动springboot的应用
三 写一个Contorller来测试
HelloConroller
3.2pom配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itsource</groupId>
<artifactId>sprinboot-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类:
@SpringBootApplication //标识为一个springboot的项目
public class App {
public static void main(String[] args) {
//启动一个应用
SpringApplication.run(App.class,args);
}
}
controller类:
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "hello";
}
}