alibaba / alibaba/spring-context-support

在方法上使用@NacosValue注解时,注入报空指针

Open
#57 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
155
Forks
87
PR merge metrics
No merged PRs in 30d

Description

### 版本信息:
- spring-context-support:1.0.11
- nacos-client:2.1.1
- nacos-config-spring-boot-starter:0.2.11
- nacos-spring-context:1.1.1

### 触发方式:
**在方法上使用@NacosValue**
```java
private String value;

@NacosValue(value = "${nacos.test.value:default_value}",autoRefreshed = true)
void setValue(String value) {
this.value = value;
System.out.println("nacos测试刷新数据:" + value);
}
```

### 报错位置:
`com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.AnnotatedMethodElement#inject`

### 报错信息:
`org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dubboServiceImpl': Injection of @NacosValue dependencies is failed; nested exception is java.lang.NullPointerException`

### 报错原因:
变量pd为null,没有判空直接使用导致空指针

### 解决方案
**判断如果pd为null则从member中获取**
`Class injectedType = null != pd?pd.getPropertyType():((Method) this.member).getParameterTypes()[0];`

**完整方法**
```java
@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {

Class injectedType = pd?pd.getPropertyType():((Method) this.member).getParameterTypes()[0];

Object injectedObject = getInjectedObject(attributes, bean, beanName, injectedType, this);

ReflectionUtils.makeAccessible(method);

method.invoke(bean, injectedObject);

}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at AbstractAnnotationBeanPostProcessor.AnnotatedMethodElement#inject, where method-level @NacosValue injection is reported to fail when the property descriptor is null. Review the member and parameter-type handling, then verify that the setter example no longer raises a NullPointerException during bean creation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.