forked from spring-projects/spring-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-projectsGH-214: Retryable on JPA Repos with proxyTargetClass
Resolves spring-projects#214 When `proxyTargetClass` is `true` (Boot's default), the JPA interfaces are not copied to the outer retry proxy created by auto-proxy. Add a BPP to fix up the proxy, when needed.
- Loading branch information
1 parent
83a73d0
commit 0d2e3fb
Showing
5 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/org/springframework/retry/annotation/JPARepositoryRetryBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.retry.annotation; | ||
|
||
import org.springframework.aop.Advisor; | ||
import org.springframework.aop.framework.Advised; | ||
import org.springframework.aop.framework.ProxyFactory; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation; | ||
import org.springframework.retry.interceptor.Retryable; | ||
|
||
/** | ||
* Add a bean of this instance if you wish to annotate JPA Repository interfaces with | ||
* {@code @Retryable}. It merges the repository proxy advisors with the retry advisor. | ||
* | ||
* @author Gary Russell | ||
* @since 1.3.2 | ||
*/ | ||
public class JPARepositoryRetryBeanPostProcessor implements BeanPostProcessor, Ordered { | ||
|
||
@Override | ||
public int getOrder() { | ||
return Integer.MAX_VALUE; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | ||
if (bean instanceof Retryable && bean instanceof Advised) { | ||
Advised advised = (Advised) bean; | ||
try { | ||
Object target = advised.getTargetSource().getTarget(); | ||
if (target instanceof Advised) { | ||
Advised advised2 = (Advised) target; | ||
Object target2 = advised2.getTargetSource().getTarget(); | ||
ProxyFactory pf = new ProxyFactory(target2); | ||
pf.removeInterface(JpaRepositoryImplementation.class); | ||
for (Advisor advisor : advised.getAdvisors()) { | ||
pf.addAdvisor(advisor); | ||
} | ||
for (Advisor advisor : advised2.getAdvisors()) { | ||
pf.addAdvisor(advisor); | ||
} | ||
for (Class<?> iface : advised2.getProxiedInterfaces()) { | ||
pf.addInterface(iface); | ||
} | ||
Object proxy = pf.getProxy(); | ||
return proxy; | ||
} | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return bean; | ||
} | ||
|
||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | ||
return bean; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters