Skip to content

Spring Core

Spring Framework 的核心模块,提供依赖注入(DI)和控制反转(IOC)功能。

IoC 容器

基本概念

概念说明
IoC控制反转,将对象创建权交给容器
DI依赖注入,容器自动注入依赖对象
Bean由容器管理的对象

Bean 配置

xml
<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 无参构造 -->
    <bean id="userService" class="com.example.UserService"/>

    <!-- 带参数构造 -->
    <bean id="userService2" class="com.example.UserService">
        <constructor-arg name="name" value="张三"/>
    </bean>

    <!-- setter 注入 -->
    <bean id="userService3" class="com.example.UserService">
        <property name="name" value="李四"/>
    </bean>
</beans>
java
// 启动容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = context.getBean("userService", UserService.class);

注解配置

java
@Configuration
@ComponentScan("com.example")
public class AppConfig {
    // @Bean 方式
    @Bean
    public UserService userService() {
        return new UserService();
    }
}
java
// 使用注解定义 Bean
@Component           // 通用组件
@Service            // 服务层
@Repository         // 数据访问层
@Controller         // 控制器(Spring MVC)
@Configuration      // 配置类

// 注入方式
@Autowired          // 按类型注入
@Qualifier("name")  // 按名称注入
@Resource(name="")  // 按名称注入(Java 标准)
@Value("${key}")    // 注入配置值

AOP

核心概念

概念说明
Join Point连接点,可拦截的方法
Pointcut切点,指定要拦截的连接点
Advice通知,增强逻辑
Aspect切面,切点+通知
Weaving织入,将增强应用到目标

通知类型

java
@Aspect
@Component
public class LogAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void before(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法前: " + methodName);
    }

    @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))",
                    returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        System.out.println("方法返回后: " + result);
    }

    @AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))",
                   throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Exception e) {
        System.out.println("方法异常: " + e.getMessage());
    }

    @After("execution(* com.example.service.*.*(..))")
    public void after(JoinPoint joinPoint) {
        System.out.println("方法执行后(最终通知)");
    }

    @Around("execution(* com.example.service.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = pjp.proceed();
        long end = System.currentTimeMillis();
        System.out.println("执行时间: " + (end - start) + "ms");
        return result;
    }
}

总结

  1. IoC/DI:控制反转,依赖注入
  2. Bean 配置:XML 配置、注解配置、Java 配置
  3. AOP:面向切面编程,通知类型
  4. 注解:@Component、@Autowired、@Bean

基于 VitePress 构建