spring项目启动服务器找不到bean的解决问题

今天在配置springMVC项目框架,启动tomcat,然后我的一个对象注入失败,找不到bean,如下:

Error creating bean with name 'userController': Injection of resource dependenci.......

解决办法如下:

这种问题我遇到2次:

1、提示我搭建项目使用的是原生dao层,结果是SQL文件里面namespace的地址错误

修改地址之后就可以了。

2、这种就比较难找了,具体不说了,直接上方案:

原来是我的web.xml文件少写了这个监听器

加上去就可以了。

org.springframework.web.context.ContextLoaderListener的作用:

ContextLoaderListener这个监听器继承自ContextLoader并且实现了ServletContextListener,他的主要作用是去寻找并读取spring主配置文件ApplicationContext.xml(也就是context-param中所定义的contextConfigLocation),然后启动WebApplicationContext,也可叫做web应用上下文,并且最重要的是,它将WebApplicationContext注入到servletContext容器中(作为servletContext的一个attribute,属性),并且在WebApplicationContext中保留了一个servletContext的引用。

 

所以我们可以通过WebApplicationContext得到servletContext,也可以通过servletContext获取到WebApplicationContext。

  通过WebApplicationContext得到servletContext:

  WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();

     ServletContext servletContext = webApplicationContext.getServletContext();

  通过servletContext获取WebApplicationContext:

  ServletContext servletContext = event.getServletContext();

  ApplicationContext application = WebApplicationContextUtils .getWebApplicationContext(servletContext);

 

后记:项目配置很多坑,不小心掉进去很难找到问题。所以以后写配置一定要仔细。。

如果在使用 Spring Boot 中,@EnableSwaggerBootstrapUI 注解无法启动服务器,通常有以下几个可能的原因: 1. 项目中没有添加 Swagger 和 Swagger UI 的依赖,可以在 pom.xml 文件中添加以下依赖: ```xml <!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <!-- Swagger UI --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <!-- Swagger Bootstrap UI --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency> ``` 2. 项目中的 Swagger 配置有误。可以在 Swagger 的配置类中添加以下代码: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } ``` 3. 如果启用了 Spring Security,需要在 Security 配置类中添加以下代码: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 允许 Swagger UI 页面的所有请求 http.authorizeRequests().antMatchers("/swagger-ui.html", "/doc.html", "/swagger-resources/**", "/v2/api-docs").permitAll(); // 关闭 CSRF 防护 http.csrf().disable(); } } ``` 4. 如果以上方法都无法解决问题,可以尝试查看控制台输出的异常信息,以便更好的定位问题。 希望以上方法可以帮助到你。如果还有问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值