java开发必备技能之Spring常用注解(重点)

news2024/9/24 5:30:05

参考了尚硅谷注解版,注解版后面的源码没看,雷神讲的太散了

Spring注解

AnnotationConfigApplicationContext

组件添加相关注解

1、@Configuration+@Bean (基础)

1-1 XML文件方式实现组件添加
public class Person { 
   
   private String name;
    
   private Integer age;
   
   private String nickName;
   
   public String getNickName() {
      return nickName;
   }
   public void setNickName(String nickName) {
      this.nickName = nickName;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Integer getAge() {
      return age;
   }
   public void setAge(Integer age) {
      this.age = age;
   }
   
   public Person(String name, Integer age) {
      super();
      this.name = name;
      this.age = age;
   }
   public Person() {
      super();
      // TODO Auto-generated constructor stub
   }
   @Override
   public String toString() {
      return "Person [name=" + name + ", age=" + age + ", nickName=" + nickName + "]";
   }
}
beans.xml-配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    //指明扫描那些路径下的bean到spring容器中
   <context:component-scan base-package="com.atguigu" use-default-filters="false"></context:component-scan>
   <bean id="person" class="com.atguigu.bean.Person">
      <property name="age" value="18"></property>
      <property name="name" value="zhangsan"></property>
   </bean>
</beans>
MainTest
public class MainTest {
   
   @SuppressWarnings("resource")
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
		Person bean = (Person) applicationContext.getBean("person");
		System.out.println(bean);
	}
}

输出

Person [name=zhangsan, age=18, nickName=null]
1-2 注解方式
//配置类==配置文件
@Configuration  //告诉Spring这是一个配置类
public class MainConfig {
   
   //给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id(就是bean的名字),在这里就是person01
   @Bean
   public Person person01(){
      return new Person("lisi", 20);
   }

}

或者以下面的这种方式

//配置类==配置文件
@Configuration  //告诉Spring这是一个配置类
public class MainConfig {
   
   //这里bean的name就是person
    @Bean("person")
	public Person person01(){
		return new Person("lisi", 20);
	}

}

@ComponentScans(告诉spring把那些位置的类扫描到spring容器中)

//配置类==配置文件
@Configuration  //告诉Spring这是一个配置类
@ComponentScans(
      value = {
            @ComponentScan(value="com.atguigu",includeFilters = {
/*                @Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
                  @Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}),*/
                  @Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class})
            },useDefaultFilters = false)   
      }
      )
//@ComponentScan  value:指定要扫描的包
//excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件
//includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件
//FilterType.ANNOTATION:按照注解
//FilterType.ASSIGNABLE_TYPE:按照给定的类型;
//FilterType.ASPECTJ:使用ASPECTJ表达式
//FilterType.REGEX:使用正则指定
//FilterType.CUSTOM:使用自定义规则
public class MainConfig {
   
   //给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
   @Bean("person")
   public Person person01(){
      return new Person("lisi", 20);
   }

}
自定义TypeFilter指定包扫描规则(结合上面ComponentScans value值看)
public class MyTypeFilter implements TypeFilter {

   /**
    * metadataReader:读取到的当前正在扫描的类的信息
    * metadataReaderFactory:可以获取到其他任何类信息的
    */
   @Override
   public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
         throws IOException {
      //获取当前类注解的信息
      AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
      //获取当前正在扫描的类的类信息
      ClassMetadata classMetadata = metadataReader.getClassMetadata();
      //获取当前类资源(类的路径)
      Resource resource = metadataReader.getResource();
      
      String className = classMetadata.getClassName();
      System.out.println("--->"+className);
      if(className.contains("er")){
         return true;
      }
      return false;
   }

}

@Scope

@Configuration
public class MainConfig2 {
   
	/**
	 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE   任何环境都可以使用
	 * @see ConfigurableBeanFactory#SCOPE_SINGLETON   任何环境都可以使用
	 * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST  request    只能在web容器里用
	 * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION	 sesssion   只能在web容器里用
	 * 
	 * @Scope:调整作用域
	 * prototype:多实例的:ioc容器启动并不会去调用方法创建对象放在容器中。
	 * 					每次获取的时候才会调用方法创建对象;
	 * singleton:单实例的(默认值):ioc容器启动会调用方法创建对象放到ioc容器中。
	 * 			以后每次获取就是直接从容器(map.get())中拿,
	 * request:同一次请求创建一个实例
	 * session:同一个session创建一个实例
	 *
	 * 默认是单实例的
	 * 
	 */
	@Scope("prototype")
	@Lazy
	@Bean("person")
	public Person person(){
		System.out.println("给容器中添加Person....");
		return new Person("张三", 25);
	}

}

@Lazy

@Configuration
public class MainConfig2 {
   
