Hier ist meine Klasse: < /p>
Code: Select all
@Getter
@Setter
public class CircleWithAnnotation implements Shape {
@Autowired
// @Qualifier("circleRelated")
@Qualifier("zeroCenter") // This also refers to the same bean as "circleRelated"
private Point center;
@Resource(name = "zeroCenter")
private Point centerX; // This refers to the same bean as "zeroCenter" field above
public void draw() {
System.out.println("Circle drawn: \n"
+ "center = (" + center.getX() + ", " + center.getY() + ")\n"
);
System.out.println("Circle with centerX drawn: \n"
+ "centerX = (" + centerX.getX() + ", " + centerX.getY() + ")\n"
);
}
}
public class DrawingApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
context.registerShutdownHook();
CircleWithAnnotation circleWithAnnotation = (CircleWithAnnotation) context.getBean("circleWithAnnotation");
circleWithAnnotation.draw();
}
Code: Select all
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'circleWithAnnotation': Unsatisfied dependency expressed through field 'center': No qualifying bean of type 'in.abc.xyz.practice.javabrains.spring.shapes.Point' available: expected single matching bean but found 7: zeroPoint,point2,point3,pointA,pointB,pointC,zeroCenter
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'circleWithAnnotation': Unsatisfied dependency expressed through field 'center': No qualifying bean of type 'in.abc.xyz.practice.javabrains.spring.shapes.Point' available: expected single matching bean but found 7: zeroPoint,point2,point3,pointA,pointB,pointC,zeroCenter
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:788)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:768)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:146)
Code: Select all
Keine der @Qualifier s über funktioniert, aber die @resource Annotation funktioniert! XML Datei (wie in vielen anderen Antworten hier auf Stack-Overflow angegeben), aber ohne Erfolg. In diesem Fall erhalte ich den folgenden Fehler: < /li>
< /ol>
Code: Select all
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'circleWithAnnotation': Unsatisfied dependency expressed through field 'center': No qualifying bean of type 'in.abc.xyz.practice.javabrains.spring.shapes.Point' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("zeroCenter")}
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'circleWithAnnotation': Unsatisfied dependency expressed through field 'center': No qualifying bean of type 'in.abc.xyz.practice.javabrains.spring.shapes.Point' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("zeroCenter")}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:788)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:768)