1. 程式人生 > >java報錯-->Error creating bean with name 'org.springframework.web.servlet.resource.ResourceHttpRequest

java報錯-->Error creating bean with name 'org.springframework.web.servlet.resource.ResourceHttpRequest

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0’: Invocation of init method failed; nested exception is java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[class path resource [WEB-INF/css/]], resolvers=[

[email protected]7a1e2d]]] does not run in a WebApplicationContext but in: [email protected]ee68d8: startup date [Sun Jul 30 18:48:01 CST 2017]; root of context hierarchy

解決辦法是新增如下註解:@WebAppConfiguration(“src/main/resources”)
註解括號的路徑是xml配置檔案的路徑

看報錯是靜態資源的Handler的ResourceHttpRequestHandler不能建立,正確程式碼如下:

package space.xxhui;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework
.test.context.web.WebAppConfiguration; import space.xxhui.POJO.User; import space.xxhui.service.UserService; import java.util.logging.Logger; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath:spring/spring-config.xml"}) @WebAppConfiguration("src/main/resources") public class UserDAOTest3 { @Autowired private UserService userService; private final Logger logger = Logger.getLogger(String.valueOf(getClass())); @Test public void TestUserSerivce(){ logger.info("begin........"); User user = new User(); user.setName("Hitvz"); user.setPwd("123456"); User result = userService.checkUser(user); if(result!=null){ System.out.println(result.toString()); } logger.info("end........"); } }

報瞭如開頭引用的錯誤,這裡解釋一下:

@WebAppConfiguration註解在類上,
用來宣告載入的ApplicationContext是一個WebApplicationContext。
它的屬性指定的是Web資源的位置,預設為src/main/webapp

這裡把他修改為src/main/resources,指定配置檔案路徑

所以解決辦法就是指定到配置檔案的根路徑,這裡找不到根路徑,也就找不到Spring-config.xml的配置吧。所以加上更改根路徑的註解@WebAppConfiguration(“src/main/resources”)就好了。

大概意思就是要有根目錄:@WebAppConfiguration(“src/main/resources”),接著從根目錄找到你的配置檔案:@ContextConfiguration({ “classpath:spring/spring-config.xml”})

這裡寫圖片描述

相關推薦

java-->Error creating bean with name 'org.springframework.web.servlet.resource.ResourceHttpRequest

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.web.servl

Spring Aop error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator

使用Spring Aop程式設計除了要加入Spring的spring-aop jar包,還要加入aspectjrt.jar,aspectjweaver.jar,以及aopalliance_1.0.jar,否則報錯: org.springframework.beans.fac

idea Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource

ror factory pojo mapped clas mes del strong per 核對一下控制器是不是寫了相同的路徑。。。org.springframework.beans.factory.BeanCreationException: Error crea

Spring注入bean Error creating bean with name的網上找不到的解決方案

                        今天在研究Bas

SpringCloud-Eureka Error creating bean with name解決

報錯:Error creating bean with name  解決辦法 在pom檔案中新增以下依賴 <!--Error creating bean with name 補丁依賴--> <dependency>

Spring boot 採用Cxf 整合WebService 執行Error creating bean with name 'endpoint' defined ”

Spring boot 採用Cxf 整合WebService 執行報錯“Error creating bean with name ‘endpoint’ defined ” 由於新專案的需要,專案框架採用Spring boot 1.5.13.RELEASE、j

SpringMVC查詢出現500,Error creating bean with name 'menuController': Injection of autowired dependency

解決方案:這是因為在service.impl包下的IEmpServiceImpl中沒有加@server,在控制層呼叫業務層,               &n

springboot 啟動的時候 Error creating bean with name 'solrClient'

springboot 啟動的時候報錯: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'solrClient' defined in class path resource [o

ssm專案整合中遇到的tomcat啟動的時候Error creating bean with name 'xxxController'

今天學習整合ssm專案在測試框架是否可用時,一執行maven的tomcat就報上面的錯誤, 一開始還以為是service層註解沒有寫好,後面檢查發現沒有錯誤,最後是在web.xml檔案中發現下面的param-value寫錯了,applicationContext-*.xml

spring boot 專案 redis啟動 Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer'

先說一下解決辦法 修改配置redis.conf檔案 C:\Users\coffee>C:\Java\Redis-x64-3.2.100\redis-server.exe redis.conf 把redis.conf的配置檔案的註釋改為 # not

SSM使用高版本的spring(我用的5.0.8),Error creating bean with name 'requestMappingHandlerAdapter' defined in

Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/web/servlet/config/annota

PageHelperError creating bean with name sqlSessionFactory defined in class path resource

2017-08-17 21:20:16.618 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] ERROR org.springframework.web.context.ContextLoa

Spring AOPError creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0

問題 在使用junit對spring aop做測試時,報錯如下: [email protected]49438269] java.lang.IllegalStateException: Failed to load ApplicationCon

解決spring cloud eureka服務註冊出現的Error creating bean with name 'org.springframework.cloud.netflix...問題

異常如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.netflix.eureka.Eu

spring boot maven install時報 Error creating bean with name 'serverEndpointExporter'。。。。。

 maven install時 自動啟動專案 並報錯:    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExpo

spring cloud 測試的時候 BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServic

因為都能正確的跑測試方法,所以我也不太注意它,但是有時候閒得蛋疼就會找一下原因。具體原因我也說不清,直接丟個連線https://github.com/spring-cloud/spring-cloud-netflix/issues/1952裡面的一位叫@Component p

spring AOP使用中Error creating bean with name ‘…’defined in class path resource..問題及其解決方法

factory fail 正常 ins aspect 工作內容 thread def before spring AOP使用中Error creating bean with name ‘…’defined in class path resource..問題及其解決方法

解決org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spring

     從svn上取下來的之前一直在開發的專案,因為專案我是中途接手的,所以同步下來,配置好tomcat,執行的時候報錯。 Cannot find class [com.lhzxt.dhub.MyExceptionHandler] for bean with name

Spring異常:Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classe

Spring Boot Error creating bean with name 'entityManagerFactory' defined in class path resource

fine 依賴 debug isp jpa java idt .com ima spring boot 寫一個web項目,在使用spring-data-jpa的時候,啟動報如下錯誤: Error starting ApplicationContext. To display