|
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;
|
@@ -103,30 +108,37 @@ public List<Advisor> buildAspectJAdvisors() {
|
103 | 108 | continue;
|
104 | 109 | }
|
105 | 110 | if (this.advisorFactory.isAspect(beanType)) {
|
106 |
| - aspectNames.add(beanName); |
107 |
| - AspectMetadata amd = new AspectMetadata(beanType, beanName); |
108 |
| - if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { |
109 |
| - MetadataAwareAspectInstanceFactory factory = |
110 |
| - new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); |
111 |
| - List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); |
112 |
| - if (this.beanFactory.isSingleton(beanName)) { |
113 |
| - this.advisorsCache.put(beanName, classAdvisors); |
| 111 | + try { |
| 112 | + AspectMetadata amd = new AspectMetadata(beanType, beanName); |
| 113 | + if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { |
| 114 | + MetadataAwareAspectInstanceFactory factory = |
| 115 | + new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); |
| 116 | + List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); |
| 117 | + if (this.beanFactory.isSingleton(beanName)) { |
| 118 | + this.advisorsCache.put(beanName, classAdvisors); |
| 119 | + } |
| 120 | + else { |
| 121 | + this.aspectFactoryCache.put(beanName, factory); |
| 122 | + } |
| 123 | + advisors.addAll(classAdvisors); |
114 | 124 | }
|
115 | 125 | else {
|
| 126 | + // Per target or per this. |
| 127 | + if (this.beanFactory.isSingleton(beanName)) { |
| 128 | + throw new IllegalArgumentException("Bean with name '" + beanName + |
| 129 | + "' is a singleton, but aspect instantiation model is not singleton"); |
| 130 | + } |
| 131 | + MetadataAwareAspectInstanceFactory factory = |
| 132 | + new PrototypeAspectInstanceFactory(this.beanFactory, beanName); |
116 | 133 | this.aspectFactoryCache.put(beanName, factory);
|
| 134 | + advisors.addAll(this.advisorFactory.getAdvisors(factory)); |
117 | 135 | }
|
118 |
| - advisors.addAll(classAdvisors); |
| 136 | + aspectNames.add(beanName); |
119 | 137 | }
|
120 |
| - else { |
121 |
| - // Per target or per this. |
122 |
| - if (this.beanFactory.isSingleton(beanName)) { |
123 |
| - throw new IllegalArgumentException("Bean with name '" + beanName + |
124 |
| - "' is a singleton, but aspect instantiation model is not singleton"); |
| 138 | + catch (IllegalArgumentException | IllegalStateException | AopConfigException ex) { |
| 139 | + if (logger.isDebugEnabled()) { |
| 140 | + logger.debug("Ignoring incompatible aspect [" + beanType.getName() + "]: " + ex); |
125 | 141 | }
|
126 |
| - MetadataAwareAspectInstanceFactory factory = |
127 |
| - new PrototypeAspectInstanceFactory(this.beanFactory, beanName); |
128 |
| - this.aspectFactoryCache.put(beanName, factory); |
129 |
| - advisors.addAll(this.advisorFactory.getAdvisors(factory)); |
130 | 142 | }
|
131 | 143 | }
|
132 | 144 | }
|
|
0 commit comments