   /**
    * 
    * 懒加载:
    *        单实例bean:默认在容器启动的时候创建对象;
    *        懒加载:容器启动不创建对象。第一次使用(获取)Bean创建对象,并初始化;
    * 
    */
   @Lazy
   @Bean("person")
   public Person person(){
      System.out.println("给容器中添加Person....");
      return new Person("张三", 25);
   }

@Conditional(读源码必备-重点)

MainConfig2
//类中组件统一设置。满足当前条件,这个类中配置的所有bean注册才能生效;
@Conditional({WindowsCondition.class})
@Configuration
public class MainConfig2 {
   /**
    * @Conditional({Condition}) : 按照一定的条件进行判断,满足条件给容器中注册bean
    * 
    * 如果系统是windows,给容器中注册("bill")
    * 如果是linux系统,给容器中注册("linus")
    */
   
   @Bean("bill")
   public Person person01(){
      return new Person("Bill Gates",62);
   }
   
   @Conditional(LinuxCondition.class)
   @Bean("linus")
   public Person person02(){
      return new Person("linus", 48);
   }
}

在这里插入图片描述

LinuxCondition
//判断是否linux系统
public class LinuxCondition implements Condition {

   /**
    * ConditionContext:判断条件能使用的上下文(环境)
    * AnnotatedTypeMetadata:注释信息
    */
   @Override
   public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
      // TODO是否linux系统
      //1、能获取到ioc使用的beanfactory
      ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
      //2、获取类加载器
      ClassLoader classLoader = context.getClassLoader();
      //3、获取当前环境信息
      Environment environment = context.getEnvironment();
      //4、获取到bean定义的注册类
      BeanDefinitionRegistry registry = context.getRegistry();
      
      String property = environment.getProperty("os.name");
      
      //可以判断容器中的bean注册情况,也可以给容器中注册bean
      boolean definition = registry.containsBeanDefinition("person");
      if(property.contains("linux")){
         return true;
      }
      
      return false;
   }
WindowsCondition
//判断是否windows系统
public class WindowsCondition implements Condition {

   @Override
   public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
      Environment environment = context.getEnvironment();
      String property = environment.getProperty("os.name");
      if(property.contains("Windows")){
         return true;
      }
      return false;
   }
}

@Import 导入组件,id默认是组件的全类名

MainConfig2
@Configuration
@Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
//@Import导入组件,id默认是组件的全类名
public class MainConfig2 {
   
   /**
    * 给容器中注册组件;
    * 1)、包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)[只能注册自己写的类]
    * 2)、@Bean[导入的第三方包里面的组件]
    * 3)、@Import[快速给容器中导入一个组件]
    *        1)、@Import(要导入到容器中的组件);容器中就会自动注册这个组件,id默认是全类名
    *        2)、ImportSelector:返回需要导入的组件的全类名数组;
    *        3)、ImportBeanDefinitionRegistrar:手动注册bean到容器中
    */
   @Bean
   public ColorFactoryBean colorFactoryBean(){
      return new ColorFactoryBean();
   }
}
MyImportSelector
//自定义逻辑返回需要导入的组件
public class MyImportSelector implements ImportSelector {

   //返回值,就是到导入到容器中的组件全类名
   //AnnotationMetadata:@Import引入MyImportSelector的类的所有注解信息
   @Override
   public String[] selectImports(AnnotationMetadata importingClassMetadata) {
      //importingClassMetadata.get
      //方法不要返回null值,不然会报错
      return new String[]{"com.atguigu.bean.Blue","com.atguigu.bean.Yellow"};
   }

}
MyImportBeanDefinitionRegistrar
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

   /**
    * AnnotationMetadata:当前类的注解信息
    * BeanDefinitionRegistry:BeanDefinition注册类;
    *        把所有需要添加到容器中的bean;调用
    *        BeanDefinitionRegistry.registerBeanDefinition手工注册进来
    */
   @Override
   public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
      
      boolean definition = registry.containsBeanDefinition("com.atguigu.bean.Red");
      boolean definition2 = registry.containsBeanDefinition("com.atguigu.bean.Blue");
      if(definition && definition2){
         //指定Bean定义信息;(Bean的类型,Bean。。。)
         RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
         //注册一个Bean,指定bean名
         registry.registerBeanDefinition("rainBow", beanDefinition);
      }
   }

}
public class Color {
   
   private Car car;

   public Car getCar() {
      return car;
   }

   public void setCar(Car car) {
      this.car = car;
   }

   @Override
   public String toString() {
      return "Color [car=" + car + "]";
   }
   
}
public class Blue {
   
   public Blue(){
      System.out.println("blue...constructor");
   }
   
   public void init(){
      System.out.println("blue...init...");
   }
   
   public void detory(){
      System.out.println("blue...detory...");
   }
   
}

FactoryBean

MainConfig2
@Configuration
public class MainConfig2 {
  
