一、获取Spring容器对象

1.实现BeanFactoryAware接口

package com.example.spepcdemo.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Service;

/**
 * @author qx
 * @date 2024/5/29
 * @des
 */
@Service
public class StudentService implements BeanFactoryAware {

    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public void test() {
        StudentService studentService = beanFactory.getBean(StudentService.class);
        System.out.println(studentService);

    }
}

测试:

@Slf4j
@SpringBootTest
class SpepcDemoApplicationTests {

    @Autowired
    private StudentService studentService;

    @Test
    void test1(){
      studentService.test();
    }
}

输出:

com.example.spepcdemo.service.StudentService@1e50eb3f

2.实现ApplicationContextAware接口

package com.example.spepcdemo.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * @author qx
 * @date 2024/5/29
 * @des
 */
@Service
public class StudentService implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }


    public void test() {
        StudentService studentService = applicationContext.getBean(StudentService.class);
        System.out.println(studentService);

    }


}

测试:

@Slf4j
@SpringBootTest
class SpepcDemoApplicationTests {

    @Autowired
    private StudentService studentService;

    @Test
    void test1() {
        studentService.test();
    }
}

输出:

com.example.spepcdemo.service.StudentService@5e9ea380

3.实现ApplicationListener接口

package com.example.spepcdemo.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

/**
 * @author qx
 * @date 2024/5/29
 * @des
 */
@Service
public class StudentService implements ApplicationListener<ContextRefreshedEvent> {

    private ApplicationContext applicationContext;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        applicationContext = contextRefreshedEvent.getApplicationContext();
    }


    public void test() {
        StudentService studentService = applicationContext.getBean(StudentService.class);
        System.out.println(studentService);

    }


}

测试:

@Slf4j
@SpringBootTest
class SpepcDemoApplicationTests {

    @Autowired
    private StudentService studentService;

    @Test
    void test1() {
        studentService.test();
    }
}

输出:

com.example.spepcdemo.service.StudentService@1d8dbf10

二、全局异常处理

我们手动创建一个异常看看页面返回什么错误信息。

package com.example.spepcdemo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qx
 * @date 2024/5/29
 * @des
 */
@RestController
public class TestController {

    @GetMapping("/test")
    public String testDo() {
        int i = 10 / 0;
        return "success";
    }

}

Spring中两种扩展能力学习_Spring容器对象

我们就需要运用Spring机制,对系统存在的各种异常进行统一的处理。

全局异常处理机制:

package com.example.spepcdemo.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author qx
 * @date 2024/5/29
 * @des
 */
@RestControllerAdvice
public class GlobalExceptionHander {

    @ExceptionHandler(Exception.class)
    public String handleException(Exception e) {
        if (e instanceof ArithmeticException) {
            return "数据异常";
        }
        if (e instanceof Exception) {
            return "其他异常";
        }
        return "系统错误";
    }

}

我们继续访问之前的请求,发现我们已经把异常信息根据自己设置的异常信息返回了。

Spring中两种扩展能力学习_全局异常处理_02