Spring中常用的11个扩展点
前言
IOC
(控制反转) 和 AOP
(面向切面编程)。扩展能力非常强
。由于这个优势的存在,让spring拥有强大的包容能力,让很多第三方应用能够轻松投入spring的怀抱。比如:rocketmq、mybatis、redis等。1.自定义拦截器
HttpServletRequest
和HttpServletResponse
等web对象实例。HandlerInterceptor
,包含三个方法:preHandle 目标方法执行前执行 postHandle 目标方法执行后执行 afterCompletion 请求完成时执行
HandlerInterceptor
接口的实现类HandlerInterceptorAdapter
类。HandlerInterceptorAdapter
类定义拦截器:public class AuthInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String requestUrl = request.getRequestURI();
if (checkAuth(requestUrl)) {
return true;
}
return false;
}
private boolean checkAuth(String requestUrl) {
System.out.println("===权限校验===");
return true;
}
}
@Configuration
public class WebAuthConfig extends WebMvcConfigurerAdapter {
@Bean
public AuthInterceptor getAuthInterceptor() {
return new AuthInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthInterceptor());
}
}
2.获取Spring容器对象
2.1 BeanFactoryAware接口
@Service
public class PersonService implements BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public void add() {
Person person = (Person) beanFactory.getBean("person");
}
}
BeanFactoryAware
接口,然后重写setBeanFactory
方法,就能从该方法中获取到spring容器对象。2.2 ApplicationContextAware接口
@Service
public class PersonService2 implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void add() {
Person person = (Person) applicationContext.getBean("person");
}
}
ApplicationContextAware
接口,然后重写setApplicationContext
方法,也能从该方法中获取到spring容器对象。2.3 ApplicationListener接口
@Service
public class PersonService3 implements ApplicationListener<ContextRefreshedEvent> {
private ApplicationContext applicationContext;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
applicationContext = event.getApplicationContext();
}
public void add() {
Person person = (Person) applicationContext.getBean("person");
}
}
3.全局异常处理
@RequestMapping("/test")
@RestController
public class TestController {
@GetMapping("/add")
public String add() {
int a = 10 / ;
return "成功";
}
}
@GetMapping("/add")
public String add() {
String result = "成功";
try {
int a = 10 / ;
} catch (Exception e) {
result = "数据异常";
}
return result;
}
RestControllerAdvice
。@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public String handleException(Exception e) {
if (e instanceof ArithmeticException) {
return "数据异常";
}
if (e instanceof Exception) {
return "服务器内部异常";
}
retur nnull;
}
}
handleException
方法中处理异常情况,业务接口中可以放心使用,不再需要捕获异常(有人统一处理了)。真是爽歪歪。4.类型转换器
Converter<S,T>:将 S 类型对象转为 T 类型对象 ConverterFactory<S, R>:将 S 类型对象转为 R 类型及子类对象 GenericConverter:它支持多个source和目标类型的转化,同时还提供了source和目标类型的上下文,这个上下文能让你实现基于属性上的注解或信息来进行类型转换。
Converter<S,T>为
例。假如:接口中接收参数的实体对象中,有个字段的类型是Date,但是实际传参的是字符串类型:2021-01-03 10:20:15,要如何处理呢?@Data
public class User {
private Long id;
private String name;
private Date registerDate;
}
Converter
接口:public class DateConverter implements Converter<String, Date> {
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public Date convert(String source) {
if (source != null && !"".equals(source)) {
try {
simpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
}
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DateConverter());
}
}
@RequestMapping("/user")
@RestController
public class UserController {
@RequestMapping("/save")
public String save(@RequestBody User user) {
return "success";
}
}
5.导入配置
@Import
注解完成这个功能。5.1 普通类
public class A {
}
@Import(A.class)
@Configuration
public class TestConfiguration {
}
@Import
注解引入A类,spring就能自动实例化A对象,然后在需要使用的地方通过@Autowired
注解注入即可:@Autowired
private A a;
@Bean
注解也能实例化bean。5.2 配置类
@Configuration
注解还支持多种组合注解,比如:@Import @ImportResource @PropertySource等。
public class A {
}
public class B {
}
@Import(B.class)
@Configuration
public class AConfiguration {
@Bean
public A a() {
return new A();
}
}
@Import(AConfiguration.class)
@Configuration
public class TestConfiguration {
}
@Import
、@ImportResource
、@PropertySource
等注解引入的类进行递归,一次性全部引入。5.3 ImportSelector
ImportSelector
接口:public class AImportSelector implements ImportSelector {
private static final String CLASS_NAME = "com.sue.cache.service.test13.A";
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{CLASS_NAME};
}
}
@Import(AImportSelector.class)
@Configuration
public class TestConfiguration {
}
selectImports
方法返回的是数组,意味着可以同时引入多个类,还是非常方便的。5.4 ImportBeanDefinitionRegistrar
ImportBeanDefinitionRegistrar
接口:public class AImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(A.class);
registry.registerBeanDefinition("a", rootBeanDefinition);
}
}
@Import(AImportBeanDefinitionRegistrar.class)
@Configuration
public class TestConfiguration {
}
registerBeanDefinitions
方法中获取到BeanDefinitionRegistry
容器注册对象,可以手动控制BeanDefinition的创建和注册。6.项目启动时
CommandLineRunner ApplicationRunner
ApplicationRunner
接口为例:@Component
public class TestRunner implements ApplicationRunner {
@Autowired
private LoadDataService loadDataService;
public void run(ApplicationArguments args) throws Exception {
loadDataService.load();
}
}
ApplicationRunner
接口,重写run
方法,在该方法中实现自己定制化需求。@Order(n)
注解,n的值越小越先执行。当然也可以通过@Priority
注解指定顺序。7.修改BeanDefinition
BeanDefinition
对象中,然后通过BeanDefinition对象,实例化Bean对象。BeanFactoryPostProcessor
接口。@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory;
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
beanDefinitionBuilder.addPropertyValue("id", 123);
beanDefinitionBuilder.addPropertyValue("name", "苏三说技术");
defaultListableBeanFactory.registerBeanDefinition("user", beanDefinitionBuilder.getBeanDefinition());
}
}
8.初始化Bean前后
BeanPostProcessor
接口。postProcessBeforeInitialization 该在初始化方法之前调用。 postProcessAfterInitialization 该方法再初始化方法之后调用。
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof User) {
((User) bean).setUserName("苏三说技术");
}
return bean;
}
}
9.初始化方法
使用@PostConstruct注解 实现InitializingBean接口
9.1 使用@PostConstruct注解
@Service
public class AService {
@PostConstruct
public void init() {
System.out.println("===初始化===");
}
}
@PostConstruct
注解,这样就有初始化的能力。9.2 实现InitializingBean接口
@Service
public class BService implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("===初始化===");
}
}
InitializingBean
接口,重写afterPropertiesSet
方法,该方法中可以完成初始化功能。10.关闭容器前
DisposableBean
接口,并且重写它的destroy
方法:@Service
public class DService implements InitializingBean, DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean afterPropertiesSet");
}
}
11.自定义作用域
Scope
只有两种:singleton 单例,每次从spring容器中获取到的bean都是同一个对象。 prototype 多例,每次从spring容器中获取到的bean都是不同的对象。
RequestScope 同一次请求从spring容器中获取到的bean都是同一个对象。 SessionScope 同一个会话从spring容器中获取到的bean都是同一个对象。
public class ThreadLocalScope implements Scope {
private static final ThreadLocal THREAD_LOCAL_SCOPE = new ThreadLocal();
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Object value = THREAD_LOCAL_SCOPE.get();
if (value != null) {
return value;
}
Object object = objectFactory.getObject();
THREAD_LOCAL_SCOPE.set(object);
return object;
}
@Override
public Object remove(String name) {
THREAD_LOCAL_SCOPE.remove();
return null;
}
@Override
public void registerDestructionCallback(String name, Runnable callback) {
}
@Override
public Object resolveContextualObject(String key) {
return null;
}
@Override
public String getConversationId() {
return null;
}
}
@Component
public class ThreadLocalBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerScope("threadLocalScope", new ThreadLocalScope());
}
}
@Scope("threadLocalScope")
@Service
public class CService {
public void add() {
}
}
以上文章来源于公众号-苏三说技术 ,作者苏三呀
相关文章