   /**
    * 给容器中注册组件;
    * 1)、包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)[自己写的类]
    * 2)、@Bean[导入的第三方包里面的组件]
    * 3)、@Import[快速给容器中导入一个组件]
    *        1)、@Import(要导入到容器中的组件);容器中就会自动注册这个组件,id默认是全类名
    *        2)、ImportSelector:返回需要导入的组件的全类名数组;
    *        3)、ImportBeanDefinitionRegistrar:手动注册bean到容器中
    * 4)、使用Spring提供的 FactoryBean(工厂Bean);
    *        1)、默认获取到的是工厂bean调用getObject创建的对象
    *        2)、要获取工厂Bean本身,我们需要给id前面加一个&
    *           &colorFactoryBean
    *
    * 虽然这里装配的是ColorFactoryBean,但实际上beand的类型是Color
    */
   @Bean
   public ColorFactoryBean colorFactoryBean(){
      return new ColorFactoryBean();
   }

}
ColorFactoryBean
//创建一个Spring定义的FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {

   //返回一个Color对象,这个对象会添加到容器中
   @Override
   public Color getObject() throws Exception {
      // TODO Auto-generated method stub
      System.out.println("ColorFactoryBean...getObject...");
      return new Color();
   }

   @Override
   public Class<?> getObjectType() {
      // TODO Auto-generated method stub
      return Color.class;
   }

   //是单例?
   //true:这个bean是单实例,在容器中保存一份
   //false:多实例,每次获取都会创建一个新的bean;
   @Override
   public boolean isSingleton() {
      // TODO Auto-generated method stub
      return false;
   }

}
IOCTest
public class IOCTest {
   AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
   
   
   @Test
   public void testImport(){
      printBeans(applicationContext);
      Blue bean = applicationContext.getBean(Blue.class);
      System.out.println(bean);
      
      //工厂Bean获取的是调用getObject创建的对象
      Object bean2 = applicationContext.getBean("colorFactoryBean");   
      System.out.println("bean的类型:"+bean2.getClass()); //pos_1   输出:bean的类型:class com.atguigu.bean.Color
      
      Object bean4 = applicationContext.getBean("&colorFactoryBean");
      System.out.println(bean4.getClass()); //pos_2  输出:class com.atguigu.bean.ColorFactoryBean
   }
   
   private void printBeans(AnnotationConfigApplicationContext applicationContext){
      String[] definitionNames = applicationContext.getBeanDefinitionNames();
      for (String name : definitionNames) {
         System.out.println(name);
      }
   }
}

输出:

//前面无关的输出省略

colorFactoryBean
ColorFactoryBean...getObject...
bean的类型:class com.atguigu.bean.Color
class com.atguigu.bean.ColorFactoryBean

生命周期(重点)

PS : 这个有点不容易理解,建议先记住,后期结合查看源码会更加清楚

@Bean指定初始化和销毁方法

  • 1、指定初始化和销毁方法,通过@Bean指定init-method和destroy-method;
  • 2、通过让Bean实现InitializingBean(定义初始化逻辑),DisposableBean(定义销毁逻辑);
IOCTest_LifeCycle

后面的几个用的都是这个测试类

public class IOCTest_LifeCycle {
   
   @Test
   public void test01(){
      //1、创建ioc容器
      AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
      System.out.println("容器创建完成...");
      //applicationContext.getBean("car");
      //关闭容器
      applicationContext.close();
   }

}
MainConfigOfLifeCycle

这个类中的注释一定要仔细的看看,讲的非常好
重点在解释一下这句话:《bean的生命周期:bean创建—初始化----销毁的过程》,这个是spring给容器中加入bean时,整个创建完成一个bean的过程,首先是bean创建(其实就是new 一个bean,也就是实例化出来对象,注意这个时候,还没有给bean中的属性赋值),第二步是初始化(这个初始化的过程就是给刚才实例化出来的对象中的属性赋值,以及init方法的执行等),最后这个bean不被使用了,就会被销毁了,执行销毁的方法

package com.atguigu.config;

import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import com.atguigu.bean.Car;

/**
 * bean的生命周期:
 *        bean创建---初始化----销毁的过程
 * 容器管理bean的生命周期;
 * 我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
 * 
 * 构造(对象创建)
 *        单实例:在容器启动的时候创建对象
 *        多实例:在每次获取的时候创建对象
 * 初始化:
 * 		对象创建完成,并赋值好,调用初始化方法。。。
 * BeanPostProcessor.postProcessAfterInitialization
 * 销毁:
 * 		单实例:容器关闭的时候
 * 		多实例:容器不会管理这个bean;容器不会调用销毁方法;
 * 
 * 1)、指定初始化和销毁方法;
 *        通过@Bean指定init-method和destroy-method;
 * @author lfy
 *
 */
@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {
   
   //@Scope("prototype")
   @Bean(initMethod="init",destroyMethod="detory")
   public Car car(){
      return new Car();
   }

}
@Component
public class Car {
   
   public Car(){
      System.out.println("car constructor...");
   }
   
   public void init(){
      System.out.println("car ... init...");
   }
   
   public void detory(){
      System.out.println("car ... detory...");
   }

}
输出
car constructor...
car ... init...
容器创建完成
car ... detory...

InitializingBean和DisposableBean

