08-26 15:28:09.285 WARN [CacheManager.java:373] CacheManager already shutdown 08-26 15:28:09.286 INFO [LocalSessionFactoryBean.java:706] Closing Hibernate SessionFactory 08-26 15:28:09.286 INFO [SessionFactoryImpl.java:542] closing 08-26 15:28:09.286 WARN [CacheManager.java:373] CacheManager already shutdown 08-26 15:28:09.287 INFO [LocalSessionFactoryBean.java:706] Closing Hibernate SessionFactory 08-26 15:28:09.287 INFO [SessionFactoryImpl.java:542] closing 08-26 15:28:09.288 WARN [CacheManager.java:373] CacheManager already shutdown 08-26 15:28:09.296 ERROR [ContextLoader.java:205] Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getAttachmentJob' defined in class path resource [com/smec/apps/crm/dev_application_context.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodException: com.sun.proxy.$Proxy17.getAttachment() java.lang.NoSuchMethodException: com.sun.proxy.$Proxy17.getAttachment() at java.lang.Class.getMethod(Class.java:1786)
时间: 2025-09-09 20:19:48 AIGC 浏览: 3
你遇到的错误是:
```
NoSuchMethodException: com.sun.proxy.$Proxy17.getAttachment()
```
这说明 Spring 在尝试调用 `getAttachment()` 方法时,找不到这个方法。问题出在你配置的 `MethodInvokingJobDetailFactoryBean` 中的 `targetMethod` 设置。
---
## 🔍 问题分析:
```xml
<bean id="getAttachmentJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="scheduledJobServiceImpl"/>
<property name="targetMethod" value="getAttachment"/>
</bean>
```
这段配置的意思是:**调用 `scheduledJobServiceImpl` 对象的 `getAttachment` 方法**。
但根据错误信息来看,Spring 没有找到这个方法,原因可能是:
1. `ScheduledJobServiceImpl` 类中没有定义 `getAttachment()` 方法。
2. 方法名拼写不一致(比如 `getAttachment` vs `getattachment`)。
3. 方法不是 `public` 的。
4. 你使用了 AOP 代理,导致目标方法被隐藏。
---
## ✅ 解决方案:
### ✅ 步骤 1:检查 `ScheduledJobServiceImpl` 是否确实有 `getAttachment()` 方法
确保你的类中包含如下方法:
```java
public class ScheduledJobServiceImpl implements ScheduledJobService {
public void getAttachment() {
// 实现逻辑
}
}
```
> 方法必须是 `public` 的,否则 Quartz 无法调用。
---
### ✅ 步骤 2:确保方法签名正确(无参)
`MethodInvokingJobDetailFactoryBean` 默认调用的是**无参方法**。
如果你的方法有参数,你需要配置 `arguments` 属性,例如:
```xml
<property name="arguments">
<list>
<value>someParam</value>
</list>
</property>
```
但根据你的代码,`getAttachment()` 是无参的,所以这一步可以跳过。
---
### ✅ 步骤 3:避免代理问题(推荐使用 `targetBeanName`)
有时候,使用 `targetObject` 会导致代理问题(如 `com.sun.proxy.$Proxy17`),建议改用 `targetBeanName`:
```xml
<bean id="getAttachmentJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetBeanName" value="scheduledJobServiceImpl"/>
<property name="targetMethod" value="getAttachment"/>
</bean>
```
这样 Spring 会直接从容器中查找 Bean,避免代理问题。
---
### ✅ 最终正确配置示例:
```xml
<!-- 定义 ScheduledJobServiceImpl Bean -->
<bean id="scheduledJobServiceImpl" parent="baseTxProxy">
<property name="target">
<bean class="com.smec.apps.crm.service.impl.ScheduledJobServiceImpl" autowire="byName"/>
</property>
</bean>
<!-- 定义 MethodInvokingJobDetailFactoryBean -->
<bean id="getAttachmentJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetBeanName" value="scheduledJobServiceImpl"/>
<property name="targetMethod" value="getAttachment"/>
</bean>
<!-- 定义 CronTriggerBean -->
<bean id="getAttachmentQuartz" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="getAttachmentJob"/>
<property name="cronExpression" value="0 0/5 * * * ?"/>
</bean>
<!-- SchedulerFactoryBean -->
<bean id="defaultJobFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="getAttachmentQuartz"/>
</list>
</property>
</bean>
```
---
### ✅ 额外建议:
- 如果你使用的是 Spring Boot,可以使用 `@Scheduled` 注解来简化定时任务配置。
- 可以在 `getAttachment()` 方法中加日志输出,验证是否真的被执行。
---
阅读全文
相关推荐















