diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGenerator.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGenerator.java index 0afc2419a8..c5c863df73 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGenerator.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -68,10 +68,10 @@ public class AutowiredArgumentsCodeGenerator { for (int i = startIndex; i < parameterTypes.length; i++) { code.add(i > startIndex ? ", " : ""); if (!ambiguous) { - code.add("$L.get($L)", variableName, i - startIndex); + code.add("$L.get($L)", variableName, i); } else { - code.add("$L.get($L, $T.class)", variableName, i - startIndex, parameterTypes[i]); + code.add("$L.get($L, $T.class)", variableName, i, parameterTypes[i]); } } return code.build(); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGeneratorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGeneratorTests.java index 7f35baca32..e53314ac63 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGeneratorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGeneratorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2025 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. @@ -67,7 +67,7 @@ class AutowiredArgumentsCodeGeneratorTests { AutowiredArgumentsCodeGenerator generator = new AutowiredArgumentsCodeGenerator( Outer.Nested.class, constructor); assertThat(generator.generateCode(constructor.getParameterTypes(), 1)) - .hasToString("args.get(0), args.get(1)"); + .hasToString("args.get(1), args.get(2)"); } @Test diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java index 0c4baa84a0..cddfb5dd2b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -130,11 +130,13 @@ class InstanceSupplierCodeGeneratorTests { @Test void generateWhenHasConstructorWithInnerClassAndParameter() { BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponent.class); + StandardEnvironment environment = new StandardEnvironment(); this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration()); - this.beanFactory.registerSingleton("environment", new StandardEnvironment()); + this.beanFactory.registerSingleton("environment", environment); compile(beanDefinition, (instanceSupplier, compiled) -> { Object bean = getBean(beanDefinition, instanceSupplier); assertThat(bean).isInstanceOf(EnvironmentAwareComponent.class); + assertThat(bean).hasFieldOrPropertyWithValue("environment", environment); assertThat(compiled.getSourceFile()).contains( "getBeanFactory().getBean(InnerComponentConfiguration.class).new EnvironmentAwareComponent("); }); @@ -158,11 +160,13 @@ class InstanceSupplierCodeGeneratorTests { @Test void generateWhenHasNonPublicConstructorWithInnerClassAndParameter() { BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponentWithoutPublicConstructor.class); + StandardEnvironment environment = new StandardEnvironment(); this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration()); - this.beanFactory.registerSingleton("environment", new StandardEnvironment()); + this.beanFactory.registerSingleton("environment", environment); compile(beanDefinition, (instanceSupplier, compiled) -> { Object bean = getBean(beanDefinition, instanceSupplier); assertThat(bean).isInstanceOf(EnvironmentAwareComponentWithoutPublicConstructor.class); + assertThat(bean).hasFieldOrPropertyWithValue("environment", environment); assertThat(compiled.getSourceFile()).doesNotContain( "getBeanFactory().getBean(InnerComponentConfiguration.class)"); }); diff --git a/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java index 6f0b03c83e..6c41ae67a6 100644 --- a/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -28,7 +28,10 @@ public class InnerComponentConfiguration { public class EnvironmentAwareComponent { + private final Environment environment; + public EnvironmentAwareComponent(Environment environment) { + this.environment = environment; } } @@ -40,7 +43,10 @@ public class InnerComponentConfiguration { public class EnvironmentAwareComponentWithoutPublicConstructor { + private final Environment environment; + EnvironmentAwareComponentWithoutPublicConstructor(Environment environment) { + this.environment = environment; } }