MainConfigOfLifeCycle
/**
 * bean的生命周期:
 *        bean创建---初始化----销毁的过程
 * 容器管理bean的生命周期;
 * 我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
 * 
 * 构造(对象创建)
 *        单实例:在容器启动的时候创建对象
 *        多实例:在每次获取的时候创建对象
 * 
 * BeanPostProcessor.postProcessBeforeInitialization
 * 初始化:
 *        对象创建完成,并赋值好,调用初始化方法。。。
 * BeanPostProcessor.postProcessAfterInitialization
 * 销毁:
 *        单实例:容器关闭的时候
 *        多实例:容器不会管理这个bean;容器不会调用销毁方法;
 * 
 * 
 * 遍历得到容器中所有的BeanPostProcessor;挨个执行beforeInitialization,
 * 一但返回null,跳出for循环,不会执行后面的BeanPostProcessor.postProcessorsBeforeInitialization
 * 
 * BeanPostProcessor原理
 * populateBean(beanName, mbd, instanceWrapper);给bean进行属性赋值
 * initializeBean
 * {
 * applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
 * invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化
 * applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
 *}
 * 
 * 
 * 
 * 1)、指定初始化和销毁方法;
 *        通过@Bean指定init-method和destroy-method;
 * 2)、通过让Bean实现InitializingBean(定义初始化逻辑),
 *              DisposableBean(定义销毁逻辑);
 * 
 * @author lfy
 *
 */
@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {
   
   //@Scope("prototype")
   @Bean(initMethod="init",destroyMethod="detory")
   public Car car(){
      return new Car();
   }

}
Cat
@Component
public class Cat implements InitializingBean,DisposableBean {
   
   public Cat(){
      System.out.println("cat constructor...");
   }

   @Override
   public void destroy() throws Exception {
      // TODO Auto-generated method stub
      System.out.println("cat...destroy...");
   }

   @Override
   public void afterPropertiesSet() throws Exception {
      // TODO Auto-generated method stub
      System.out.println("cat...afterPropertiesSet...");
   }

}
输出
cat constructor...
cat...afterPropertiesSet...
car constructor...
car ... init...
容器创建完成
car ... detory...
cat...destroy...

@PostConstruct和@PreDestroy

PostConstruct:可以理解为构造函数后,就是new bean之后
PreDestroy:可以理解为销毁前:就是销毁bean前

下面类中的注释看不懂没关系,因为那都是结合spring源码来说的,没有看过源码的,直接记住下面的描述就可以了
1)、指定初始化和销毁方法;
通过@Bean指定init-method和destroy-method;
2)、通过让Bean实现InitializingBean(定义初始化逻辑),
DisposableBean(定义销毁逻辑);
3)、可以使用JSR250;
@PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
@PreDestroy:在容器销毁bean之前通知我们进行清理工作

MainConfigOfLifeCycle
/**
 * bean的生命周期:
 *        bean创建---初始化----销毁的过程
 * 容器管理bean的生命周期;
 * 我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
 * 
 * 构造(对象创建)
 *        单实例:在容器启动的时候创建对象
 *        多实例:在每次获取的时候创建对象\
 * 
 * BeanPostProcessor.postProcessBeforeInitialization
 * 初始化:
 *        对象创建完成,并赋值好,调用初始化方法。。。
 * BeanPostProcessor.postProcessAfterInitialization
 * 销毁:
 *        单实例:容器关闭的时候
 *        多实例:容器不会管理这个bean;容器不会调用销毁方法;
 * 
 * 
 * 遍历得到容器中所有的BeanPostProcessor;挨个执行beforeInitialization,
 * 一但返回null,跳出for循环,不会执行后面的BeanPostProcessor.postProcessorsBeforeInitialization
 * 
 * BeanPostProcessor原理
 * populateBean(beanName, mbd, instanceWrapper);给bean进行属性赋值
 * initializeBean
 * {
 * applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
 * invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化
 * applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
 *}
 * 
 * 
 * 看不懂上面的不要紧,因为上面都是结合spring源码来写的,没有看过原来的就记住下面的描述就可以了
 * 1)、指定初始化和销毁方法;
 *        通过@Bean指定init-method和destroy-method;
 * 2)、通过让Bean实现InitializingBean(定义初始化逻辑),
 *              DisposableBean(定义销毁逻辑);
 * 3)、可以使用JSR250;
 *        @PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
 *        @PreDestroy:在容器销毁bean之前通知我们进行清理工作
 * 
 * @author lfy
 *
 */
@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {
   
   //@Scope("prototype")
   @Bean(initMethod="init",destroyMethod="detory")
   public Car car(){
      return new Car();
   }

}
Dog
@Component
public class Dog implements ApplicationContextAware {
   
   //@Autowired
   private ApplicationContext applicationContext;
   
   public Dog(){
      System.out.println("dog constructor...");
   }
   
   //对象创建并赋值之后调用
   @PostConstruct
   public void init(){
      System.out.println("Dog....@PostConstruct...");
   }
   
   //容器移除对象之前
   @PreDestroy
   public void detory(){
      System.out.println("Dog....@PreDestroy...");
   }

