|
1 | 1 | /* |
2 | | - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
22 | 22 | import java.util.Map; |
23 | 23 | import java.util.concurrent.ConcurrentHashMap; |
24 | 24 |
|
| 25 | +import org.apache.commons.logging.Log; |
| 26 | +import org.apache.commons.logging.LogFactory; |
25 | 27 | import org.aspectj.lang.reflect.PerClauseKind; |
26 | 28 |
|
27 | 29 | import org.springframework.aop.Advisor; |
| 30 | +import org.springframework.aop.framework.AopConfigException; |
28 | 31 | import org.springframework.beans.factory.BeanFactoryUtils; |
29 | 32 | import org.springframework.beans.factory.ListableBeanFactory; |
30 | 33 | import org.springframework.lang.Nullable; |
|
40 | 43 | */ |
41 | 44 | public class BeanFactoryAspectJAdvisorsBuilder { |
42 | 45 |
|
| 46 | + private static final Log logger = LogFactory.getLog(BeanFactoryAspectJAdvisorsBuilder.class); |
| 47 | + |
43 | 48 | private final ListableBeanFactory beanFactory; |
44 | 49 |
|
45 | 50 | private final AspectJAdvisorFactory advisorFactory; |
@@ -102,30 +107,37 @@ public List<Advisor> buildAspectJAdvisors() { |
102 | 107 | continue; |
103 | 108 | } |
104 | 109 | if (this.advisorFactory.isAspect(beanType)) { |
105 | | - aspectNames.add(beanName); |
106 | | - AspectMetadata amd = new AspectMetadata(beanType, beanName); |
107 | | - if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { |
108 | | - MetadataAwareAspectInstanceFactory factory = |
109 | | - new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); |
110 | | - List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); |
111 | | - if (this.beanFactory.isSingleton(beanName)) { |
112 | | - this.advisorsCache.put(beanName, classAdvisors); |
| 110 | + try { |
| 111 | + AspectMetadata amd = new AspectMetadata(beanType, beanName); |
| 112 | + if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { |
| 113 | + MetadataAwareAspectInstanceFactory factory = |
| 114 | + new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); |
| 115 | + List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); |
| 116 | + if (this.beanFactory.isSingleton(beanName)) { |
| 117 | + this.advisorsCache.put(beanName, classAdvisors); |
| 118 | + } |
| 119 | + else { |
| 120 | + this.aspectFactoryCache.put(beanName, factory); |
| 121 | + } |
| 122 | + advisors.addAll(classAdvisors); |
113 | 123 | } |
114 | 124 | else { |
| 125 | + // Per target or per this. |
| 126 | + if (this.beanFactory.isSingleton(beanName)) { |
| 127 | + throw new IllegalArgumentException("Bean with name '" + beanName + |
| 128 | + "' is a singleton, but aspect instantiation model is not singleton"); |
| 129 | + } |
| 130 | + MetadataAwareAspectInstanceFactory factory = |
| 131 | + new PrototypeAspectInstanceFactory(this.beanFactory, beanName); |
115 | 132 | this.aspectFactoryCache.put(beanName, factory); |
| 133 | + advisors.addAll(this.advisorFactory.getAdvisors(factory)); |
116 | 134 | } |
117 | | - advisors.addAll(classAdvisors); |
| 135 | + aspectNames.add(beanName); |
118 | 136 | } |
119 | | - else { |
120 | | - // Per target or per this. |
121 | | - if (this.beanFactory.isSingleton(beanName)) { |
122 | | - throw new IllegalArgumentException("Bean with name '" + beanName + |
123 | | - "' is a singleton, but aspect instantiation model is not singleton"); |
| 137 | + catch (IllegalArgumentException | IllegalStateException | AopConfigException ex) { |
| 138 | + if (logger.isDebugEnabled()) { |
| 139 | + logger.debug("Ignoring incompatible aspect [" + beanType.getName() + "]: " + ex); |
124 | 140 | } |
125 | | - MetadataAwareAspectInstanceFactory factory = |
126 | | - new PrototypeAspectInstanceFactory(this.beanFactory, beanName); |
127 | | - this.aspectFactoryCache.put(beanName, factory); |
128 | | - advisors.addAll(this.advisorFactory.getAdvisors(factory)); |
129 | 141 | } |
130 | 142 | } |
131 | 143 | } |
|
0 commit comments