by Anonymous » 06 Feb 2025, 06:24
Ich lerne Bean-Konzepte mithilfe von XML -basierte Konfiguration, aber ich habe ein Problem mit der @Qualifier Annotation.
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();
}
Wenn ich die Hauptmethode oben ausführe, erhalte ich den folgenden Fehler:
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)
meine vollständigen Beans.xml Datei
Hinweis:
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)
Ich bin ziemlich neu im Frühling - also würde jede Hilfe geschätzt.
Ich lerne Bean-Konzepte mithilfe von XML -basierte Konfiguration, aber ich habe ein Problem mit der @Qualifier Annotation.
Hier ist meine Klasse: < /p>
[code]@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]
Wenn ich die Hauptmethode oben ausführe, erhalte ich den folgenden Fehler:
[code]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]
meine vollständigen Beans.xml Datei
[code]
[/code]
[b] Hinweis: [/b]
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]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)
[/code]
Ich bin ziemlich neu im Frühling - also würde jede Hilfe geschätzt.