   @Override
   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      // TODO Auto-generated method stub
      this.applicationContext = applicationContext;
   }
}
输出
cat constructor...
cat...afterPropertiesSet...
dog constructor...
Dog....@PostConstruct...
car constructor...
car ... init...
容器创建完成
car ... detory...
Dog....@PreDestroy...
cat...destroy...

BeanPostProcessor


/**
 * bean的生命周期:
 *        bean创建---初始化----销毁的过程
 * 容器管理bean的生命周期;
 * 我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
 * 
 * 构造(对象创建)
 *        单实例:在容器启动的时候创建对象
 *        多实例:在每次获取的时候创建对象\
 * 
 * BeanPostProcessor.postProcessBeforeInitialization
 * 初始化:
 *        对象创建完成,并赋值好,调用初始化方法。。。
 * BeanPostProcessor.postProcessAfterInitialization
 * 销毁:
 *        单实例:容器关闭的时候
 *        多实例:容器不会管理这个bean;容器不会调用销毁方法;
 * 
 * 
 * 遍历得到容器中所有的BeanPostProcessor;挨个执行beforeInitialization,
 * 一但返回null,跳出for循环,不会执行后面的BeanPostProcessor.postProcessorsBeforeInitialization
 * 
 * BeanPostProcessor原理
 * populateBean(beanName, mbd, instanceWrapper);给bean进行属性赋值
 * initializeBean
 * {
 * applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
 * invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化
 * applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
 *}
 * 
 * 
 * 
 * 1)、指定初始化和销毁方法;
 *        通过@Bean指定init-method和destroy-method;
 * 2)、通过让Bean实现InitializingBean(定义初始化逻辑),
 *              DisposableBean(定义销毁逻辑);
 * 3)、可以使用JSR250;
 *        @PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
 *        @PreDestroy:在容器销毁bean之前通知我们进行清理工作
 * 4)、BeanPostProcessor【interface】:bean的后置处理器;
 *        在bean初始化前后进行一些处理工作;
 *        postProcessBeforeInitialization:在初始化之前工作
 *        postProcessAfterInitialization:在初始化之后工作
 * 
 * Spring底层对 BeanPostProcessor 的使用;
 *        bean赋值,注入其他组件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor;
 * 
 * @author lfy
 *
 */
