Skip to content

Commit f9734a3

Browse files
committed
Add support for @Component to TypeResolverHelper
1 parent 01fa511 commit f9734a3

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

spring-ai-core/src/main/java/org/springframework/ai/model/function/TypeResolverHelper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ else if (isParamMismatch(uniqueCandidate, candidate)) {
152152
rootBeanDefinition.setResolvedFactoryMethod(uniqueCandidate);
153153
return rootBeanDefinition.getResolvableType();
154154
}
155+
// Support for @Component
156+
if (beanDefinition.getFactoryMethodName() == null && beanDefinition.getBeanClassName() != null) {
157+
try {
158+
return ResolvableType.forClass(
159+
ClassUtils.forName(beanDefinition.getBeanClassName(), applicationContext.getClassLoader()));
160+
}
161+
catch (ClassNotFoundException ex) {
162+
throw new IllegalArgumentException("Impossible to resolve the type of bean " + beanName, ex);
163+
}
164+
}
155165
throw new IllegalArgumentException("Impossible to resolve the type of bean " + beanName);
156166
}
157167

spring-ai-core/src/test/java/org/springframework/ai/model/function/TypeResolverHelperIT.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
import static org.assertj.core.api.Assertions.assertThat;
3333

3434
@SpringBootTest
35-
class TypeResolverHelperIT {
35+
public class TypeResolverHelperIT {
3636

3737
@Autowired
3838
GenericApplicationContext applicationContext;
3939

4040
@ParameterizedTest(name = "{0} : {displayName} ")
4141
@ValueSource(strings = { "weatherClassDefinition", "weatherFunctionDefinition", "standaloneWeatherFunction",
42-
"scannedStandaloneWeatherFunction" })
42+
"scannedStandaloneWeatherFunction", "componentWeatherFunction" })
4343
void beanInputTypeResolutionWithResolvableType(String beanName) {
4444
assertThat(this.applicationContext).isNotNull();
4545
ResolvableType functionType = TypeResolverHelper.resolveBeanType(this.applicationContext, beanName);
@@ -70,7 +70,8 @@ public WeatherResponse apply(WeatherRequest weatherRequest) {
7070
}
7171

7272
@Configuration
73-
@ComponentScan("org.springframework.ai.model.function.config")
73+
@ComponentScan({ "org.springframework.ai.model.function.config",
74+
"org.springframework.ai.model.function.component" })
7475
public static class TypeResolverHelperConfiguration {
7576

7677
@Bean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2023-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.model.function.component;
18+
19+
import java.util.function.Function;
20+
21+
import org.springframework.ai.model.function.TypeResolverHelperIT.WeatherRequest;
22+
import org.springframework.ai.model.function.TypeResolverHelperIT.WeatherResponse;
23+
import org.springframework.stereotype.Component;
24+
25+
/**
26+
* @author Sebastien Deleuze
27+
*/
28+
@Component
29+
public class ComponentWeatherFunction implements Function<WeatherRequest, WeatherResponse> {
30+
31+
@Override
32+
public WeatherResponse apply(WeatherRequest weatherRequest) {
33+
return new WeatherResponse(42.0f);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)