Spring5.2.5bean的加载过程

本文介绍如何使用Spring框架进行基本的Java应用程序开发。通过配置Gradle构建文件引入Spring核心库,定义简单的POJO类作为业务对象,并利用Spring的XML配置完成依赖注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.搭建环境

1.gradle文件

plugins {
    id 'java'
}

group 'com.sth'
version '1.0'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }
}


dependencies {
    compile(
            "org.springframework:spring-core:5.2.5.RELEASE",//最新的版本core   DI IOC
            "org.springframework:spring-aop:5.2.5.RELEASE", //
            "org.springframework:spring-beans:5.2.5.RELEASE",
            "org.springframework:spring-context:5.2.5.RELEASE",
            "org.springframework:spring-context-support:5.2.5.RELEASE",
            "org.springframework:spring-web:5.2.5.RELEASE",
            "org.springframework:spring-orm:5.2.5.RELEASE", // 对象关系库
            "org.springframework:spring-aspects:5.2.5.RELEASE",// 面向切面编程
            "org.springframework:spring-webmvc:5.2.5.RELEASE",
            "org.springframework:spring-jdbc:5.2.5.RELEASE",
            "org.springframework:spring-instrument:5.2.5.RELEASE",// 字节码 增强
            "org.springframework:spring-tx:5.2.5.RELEASE" // 自定义标枪
    )
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}


[compileJava, javadoc, compileTestJava]*.options*.encoding = "UTF-8"

2.创建一个pojo

package com.sth.spring.bean;

/**
 * POJO : Plain Old Java Object
 */
public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

3.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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 ="student"  class="com.sth.spring.bean.Student">
        <property name="name" value="sitianhong"/>
        <property name="age" value="20"/>
    </bean>
</beans>

4.客户端

import com.sth.spring.bean.Student;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * IOC(Inverse of Controller,控制反转)
 * DI (Dependency Injection,依赖注入)
 */
public class SpringClient {

    public static void main(String[] args) {
        Resource resource = new ClassPathResource("applicationContext.xml");
        DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
        BeanDefinitionReader beanDefinitionReader = 
        new XmlBeanDefinitionReader(defaultListableBeanFactory);

        int a = beanDefinitionReader.loadBeanDefinitions(resource);
        Student s1 =(Student)defaultListableBeanFactory.getBean("student");
    }
}

… 未完待续

### 关于 Spring Core 5.2.5 中 MethodProxy.java 的 `roxy.invoke` 方法 在 Spring Framework 5.2.5 版本中,`MethodProxy.java` 是 CGLIB 库的一部分,而该库被广泛用于动态代理功能。当调用 `roxy.invoke` 方法时,可能会遇到一些特定的异常或堆栈跟踪信息[^1]。 #### 堆栈信息分析 通常情况下,如果 `roxy.invoke` 出现问题,可能的原因包括但不限于以下几种: 1. **目标对象为空** 如果传递给 `invoke` 方法的目标对象为 null,则会抛出 NullPointerException。这种错误通常是由于未正确初始化代理类实例引起的[^2]。 2. **参数不匹配** 当传入的方法参数与实际定义不符时,CGLIB 可能无法正常执行方法调用,从而引发 IllegalArgumentException 或其他类似的运行时异常[^3]。 3. **AOP 配置问题** 在 Spring AOP 场景下,如果切面配置不当或者拦截器链未能正确加载,也可能导致 `roxy.invoke` 抛出异常。这通常表现为 InvocationTargetException 或者更深层次的内部逻辑错误[^4]。 以下是可能出现的一个典型堆栈跟踪示例: ```java java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ... Caused by: java.lang.NullPointerException at com.example.MyService.performAction(MyService.java:30)... ``` 上述堆栈表明,在调用 `performAction` 方法的过程中发生了空指针异常,并最终通过 CGLIB 的 `MethodProxy.invoke` 泡沫化到顶层[^5]。 #### 解决方案建议 为了排查此类问题,可以采取以下措施: - 检查目标对象是否已被正确定义并赋值。 - 确认所有依赖注入均已完成,尤其是涉及复杂 Bean 生命周期管理的情况。 - 审视 AOP 切面规则以及切入点表达式的准确性,确保它们覆盖预期范围内的业务逻辑。 ```java // 示例代码片段展示如何安全地处理 invoke 调用 try { Object result = methodProxy.invoke(targetObject, args); } catch (Throwable throwable) { throw new RuntimeException("Error invoking method via proxy", throwable); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值