@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {
   
   //@Scope("prototype")
   @Bean(initMethod="init",destroyMethod="detory")
   public Car car(){
      return new Car();
   }

}
MyBeanPostProcessor
/**
 * 后置处理器:初始化前后进行处理工作
 * 将后置处理器加入到容器中
 * @author lfy
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

   @Override
   public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
      // TODO Auto-generated method stub
      System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
      return bean;
   }

   @Override
   public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
      // TODO Auto-generated method stub
      System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
      return bean;
   }

}

输出

自己写的组件输出内容

car constructor...
postProcessBeforeInitialization...car=>com.atguigu.bean.Car@5ef60048
car ... init...
postProcessAfterInitialization...car=>com.atguigu.bean.Car@5ef60048
cat constructor...
postProcessBeforeInitialization...cat=>com.atguigu.bean.Cat@780cb77
cat...afterPropertiesSet...
postProcessAfterInitialization...cat=>com.atguigu.bean.Cat@780cb77
dog constructor...
postProcessBeforeInitialization...dog=>com.atguigu.bean.Dog@4034c28c
Dog....@PostConstruct...
postProcessAfterInitialization...dog=>com.atguigu.bean.Dog@4034c28c
容器创建完成...
Dog....@PreDestroy...
cat...destroy...
car ... detory...
  1. BeanPostProcessor在Spring源码里大量被使用到,仅凭这里雷丰阳老师讲的一点点原理,是无法体会的,建议自己去看看Spring源码。所以这里的原理部分我也就直接省略了,在本视频中讲的太浅了。

属性赋值

@Value和@PropertySource(重点)

Person
public class Person {
   
   //使用@Value赋值;
   //1、基本数值
   //2、可以写SpEL; #{}
   //3、可以写${};取出配置文件【properties】中的值(在运行环境变量里面的值)
   
   @Value("张三")
   private String name;
   @Value("#{20-2}")
   private Integer age;
   
   @Value("${person.nickName}")
   private String nickName;
   
   
   
   public String getNickName() {
      return nickName;
   }
   public void setNickName(String nickName) {
      this.nickName = nickName;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Integer getAge() {
      return age;
   }
   public void setAge(Integer age) {
      this.age = age;
   }
   
   public Person(String name, Integer age) {
      super();
      this.name = name;
      this.age = age;
   }
   public Person() {
      super();
      // TODO Auto-generated constructor stub
   }
   @Override
   public String toString() {
      return "Person [name=" + name + ", age=" + age + ", nickName=" + nickName + "]";
   }
}
person.properties
person.nickName=\u5C0F\u674E\u56DB
MainConfigOfPropertyValues

使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中(以key/value) 的形式保存到spring容器中了

//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;加载完外部的配置文件以后使用${}取出配置文件的值
@PropertySource(value={"classpath:/person.properties"})
@Configuration
public class MainConfigOfPropertyValues {
   
   @Bean
   public Person person(){
      return new Person();
   }

}
IOCTest_PropertyValue
public class IOCTest_PropertyValue {
   AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
   @Test
   public void test01(){
      printBeans(applicationContext);
      System.out.println("=============");
      
      Person person = (Person) applicationContext.getBean("person");
      System.out.println(person);
      
      
      ConfigurableEnvironment environment = applicationContext.getEnvironment();
      String property = environment.getProperty("person.nickName");
      System.out.println(property);
      applicationContext.close();
   }
   
   private void printBeans(AnnotationConfigApplicationContext applicationContext){
      String[] definitionNames = applicationContext.getBeanDefinitionNames();
      for (String name : definitionNames) {
         System.out.println(name);
      }
   }

}
输出
mainConfigOfPropertyValues
person
=============
Person [name=张三, age=18, nickName=小李四]
小李四

自动装配

@Autowired-@Qualifier-@Primary-@Resource-@Inject

@Controller
public class BookController {
   
   @Autowired
   private BookService bookService;

}
@Service
public class BookService {
   
   //@Qualifier("bookDao")
   //@Autowired(required=false)
   //@Resource(name="bookDao2")
   @Inject
   private BookDao bookDao;
   
   public void print(){
      System.out.println(bookDao);
   }

   @Override
   public String toString() {
      return "BookService [bookDao=" + bookDao + "]";
   }

}
//名字默认是类名首字母小写
@Repository
public class BookDao {
   
   private String lable = "1";

   public String getLable() {
      return lable;
   }

   public void setLable(String lable) {
      this.lable = lable;
   }

   @Override
   public String toString() {
      return "BookDao [lable=" + lable + "]";
   }
  
}
MainConifgOfAutowired
/**
 * 自动装配;
 *        Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
 * 
 * 1)、@Autowired:自动注入:
 *        1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
 *        2)、如果找到多个相同类型的组件,再将属性的名称作为组件的id去容器中查找
 *                       applicationContext.getBean("bookDao")
 *        3)、@Qualifier("bookDao"):使用@Qualifier指定需要装配的组件的id,而不是使用属性名
 *        4)、自动装配默认一定要将属性赋值好,没有就会报错;
 *           可以使用@Autowired(required=false);
 *        5)、@Primary:让Spring进行自动装配的时候,默认使用首选的bean;
 *              也可以继续使用@Qualifier指定需要装配的bean的名字
 *        BookService{
 *           @Autowired
 *           BookDao  bookDao;
 *        }
 * 
 * 2)、Spring还支持使用@Resource(JSR250)和@Inject(JSR330)[java规范的注解]
 *        @Resource:
 *           可以和@Autowired一样实现自动装配功能;默认是按照组件名称进行装配的;
 *           没有能支持@Primary功能没有支持@Autowired(reqiured=false);
 *        @Inject:
 *           需要导入javax.inject的包,和Autowired的功能一样。没有required=false的功能;
 *  @Autowired:Spring定义的; @Resource、@Inject都是java规范
 *     
 * AutowiredAnnotationBeanPostProcessor:解析完成自动装配功能;       
 * 
 * 3)、 @Autowired:构造器,参数,方法,属性;都是从容器中获取参数组件的值
 *        1)、[标注在方法位置]:@Bean+方法参数;参数从容器中获取;默认不写@Autowired效果是一样的;都能自动装配
 *        2)、[标在构造器上]:如果组件只有一个有参构造器,这个有参构造器的@Autowired可以省略,参数位置的组件还是可以自动从容器中获取
 *        3)、放在参数位置:
 			public Boss(@Autowired Car car){
				this.car = car;
				System.out.println("Boss...有参构造器");
			}
 * 
 * 4)、自定义组件想要使用Spring容器底层的一些组件(ApplicationContext,BeanFactory,xxx);
 *        自定义组件实现xxxAware;在创建对象的时候,会调用接口规定的方法注入相关组件;Aware;
 *        把Spring底层一些组件注入到自定义的Bean中;
 *        xxxAware:功能使用xxxProcessor;
 *           ApplicationContextAware==》ApplicationContextAwareProcessor;
 *     
 *        
 * @author lfy
 *
 */
@Configuration
@ComponentScan({"com.atguigu.service","com.atguigu.dao",
   "com.atguigu.controller","com.atguigu.bean"})
public class MainConifgOfAutowired {
   
   @Primary
   @Bean("bookDao2")
   public BookDao bookDao(){
      BookDao bookDao = new BookDao();
      bookDao.setLable("2");
      return bookDao;
   }
   
   /**
    * @Bean标注的方法创建对象的时候,方法参数的值默认从容器中获取
    * @param car
    * @return
    */
   @Bean
   public Color color(Car car){
      Color color = new Color();
      color.setCar(car);
      return color;
   }
   

}
IOCTest_Autowired
public class IOCTest_Autowired {
   
   @Test
   public void test01(){
      AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConifgOfAutowired.class);
      
