jamesfredley commented on code in PR #16709:
URL: https://github.com/apache/grails-core/pull/16709#discussion_r4342064333


##########
grails-core/src/main/groovy/org/apache/grails/core/aot/GeneratedArtifactsAutowiredAnnotationBeanPostProcessor.java:
##########
@@ -0,0 +1,75 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.grails.core.aot;
+
+import org.jspecify.annotations.Nullable;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.PropertyValues;
+import org.springframework.beans.factory.BeanFactory;
+import 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
+import org.springframework.beans.factory.support.RootBeanDefinition;
+
+/**
+ * Injects through {@code @Autowired}, {@code @Value} and {@code @Inject} what 
the code generated ahead of time
+ * did not: an instance that code created only through the members its class 
adds to the type the definition
+ * names, and every other bean in full.
+ *
+ * @since 8.0.1
+ * @see GeneratedBeanDefinitions
+ */
+public class GeneratedArtifactsAutowiredAnnotationBeanPostProcessor extends 
AutowiredAnnotationBeanPostProcessor {
+
+    /**
+     * Spring's own, as the code was generated with: it finds the members that 
code injected, and injects in full
+     * another instance of a generated bean.
+     */
+    private final AutowiredAnnotationBeanPostProcessor generatingProcessor = 
new AutowiredAnnotationBeanPostProcessor();
+
+    @Nullable
+    private BeanFactory beanFactory;
+
+    @Override
+    public void setBeanFactory(BeanFactory beanFactory) {
+        super.setBeanFactory(beanFactory);
+        this.generatingProcessor.setBeanFactory(beanFactory);
+        this.beanFactory = beanFactory;
+    }
+
+    @Override
+    public void postProcessMergedBeanDefinition(RootBeanDefinition 
beanDefinition, Class<?> beanType, String beanName) {
+        
GeneratedBeanDefinitions.markMembersInjectedByGeneratedCode(beanDefinition, 
beanType, generatingProcessor);
+        super.postProcessMergedBeanDefinition(beanDefinition, beanType, 
beanName);
+    }
+
+    @Override
+    public void resetBeanDefinition(String beanName) {
+        super.resetBeanDefinition(beanName);
+        generatingProcessor.resetBeanDefinition(beanName);
+    }
+
+    @Override
+    public PropertyValues postProcessProperties(PropertyValues pvs, Object 
bean, String beanName) throws BeansException {
+        if 
(GeneratedBeanDefinitions.isAnotherInstanceOfGeneratedBean(beanFactory, 
beanName, bean)) {

Review Comment:
   `configureBean(new Foo(), generatedBeanName)` on a field annotated with both 
`@Resource(name = "named")` and `@Autowired` is injected by both fallback 
processors. They do not share a merged definition, so autowiring can replace 
the named resource with a different primary bean, and a dual-annotated 
non-setter method runs twice.
   
   Coordinate the externally managed members before the full injection. Please 
add a regression with both annotations on one field and assert the named 
resource wins and the method runs once.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to