• 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
     
     
  • 相关阅读:
    体验用yarp连接websocket
    从 ASP.NET Core 5.0 迁移到.NET 6
    对接网易云信音视频2.0呼叫组件集成到vue中,实现web端呼叫app,视频语音通话。
    .NET6 WebAPI 自定义过滤器
    .NET6 WebApi 获取访问者IP地址
    .NET6 部署到 IIS
    .NET6 WebApi JSON传到前台默认变成小驼峰
    开发环境 测试环境 生产环境
    .NET6 WebApi 使用 log4net
    .NET6 WebApi 解决跨域问题
  • 原文地址:https://www.cnblogs.com/highriver/p/2423860.html
Copyright © 2020-2023  润新知