gpt4 book ai didi

java - Spring中使用注释 Autowiring

转载 作者:行者123 更新时间:2023-12-02 02:31:16 25 4
gpt4 key购买 nike

在我的简单 Spring 项目中,我有一个名为 Property 的类 bean:

@Component
public class Property implements DisposableBean,InitializingBean{

private int square_area;
private int floor_count;

@Autowired
@Qualifier("myLocation")
private Location location;

public Property() {
super();
}

@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Property prop(){
return new Property(100,3);
}

public Property(int square_area, int floor_count, Location location) {
this.square_area = square_area;
this.floor_count = floor_count;
this.location = location;
}

public Property(int square_area, int floor_count) {
this.square_area = square_area;
this.floor_count = floor_count;
this.location = location;
}


public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub

}

public void destroy() throws Exception {
// TODO Auto-generated method stub

}

public int getSquare_area() {
return square_area;
}

public void setSquare_area(int square_area) {
this.square_area = square_area;
}

public int getFloor_count() {
return floor_count;
}

public void setFloor_count(int floor_count) {
this.floor_count = floor_count;
}


public Location getLocation() {
return location;
}


public void setLocation(Location location) {
this.location = location;
}

@Override
public String toString() {
return "Property [square_area=" + square_area + ", floor_count=" + floor_count + ", location=" + location + "]";
}

public void gettingDusty(){
System.out.println("Im getting dusty | " + this);
}

}

我正在尝试 Autowiring 字段“位置”。位置类如下所示:

@Component
public class Location {

private String country;
private String region;
private String settlement;
private String street;
private int house;

@Bean("myLocation")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Location myLocation(){
return new Location("US","California","Some peacefull town","Some peacefull street",1);
}

public Location(String country, String region, String settlement, String street, int house) {
super();
this.country = country;
this.region = region;
this.settlement = settlement;
this.street = street;
this.house = house;
}

@Override
public String toString() {
return "Location [country=" + country + ", region=" + region + ", settlement=" + settlement + ", street="
+ street + ", house=" + house + "]";
}
}

我收到了 org.springframework.beans.factory.UnsatisfiedDependencyException:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'property': Unsatisfied dependency expressed through field 'location'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'location' defined in file [C:\Users\TheDanileron\spring_tests\spring_test4\target\classes\spring_tests\spring_test3\Location.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:367)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1340)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1249)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1098)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:330)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
at spring_tests.spring_test3.App.main(App.java:25)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'location' defined in file [C:\Users\TheDanileron\spring_tests\spring_test4\target\classes\spring_tests\spring_test3\Location.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1269)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1126)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1133)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1060)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:578)
... 18 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1501)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1099)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1060)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:809)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:715)
... 31 more

如何使用 Location.class 中的 myLocation 方法创建的 bean Autowiring 字段“location”?

最佳答案

您在 Location 上有一个 @Component 注释,因此 Spring 尝试使用需要参数的构造函数将其创建为 bean。您的应用程序上下文不提供这些参数,因此 Bean 创建失败。

您似乎只想以编程方式配置 Location 的实例。然后从 Location 中删除 @Component 并创建一个单独的配置类,如 LocationConfiguration:

@Configuration
public class LocationConfiguration {

@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Location myLocation(){
return new Location("US","California","Some peacefull town","Some peacefull street",1);
}
}

关于java - Spring中使用注释 Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47044159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com