zoukankan      html  css  js  c++  java
  • Spring的Annotation使用注意

    使用了annotation的工程导出jar在运行时,报找不到@Service修饰的类。但是在工程里执行没有问题。报错如下:
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'wikiCategoryMigrateService' is defined

    解决:
    1将bean定义定义到xml中。这样即使不存在directory entries(DE),Spring也能找到bean。
    2使用export jar+Add directory entries把directory entries导出到classpath。但是由于export jar没办法将依赖的jar导出。所以需要使用外部cp指定。
    3export runnable jar同时save as ant script, 然后修改ant script,将<jar destfile="/Users/zhonghua/Desktop/transfer.jar">里面添加属性 filesonly="false"。目前测试好象不行。
    4利用maven的assemble打jar.


    二种导出DE的办法:
    DE一种是存在于jar被外界依赖,比如某个war依赖于某个jar,而该jar导出时没有包含DE应该也没关系。只要该jar位于war文件的/WEB-INFO/lib目录下,将来被同一个classloader加载就没有问题。个人理解war在调用jar的时候,会扫描jar形成DE并放到classpath下。
    另一种情况是没有war调用jar,jar需要单独执行,此时生成DE的办法是导出jar的时候导出,ant是在jar的任务中添加filesonly="false", eclipse需要通过选中Add directory entries实现。



    原理:annotation不会扫描jar,而是会扫描directory entries,因此Spring的文档中强调如果ant导出jar时,需要将files-only="false"
    "The scanning of classpath packages requires the presence of corresponding directory entries in the classpath. When you build JARs with Ant, make sure that you do not activate the files-only switch of the JAR task".

     
    参:http://stackoverflow.com/questions/8671061/directory-entries-in-generated-jar-archive-with-gradle
      http://stackoverflow.com/questions/6857130/spring-component-scan-not-scanning-jboss-server-lib-directory
     
     
  • 相关阅读:
    013.ES6 -对象字面量增强型写法
    012. ES6
    011. ES6 语法
    10. 9. Vue 计算属性的setter和getter 以及 计算属性的缓存讲解
    4. Spring MVC 数据响应方式
    3. SpringMVC 组件解析
    9. Vue 计算属性
    【洛谷 2984】给巧克力
    【洛谷 1821】捉迷藏 Hide and Seek
    【洛谷 1821】银牛派对Silver Cow Party
  • 原文地址:https://www.cnblogs.com/highriver/p/2423860.html
Copyright © 2011-2022 走看看