Spring中常用接口的简单使用

Posted 愉悦滴帮主)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring中常用接口的简单使用相关的知识,希望对你有一定的参考价值。

Spring中常用接口

 

1.Aware接口


1.1BeanNameAware接口

该接口可以对bean的生命周期进行一个回调的作用,例如:在Spring启动后,去回调bean的生命周期从而得到bean对象的beanName。我们开发者可以根据业务场景对beanName进行修改。代码如下:


@Component
public class AService implements BeanNameAware {
    @Autowired
    private BService bService;
    private String beanName;

    public void  test(){}

    @Override
    public void setBeanName(String name) {
        name = "张大仙";
        this.beanName = name;
    }

    public void setbService(BService bService) {
        this.bService = bService;
    }

    public BService getbService() {
        return bService;
    }

    public String getBeanName() {
        return beanName;
    }
}
   public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        AService aService = applicationContext.getBean("AService", AService.class);
        System.out.println(aService.getBeanName());
    }

1.2BeanFactoryAware接口

该接口同样可以对bean的生命周期进行一个回调的作用,例如:在Spring启动后,去回调bean的生命周期从而得到bean对象是由哪个bean工厂创建的。我们开发者可以根据业务场景对beanFactory进行修改。代码如下:

@Component
public class AService implements BeanFactoryAware {
    @Autowired
    private BService bService;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println(beanFactory);
    }

}

2.初始化相关接口

2.1 InitializingBean接口

在执行Bean的生命周期的时候,Spring会去判断InitializingBean接口有没有被实现,如果有则会去执行InitializingBean.afterPropertiesSet()方法。我们开发者可以根据业务场景对afterPropertiesSet方法进行编写。代码如下:

@Component
public class CService implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化逻辑");
    }
}

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
    }

3.AOP的使用

在bean的生命周期中,我们开发者可以根据业务场景对bean对象进行切面编写。例如:

@Configuration
@ComponentScan("com.yunhuan")
@EnableAspectJAutoProxy  //开启AOP
public class AppConfig {
}

@Aspect
@Component
public class LubanAspect {

    @Around("execution(* com.yunhuan.service.AService.test())")
    public void invoke(ProceedingJoinPoint point) {
        try {
            System.out.println("aop");
            point.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}
@Component
public class AService {

    public void test() {}
}
public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        AService aService = applicationContext.getBean("AService", AService.class);
        aService.test();

    }
}

4.beanDefinition

beanDefinition有属性,包括:constructorArgumentValues、beanClass、parentName、initMethodName、scope、factoryBeanName、dependSon

、propertyValues、destoryMethodName、primary、beanClass、lazyInit,autowireCandidate、FactoryMethodName。

5.BeanFactoryPostProcessor接口

bean生命周期:class---->BeanDefinition---->BeanFactory组建完成---->BeanFactoryPostProcessor---->new 对象(原始对象)---->填充属性---->Aware---->初始化---->AOP---->单例池(Map<beanName,对象>)

BeanFactoryPostProcessor接口是在bean生成BeanFactory组建完成进行之后调用的后置处理器。

BeanFactoryPostProcessor接口与 BeanPostProcessor接口类似,可以对bean的定义(配置元数据)进行处理;也就是spring ioc运行BeanFactoryPostProcessor在容器实例化任何其他的bean之前读取配置元数据,并有可能修改它;如果业务需要,可以配置多个BeanFactoryPostProcessor的实现类,通过”order”控制执行次序(要实现Ordered接口)。  例如:

@Component
public class FService {

    private String  name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
@Component
public class LubanBeanFactory implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("FService");
        String beanClassName = beanDefinition.getBeanClassName();
        System.out.println(beanClassName);
        System.out.println("修改属性name值");
        beanDefinition.getPropertyValues().add("name", "liSi");
        System.out.println(beanDefinition.getPropertyValues());}
}
public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
       // FService fService = (FService) applicationContext.getBean("FService", FService.class);


    }
}

6.BeanPostProcessor接口

bean生命周期:class---->BeanDefinition---->BeanFactory组建完成---->BeanFactoryPostProcessor---->new 对象(原始对象)---->填充属性---->Aware---->初始化---->AOP---->单例池(Map<beanName,对象>)

BeanPostProcessor:bean级别的处理,针对某个具体的bean进行处理。了解bean的生命周期就能知道,BeanPostProcessor接口是在bean生成原始对象之后进行调用的后置处理器。

public interface BeanPostProcessor {

	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}

 

以上是关于Spring中常用接口的简单使用的主要内容,如果未能解决你的问题,请参考以下文章

17 Spring Data JPA的常用接口分析

简单了解Spring中常用工具类_java - JAVA

收藏|分享前端开发常用代码片段

IOS开发-OC学习-常用功能代码片段整理

Android片段和依赖注入

常用python日期日志获取内容循环的代码片段