      BookService bookService = applicationContext.getBean(BookService.class);
      System.out.println(bookService);
      
      //BookDao bean = applicationContext.getBean(BookDao.class);
      //System.out.println(bean);
      
      Boss boss = applicationContext.getBean(Boss.class);
      System.out.println(boss);
      Car car = applicationContext.getBean(Car.class);
      System.out.println(car);
      
      Color color = applicationContext.getBean(Color.class);
      System.out.println(color);
      System.out.println(applicationContext);
      applicationContext.close();
   }

}

@Profle

MainConfigOfProfile
/**
 * Profile:
 *        Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
 * 
 * 开发环境、测试环境、生产环境;
 * 数据源:(/A)(/B)(/C);
 * 
 * 
 * @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
 * 
 * 1)、加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
 * 2)、写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
 * 3)、没有标注环境标识的bean在,任何环境下都是加载的;
 */

@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware{
   
   @Value("${db.user}")
   private String user;
   
   private StringValueResolver valueResolver;
   
   private String  driverClass;
   
   
   @Bean
   public Yellow yellow(){
      return new Yellow();
   }
   
   @Profile("test")
   @Bean("testDataSource")
   public DataSource dataSourceTest(@Value("${db.password}")String pwd) throws Exception{
      ComboPooledDataSource dataSource = new ComboPooledDataSource();
      dataSource.setUser(user);
      dataSource.setPassword(pwd);
      dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
      dataSource.setDriverClass(driverClass);
      return dataSource;
   }
   
   
   @Profile("dev")
   @Bean("devDataSource")
   public DataSource dataSourceDev(@Value("${db.password}")String pwd) throws Exception{
      ComboPooledDataSource dataSource = new ComboPooledDataSource();
      dataSource.setUser(user);
      dataSource.setPassword(pwd);
      dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/ssm_crud");
      dataSource.setDriverClass(driverClass);
      return dataSource;
   }
   
   @Profile("prod")
   @Bean("prodDataSource")
   public DataSource dataSourceProd(@Value("${db.password}")String pwd) throws Exception{
      ComboPooledDataSource dataSource = new ComboPooledDataSource();
      dataSource.setUser(user);
      dataSource.setPassword(pwd);
      dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/scw_0515");
      
      dataSource.setDriverClass(driverClass);
      return dataSource;
   }

   @Override
   public void setEmbeddedValueResolver(StringValueResolver resolver) {
      // TODO Auto-generated method stub
      this.valueResolver = resolver;
      driverClass = valueResolver.resolveStringValue("${db.driverClass}");
   }

}
dbconfig.properties
db.user=root
db.password=123456
db.driverClass=com.mysql.jdbc.Driver

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/696876.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

五个漂亮的 Arch Linux 发行版

导读本文将介绍几个漂亮的 Arch Linux 发行版&#xff0c;这些发行版将设计的优雅之美与 Arch Linux 强大的底层框架相结合。 Arch Linux 提供滚动更新模型、强大的 pacman 软件包管理器&#xff0c;以及通过其软件仓库提供的成千上万的应用程序。它非常受欢迎&#xff0c;因为…

多线程之wait()和notify()详解

1.每个Java对象都有一个监视器&#xff08;monitor&#xff09;&#xff0c;它是用来控制对该对象的访问的。一个线程要想进入某个对象的监视器区域&#xff0c;必须先获得该对象的锁。然后可以通过wait方法来释放该对象的锁并进入等待状态&#xff0c;直到其他线程调用notify或…

python读取.ann文件编码格式

encoding‘latin1.(gbk和utf-8均报错) 【还是chatpgt告诉我的呢】

oracle查看当前实例与切换实例

查看实例 select name from v$database;切换实例 sqlplus /ORCL as sysdba

【前端工程化】深入浅出vite(二)--vue3全家桶+ts构建后管系统

安装基础包 npm create vitelatest # 这里选择的是VueTypescript的组合 cd vue-admin npm install# 先安装基础包 npm install vue-router4 npm i pinia npm i axios npm install sass --save-dev npm install element-plus --save npm install element-plus/icons-vue npm in…

全志V3S嵌入式驱动开发(spi-nor image制作)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 其实&#xff0c;我们之前就讨论过怎么把image烧入到v3s的spi-nor当中去。当时使用的方法是借助于sunxi-fel工具&#xff0c;烧入的image也比计较小…

2. 计算点到平面的投影

参考 https://www.cnblogs.com/nobodyzhou/p/6145030.html 所以用各种平面提取算法后&#xff0c;得到的平面方程, 注意此处的平面方程abcd已经是归一化了。 a x b y c z d 0 ax by cz d 0 axbyczd0 此时的原点到该平面点的投影点为 C P ( − a d , − b d , − c …

渗透测试内网基础知识

点赞后看&#xff0c;养成习惯 喜欢的话 可以点个关注哟 你们的点赞支持对博主们来说很重要哦 &#xff01;&#xff01;&#xff01; 为方便您的阅读&#xff0c;可点击下方蓝色字体&#xff0c;进行跳转↓↓↓ 01 内网概述02 工作组03 域04 活动目录05 域控制器和活动目录的…

