Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

前言

本案例适合使用的IDE是MyEclipse的小伙伴参考,解决思路中的某个步骤对你有所启发。

环境:jdk 1.7 maven-3.2.3  工程启动时报错。

报错原因,报错的接口类的具体实现类没有真正的成功发布到Tomcat。

给使用IDEA工具小伙伴的一点提示

如果你使用的是IDEA,本篇对你的帮助不是很大,不过可以告诉你一些解决思路

该错误往往是因为:

被注解的接口类,在项目启动进行IOC容器自动装配时,找不到它的具体实现类,或者找到了该接口的多个实现类所致。

例如:LxbxService被注解了 @Autowired,在工程启动时,它就会自动寻找它的具体实现类,如果没有找到或者找到多个,都有可能会报这个错误。

如果你明确的知道,这个接口又多个实现类,则在注解接口的时候,需要指明你要使用哪一个实现类,如下所示:

public interface LxbxService{
}

@service("lxbxServiceSh")
public class LxbxServiceShImpl implements LxbxService{
}

@service("lxbxServiceSp")
public class LxbxServiceSpImpl implements LxbxService{
}

public LxbxController {
    @Autowired
    @Qualifier("lxbxServiceSp")//这里就需要指明用的是哪一个实现类
    private LxbxService lxbxService;
}

当然,这两个注解也可以替换为Resource一个注解:

public LxbxController {
	@Resource(name="lxbxServiceSp")
	private LxbxService lxbxService;
}

¥¥使用IDEA的小伙伴,看到这里就可以了,下面的篇幅留给使用MyEclipse的小伙伴参考!¥¥


报错原因:报错接口,其具体实现类所在的工程没有被导入,下面围绕该话题展开!


一、解决步骤

1、主工程Controller层代码截图

2、打开Tomcat,展开,发现Service层的工程包没有被引入进来

后面的步骤,围绕这个问题依次展开!

3、彻查该报错接口所需的依赖工程,是否被引入了

4、小节

看到这里,发现这不是我的错,是MyEclipse工具,哪根神经出问题了,也就是说LxbxService接口的实现类所在的工程包已经被引进来了,就是报错,通过下面的继续查看,得知,是该实现类编译后没有被装配到Tomcat中。

5、绝招,进入项目的工作空间的源码,删除除了src和pom.xml文件以外的所有文件,重新导入整个项目

1)删除除了src和pom.xml文件以外的所有文件

2)移除工程下面报错的那个模块,然后重新导入该模块

移除该工程,不是删除,下面是重新导入该模块

3)从远程仓库,找到Lxbx-service模块重新导入本地工程

4)把该工程重新添加进来,重复发布工程至Tomcat,问题到此解决

总结

1、问题回顾

由于本项目是多工程 controller层和 持久层 以及 接口层 完全分离,不在同一个模块中(每个模块由公司的不同开发小组完成)。

发布到Tomcat后,如果某个工程(接口的实现类)因某种原因,没有以jar/war包的方式没引用到主工程,那么Spring注解就会发生此类错误。

代码本身没有问题,问题出在LxbxCxService是一个接口,它需要自动装配他的实现类LxbxCxServiceImpl,而实现类在另一个工程(hnsi-jmyb-service)里没有被以jar包的方式引入进来(实时证明,打开Tomcat的工程发布的文件夹,该依赖确实没有被发布成功),当然就要报错了。

2、小节

问题出现的根源,就是在项目启动时,在Controller代码中,用@Autowired 注解的 LxbxCxService接口找不到自己的实现类所致。

围绕它,展开一些列排查即可,其中最先想到的就是进入Tomcat查找其实现类是否发布成功(实际上并没有),最后用最简单最暴力的方式,删除工程重新导入,问题得以解决。

尾言

解决问题,首先需要了解该错误报错的根源,顺藤摸瓜,尽量以最简单、最快的方式把问题解决,哪怕粗暴一点也无妨。

有时候的错误,明明知道报错原因,就是难以解决,这样的问题伤害不大,侮辱性极强,先暴力解决就好!!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Dependency Injection Principles, Practices, and Patterns teaches you to use DI to reduce hard-coded dependencies between application components. You'll start by learning what DI is and what types of applications will benefit from it. Then, you'll work through concrete scenarios using C# and the .NET framework to implement DI in your own projects. As you dive into the thoroughly-explained examples, you'll develop a foundation you can apply to any of the many DI libraries for .NET and .NET Core. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology Dependency Injection (DI) is a great way to reduce tight coupling between software components. Instead of hard-coding dependencies, such as specifying a database driver, you make those connections through a third party. Central to application frameworks like ASP.NET Core, DI enables you to better manage changes and other complexity in your software. About the Book Dependency Injection Principles, Practices, and Patterns is a revised and expanded edition of the bestselling classic Dependency Injection in .NET. It teaches you DI from the ground up, featuring relevant examples, patterns, and anti-patterns for creating loosely coupled, well-structured applications. The well-annotated code and diagrams use C# examples to illustrate principles that work flawlessly with modern object-oriented languages and DI libraries. What's Inside Refactoring existing code into loosely coupled code DI techniques that work with statically typed OO languages Integration with common .NET frameworks Updated examples illustrating DI in .NET Core About the Reader For intermediate OO developers.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值