- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定问题是否是因为我对 Spring 和 Jersey 使用了相同的类。但是每当我尝试创建一个 bean 时,我都会得到这个异常:
Feb 18, 2015 2:13:12 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Testing] in context with path [/testing] threw exception [com.sun.jersey.api.container.ContainerException: Unable to create resource class com.sample.test.TestWebService] with root cause
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1094)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
at com.sample.test.TestWebService.setServletContext(TestWebService.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.server.spi.component.ResourceComponentInjector.inject(ResourceComponentInjector.java:231)
at com.sun.jersey.server.spi.component.ResourceComponentConstructor.construct(ResourceComponentConstructor.java:180)
at com.sun.jersey.server.impl.resource.PerRequestFactory$PerRequest._getInstance(PerRequestFactory.java:182)
at com.sun.jersey.server.impl.resource.PerRequestFactory$AbstractPerRequest.getInstance(PerRequestFactory.java:144)
at com.sun.jersey.server.impl.application.WebApplicationContext.getResource(WebApplicationContext.java:238)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
这是我的 web.xml
的样子:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5" >
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-test.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sample.test</param-name>
<param-value>Testing</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Testing</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
这是 spring-test.xml
的样子(完全加载 :P):
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.6.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.6.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.6.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.6.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jdbc-3.0.6.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.6.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.6.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.6.xsd">
<context:component-scan base-package="com.sample.test"/>
<jee:jndi-lookup id="dataSource" jndi-name="java:/comp/evn/jdbc/localdb2"/>
<bean id="npJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
</beans>
这是我的 TestWebservice.java
,它使用 jersey 的 @Path:
package com.sample.test;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
@Path("test")
@Component
public class TestWebService {
private ServletContext sc;
@Autowired
private DataSource dataSource;
@Autowired
private NamedParameterJdbcTemplate npJdbcTemplate;
@Context
public void setServletContext(ServletContext context) {
this.sc = context;
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
/*
* none of these work, tried both
*/
//NamedParameterJdbcTemplate npJdbcTemplate = (NamedParameterJdbcTemplate) ctx.getBean("npJdbcTemplate");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
}
@GET
@Path("employee/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String getEmployee(@PathParam("id") String id) {
return id;
}
}
最佳答案
我认为你的 beans 文件是问题所在,当我使用它时我得到“长度:8”
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
>
<context:annotation-config />
<context:component-scan base-package="com.sample.test"/>
<!--
<jee:jndi-lookup id="dataSource" jndi-name="java:/comp/evn/jdbc/localdb2"/>
<bean id="npJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
-->
<!-- <bean id="testB" class="com.sample.test.TestB"></bean> -->
<!-- <bean id="testJdbc" class="com.sample.test.TestJdbc"> -->
<!--
<property name="npJdbcTemplate" ref="npJdbcTemplate" />
<property name="dataSource" ref="dataSource" />
-->
<!-- <property name="testB" ref="testB" />
</bean> -->
</beans>
关于java - 尝试将 Spring 与 Jersey 一起使用时出现 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28592841/
我正在获取 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring
我创建了一个库,用于在多个 Spring Boot 应用程序上共享代码。 该库包含一个 Repository 类 RequestRepository。将库添加到 Spring Boot 项目后,编译并
所以我尝试使用在另一个类中生成的 bean 以在主应用程序中使用 package com.simon.spring.basics.properties; import org.spri
我使用 spring-data-jpa。没有 Spring Boot。 package ru.spb.repository; public interface CrudUserRepository e
我尝试使用ClassPathXmlApplicationContext获取bean。但我收到:NoSuchBeanDefinitionException:未定义名为“newsController”的
请解释以下有关 Spring 中 NoSuchBeanDefinitionException 异常的信息: 是什么意思? 什么情况下会抛出? 我该如何预防? 这篇文章旨在对使用 Spring 的应用程
我刚刚使用 Spring Boot V1.3.5 设置了一个新项目,在尝试将存储库 Autowiring 到服务中时,我不断收到 NoSuchBeanDefinitionException 异常。这很
我在 Spring mvc 中遇到错误: (org.springframework.beans.factory.BeanCreationException:创建名称为“webController”的b
我正在使用 spring mvc 开发 Web 应用程序。我想将 spring bean 注入(inject)到我的 Servlet Filter 中。 我引用了这个教程http://www.dead
我们有一个(非 Web 应用程序)Spring 应用程序,在我们的 CruiseControl 持续集成 Linux 盒子上运行测试时会抛出 NoSuchBeanDefinitionException
我想在 BookRestController 中测试我的休息端点。我使用 @WebMvcTest 编写了一个测试。 @RunWith(SpringRunner.class) @WebMvcTest(B
我正在尝试按照官方教程开发我的第一个 Spring MVC 应用程序。我从模板创建了 MVC 项目,然后添加了以下 Spring Security 配置。 pom.xml: ... ...
我正在尝试使用 @RestClientTest 来测试休息客户端类。 据说: It will apply only configuration relevant to rest client test
我想使用ElasticSearch和Spring Data。我添加了此依赖项: org.springframework.boot spring-boot-starter-data-e
当我运行"grails dbm-update --dataSource=production"时,出现以下异常: org.springframework.beans.factory.NoSuchBea
我有一个 Java 桌面应用程序,它通过 Hibernate 连接到 MySql 数据库。 我有以下配置文件: pom.xml,我在其中放置了一些内容和数据库配置文件 [app_name]_conte
我有一个使用 Quartz 调度程序的 Spring Boot (2.1.3) 项目。它是通过启动器包含的: dependencies { ... implementation('or
我正在从事 Spring Hibernate 项目。如果我没有将 bean 名称与 @Repository 或 @Component 一起提及,我会收到错误“NoSuchBeanDefinitionE
我的多模块项目结构如下: project-example (parent) -- project-example-domain: contains DAOs and Services -- proje
我有一个 Spring Boot 应用程序。当我调用 context.getBean(MyController.class) 时,它工作正常。当我调用 context.getBean("MyContr
我是一名优秀的程序员,十分优秀!