【C/C++】使用类和对象 设计点和圆的关系案例

创作不易&#xff0c;本篇文章如果帮助到了你&#xff0c;还请点赞 关注支持一下♡>&#x16966;<)!! 主页专栏有更多知识&#xff0c;如有疑问欢迎大家指正讨论&#xff0c;共同进步&#xff01; &#x1f525;c系列专栏&#xff1a;C/C零基础到精通 &#x1f525; 给大…

误删除boot目录 修复办法

误删除boot目录 修复办法 本次演示系统为kylin-v10-sp3-20230324&#xff0c;kernel&#xff1a;4.19.90-52.22.v2207.ky10.x86_64 文章目录 误删除boot目录 修复办法1.报错现象2.正常机器boot目录结构3.误删除boot目录4.故障修复挂载光盘&#xff0c;进入救援模式 5.非正常情况…

mysql8.0.26忘记密码后重置(centos8)

1、查看配置文件地址 mysql --help|grep my.cnf 2、编辑/etc/my.cnf配置文件 vim /etc/my.cnf增加跳过密码配置 [mysqld] skip-grant-tables 3、重启mysql service mysqld restart4、修改密码 use mysql; update user set authentication_string where userroot; FLUSH pr…

NoSQL之Redis配置与优化(初级)理论较多

目录 一、关系数据库与非关系型数据库 1、 关系型数据库 2、 非关系型数据库 二、关系型数据库和非关系型数据库区别 1、 数据存储方式不同 2、 扩展方式不同 3、 对事务性的支持不同 三、非关系型数据库产生背景 1、可用于应对Web2.0纯动态网站类型的三高问题&#xf…

大数据Doris(五十一):Export语法和结果

文章目录 Export语法和结果 一、语法 二、结果 Export语法和结果 一、语法 Export 需要借助 Broker 进程访问远端存储&#xff0c;不同的 Broker 需要提供不同的参数&#xff0c;这里以导出到HDFS为例介绍Export 写法&#xff0c;也可以通过"help export "命令查…

Azure CLI - Premium SSD V1磁盘迁移至Premium SSD V2

目录 1.从服务器卸载Premium SSD V1 磁盘 2.创建Premium SSD V1磁盘snapshot 3.创建Premium SSD V2磁盘 4.挂载Premium SSD V2磁盘到服务器 已经在生产使用的Premium SSD V1磁盘&#xff0c;通过制作快照后&#xff0c;购买Premium SSD V2磁盘。 1.从服务器卸载Premium SSD…

Intellij IDEA detected unusually high memory use

一、IDEA如图 二、解决 2.1 修改IDEA的内存堆 运行IntelliJ IDEA的Java虚拟机&#xff08;JVM&#xff09;分配一些预定义的内存量。默认值取决于平台。如果您遇到速度减慢&#xff0c;可能需要增加内存堆。 为运行IDE分配的堆大小与编译应用程序的堆大小不同。如果要为编译代…

服务行业的企业所得税高,这是为何,该如何解决?

服务行业的企业所得税高&#xff0c;这是为何&#xff0c;该如何解决&#xff1f; 《税筹顾问》专注于园区招商、企业税务筹划&#xff0c;合理合规助力企业节税&#xff01; 服务行业的发展历程&#xff0c;源远流长。它是为了满足我们生活中的需要应运而生&#xff0c;大家眼…

广州华锐互动:建筑施工VR虚拟仿真实训系统

VR技术在施工现场安全教育中有着广泛的应用。随着VR技术的不断发展&#xff0c;越来越多的企业开始将其应用于施工现场安全教育中&#xff0c;以提高工人的安全意识和应对能力。 传统的施工现场安全教育往往需要大量的时间和人力物力投入&#xff0c;而且效果并不理想.而VR技术…

生物数据下载

目录 1. 获取数据下载的地址 2. 生物数据常用的下载站点 1、核酸数据库 2、非编码RNA数据库 &#xff08;1&#xff09;.非编码小RNA数据库 &#xff08;2&#xff09;.长非编码RNA数据库&#xff1a; &#xff08;3&#xff09;.非编码RNA家族数据库 &#xff08;4&a…

C++ day41

1、总结类和结构体的区别 结构体中是变量和函数&#xff1b;类中是成员属性(成员变量)、成员方法(成员函数)/行为类的成员变量和成员函数的默认访问权限是私有的&#xff1b;结构体的成员变量和成员函数的默认访问权限是公开的类创建一个对象后&#xff0c;对该对象的任何修改…

2022年全国硕士研究生入学统一考试管理类专业学位联考数学试题——纯题目版

2022 年全国硕士研究生入学统一考试管理类专业学位联考数学试题 一、问题求解&#xff1a;第 1∼15 小题&#xff0c;每小题 3 分&#xff0c;共 45 分。下列每题给出的 A、B、C、D、E 五个选项中&#xff0c;只有一项是符合试题要求的&#xff0c;请在答&#xff0e;题&#…