springmvc注入jar包中的类

changerzhuo 2017-02-28 10:57:53
背景:
springdemo是一个小的spring项目, 可以从容器中获取bean并操作。 将springdemo项目打成jar包, 放入到spring_mybatis项目中, 在spring_mybatis项目中不需要在特意配置springdemo的任何bean(因为在spring_mybatis项目的web.xml文件中使用了classpath*:spring-*.xml),而直接使用。 例如, 在spring_mybatis中直接使用springdemo中的UserController。
1. springdemo项目,
项目结构


Controller


spring-content.xml文件配置


2. spring_mybatis项目
将springdemo项目打成jar包, 并添加到spring_mybatis项目中


spring_mybatis项目中web.xml文件加载spring配置文件的配置


以上是项目主要内容。 其实主要的问题就在于, 使用classpath*:spring-*.xml能不能加载jar包中的spring-*.xml
...全文
1835 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
changerzhuo 2017-03-10
  • 打赏
  • 举报
回复
谢谢大神们的帮助。 classpath* 可以直接扫描jar包中的spring配置文件。 但是我的资源输出地址不正确。 按照13楼大神的方式解决了问题。 再次感谢各位帮忙
cgs1999 2017-03-09
  • 打赏
  • 举报
回复
调试了一下你提供的代码http://download.csdn.net/download/changerzhuo_319/9770197

结果是正常的呀,如下图所示


如果说你的代码有什么问题的话,就是需要调整一下默认的输出路径

cgs1999 2017-03-06
  • 打赏
  • 举报
回复
可以加载到 web.xml中classpath:和classpath*的区别 classpath:只会到你的class路径中查找文件; classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.
changerzhuo 2017-03-04
  • 打赏
  • 举报
回复
引用 7 楼 qq_35567079 的回复:
[quote=引用 6 楼 yuxiangaaaaa 的回复:] [quote=引用 4 楼 changerzhuo_319 的回复:] [quote=引用 2 楼 yuxiangaaaaa 的回复:] classpath*:**/spring-*.xml 试试这个,可能路径不全
试了下, 还是会报错! 兄弟, 跟你确认下, 在web.xml文件中使用classpath*:**/spring-*.xml 是不是一定会加载jar包中的spring-*.xml文件? 你测试过可以加载吗?[/quote] classpath* 就是可以加载jar包里面的xml文件的[/quote]按说上面的所有的jar包应该都加上了[/quote] 我把两个项目压缩到一个包中, 帮忙调试下! 谢谢。 springdemo用的是maven, 我已经将springdemo打包一份丢在了spring_mybatis的lib中, 如果你们使用maven找不到springdemo依赖的jar包, 可以解压spring_mybatis中的springdemo-0.0.1-SNAPSHOT.jar获取 资源地址: http://download.csdn.net/download/changerzhuo_319/9770085
changerzhuo 2017-03-04
  • 打赏
  • 举报
回复
引用 2 楼 yuxiangaaaaa 的回复:
classpath*:**/spring-*.xml 试试这个,可能路径不全
我把两个项目压缩到一个包中, 帮忙调试下! 谢谢。 springdemo用的是maven, 我已经将springdemo打包一份丢在了spring_mybatis的lib中, 如果你们使用maven找不到springdemo依赖的jar包, 可以解压spring_mybatis中的springdemo-0.0.1-SNAPSHOT.jar获取 资源地址: http://download.csdn.net/download/changerzhuo_319/9770085
changerzhuo 2017-03-04
  • 打赏
  • 举报
回复
引用 7 楼 qq_35567079 的回复:
[quote=引用 6 楼 yuxiangaaaaa 的回复:] [quote=引用 4 楼 changerzhuo_319 的回复:] [quote=引用 2 楼 yuxiangaaaaa 的回复:] classpath*:**/spring-*.xml 试试这个,可能路径不全
试了下, 还是会报错! 兄弟, 跟你确认下, 在web.xml文件中使用classpath*:**/spring-*.xml 是不是一定会加载jar包中的spring-*.xml文件? 你测试过可以加载吗?[/quote] classpath* 就是可以加载jar包里面的xml文件的[/quote]按说上面的所有的jar包应该都加上了[/quote] 刚才的那个资源没有审核通过, 这是新的地址:http://download.csdn.net/download/changerzhuo_319/9770197     方便的话加个qq, 我的qq:1014043781
  • 打赏
  • 举报
回复
可以加载到的
changerzhuo 2017-03-01
  • 打赏
  • 举报
回复
引用 2 楼 yuxiangaaaaa 的回复:
classpath*:**/spring-*.xml 试试这个,可能路径不全
试了下, 还是会报错! 兄弟, 跟你确认下, 在web.xml文件中使用classpath*:**/spring-*.xml 是不是一定会加载jar包中的spring-*.xml文件? 你测试过可以加载吗?
changerzhuo 2017-03-01
  • 打赏
  • 举报
回复
昨晚太慌张了, 忘贴报错信息了。 下面错误信息是在spring_mybatis中的TestController中调用springdemo中的UserController。 主要就是说找不到springdemo中的userController org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cez.spring.controller.UserController com.changez.sm.controller.TestController.userController; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.cez.spring.controller.UserController] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cez.spring.controller.UserController com.changez.sm.controller.TestController.userController; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.cez.spring.controller.UserController] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.cez.spring.controller.UserController] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
自由自在_Yu 2017-03-01
  • 打赏
  • 举报
回复
classpath*:**/spring-*.xml 试试这个,可能路径不全
燕-十三 2017-03-01
  • 打赏
  • 举报
回复
引用 6 楼 yuxiangaaaaa 的回复:
[quote=引用 4 楼 changerzhuo_319 的回复:] [quote=引用 2 楼 yuxiangaaaaa 的回复:] classpath*:**/spring-*.xml 试试这个,可能路径不全
试了下, 还是会报错! 兄弟, 跟你确认下, 在web.xml文件中使用classpath*:**/spring-*.xml 是不是一定会加载jar包中的spring-*.xml文件? 你测试过可以加载吗?[/quote] classpath* 就是可以加载jar包里面的xml文件的[/quote]按说上面的所有的jar包应该都加上了
自由自在_Yu 2017-03-01
  • 打赏
  • 举报
回复
引用 4 楼 changerzhuo_319 的回复:
[quote=引用 2 楼 yuxiangaaaaa 的回复:] classpath*:**/spring-*.xml 试试这个,可能路径不全
试了下, 还是会报错! 兄弟, 跟你确认下, 在web.xml文件中使用classpath*:**/spring-*.xml 是不是一定会加载jar包中的spring-*.xml文件? 你测试过可以加载吗?[/quote] classpath* 就是可以加载jar包里面的xml文件的
lzstc 2017-03-01
  • 打赏
  • 举报
回复
导入的jar文件也可以通过XML来配置其中文档的路径?是classpath还是import?来学习了!!
李德胜1995 2017-02-28
  • 打赏
  • 举报
回复
试试import标签

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