gpt4 book ai didi

java - 如何修复 "No qualifying bean of type ' com.newmvc.demo.queue.NewSender' 可用”

转载 作者:行者123 更新时间:2023-12-05 05:09:53 25 4
gpt4 key购买 nike

我是 spring boot 技术的新手,我正在构建微服务,我在 spring boot 中使用 jmstemplate 发送和接收消息,但我收到错误消息“没有可用的‘com.newmvc.demo.queue.NewSender’类型的合格 bean”和我在 NewSender 类中声明了@Component

我已经厌倦了 @Component 注释,它仍然显示相同的错误

@SpringBootApplication
@EnableJms
public class Project1Application {
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}

public static void main(String[] args) {

SpringApplication.run(Project1Application.class, args);

}}

这是我的主要应用程序,Project1Application.class

package com.newmvc.demo.queue;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class MessageInt {
public static String Sendmessage(String xml) {
ApplicationContext context = new AnnotationConfigApplicationContext(MessageInt.class);
NewSender sender = context.getBean(NewSender.class);
sender.send("msgQueue", xml);
return xml;
}}

这是我的消息初始化类

@Component
public class NewSender {
@Autowired
private JmsTemplate jmsTemplate;

public void send(final String queue, final String msg) {
jmsTemplate.convertAndSend(queue, msg);
}
}

这是我的发送者类

  @Component
public class NewRecevier {

@JmsListener(destination = "msgQueue", containerFactory = "jmsListenerContainerFactory")
public void receiveMessage(String xml) throws Exception {
System.out.println("Message Received\n\n");
System.out.println("Message received from Queue: " + xml);

}
}

这是我的接收类

在我的 Controller 类中,我添加了@Componentscan{com.newmvc.demo.queue}

我希望在接收端接收字符串输出

我遇到这样的错误

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.newmvc.demo.queue.NewSender' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:343) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at com.newmvc.demo.queue.MessageInt.SendLog(MessageInt.java:15) ~[classes/:na]
at newgateway.Gatewayy.updateLogs(Gatewayy.java:41) ~[classes/:na]
at org.apache.jsp.updateresults_jsp._jspService(updateresults_jsp.java:135) ~[na:na]
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) ~[tomcat-jasper-9.0.16.jar:9.0.16]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476) ~[tomcat-jasper-9.0.16.jar:9.0.16]
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) ~[tomcat-jasper-9.0.16.jar:9.0.16]
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) ~[tomcat-jasper-9.0.16.jar:9.0.16]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415) [tomcat-embed-core-9.0.17.jar:9.0.17]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.17.jar:9.0.17]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_181]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.17.jar:9.0.17]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_181]

请帮我解决这个错误,

提前致谢,如有任何帮助,我们将不胜感激

最佳答案

天哪,这里有一些问题。

您的错误消息的主要原因是,您正在构造一个只有一个类/bean 的 ApplicationContext,并试图在其中搜索另一个 bean (NewSender)。

您必须作为类的成员/字段来执行此操作并将其定义为静态的:

ApplicationContext context = new AnnotationConfigApplicationContext(MessageInt.class);

并且尽量不要以包含您需要的每个 bean 的方式创建 ApplicationContext。所以像这样:

private static ApplicationContext context = new AnnotationConfigApplicationContext("com.mycompany.myfirm.myproject.mybeans");

关于java - 如何修复 "No qualifying bean of type ' com.newmvc.demo.queue.NewSender' 可用”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57136402/

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