当前位置: 首页 > 知识库问答 >
问题:

spring-boot-starter-jdbc DAO存储库对象未注入到工作的遗留webservice中

姜奇
2023-03-14
    package X;
    ....
    @Component
    @WebService(name = "***",***)
    @BindingType("http://schemas.xmlsoap.org/wsdl/soap/http")
    @XmlSeeAlso({
        packagesxxx.class,
        ....
        })
    public class BServiceManager
        implements xxxxx
        {
         ....
         @Autowired    
         private ItemRepository irepo;
         
         ....
         @WebMethod(**)
         @WebResult(***)
         public ResponseDataInfo sendInfo( ){
         ....
         trepo.saveitem(item)
         ....         
         }
    }
    package Y.Z;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.dao.DataAccessException;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;

    @Component
    @Repository
    public class ItemRepository {
    
        @Autowired
        private JdbcTemplate jdbcTemplate ;
        
        ....
        public boolean saveitem(Item item) {
        
        ....
        }
    
    
     }
    package Y.Z;
    public class Item {
        ....
    }
    package Y;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    //@ComponentScan(basePackages={"Y","Y.Z","X"})
    @SpringBootApplication
    public class GetItemsApplication   {
        ....
        public static void main(String[] args) {
            SpringApplication.run(GetItemsApplication.class, args);
            log.info("--Spring Boot inits done--");
        }
        
    }

Application.Properties

    spring.data.jpa.repositories.enabled=false
    spring.data.jdbc.repositories.enabled=true

    # MySQL properties
    spring.datasource.url=****
    spring.datasource.username=****
    spring.datasource.password=****
    ....
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    logging.level.org.springframework.jdbc.core.JdbcTemplate=debug

注意:即使有数据源bean也没有帮助:

    File: DataSourceConfig.java

    package Y.Z;
    import javax.sql.DataSource;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.core.JdbcTemplate;
    //@EnableJdbcRepositories for Spring

    @Configuration
    public class MDataSourceConfig {
        
        @Bean
        public DataSource dataSource() {
            DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
            return dataSourceBuilder.build();      
        }

        @Bean
        public JdbcTemplate getJdbcTemplate() {
           return new JdbcTemplate(dataSource());
        }            
    }

共有1个答案

胡安怡
2023-03-14

您的Webservice似乎不是由Spring创建的。因此Spring无法控制其依赖项,因此必须通过编程方式获取依赖项。

有很多方法可以做到这一点。

  • 简单但不是很优雅,使用的全局变量可能会导致问题,尤其是在测试中:https://stackoverflow.com/a/18486178/66686
  • 更优雅,但需要使用@configurable
  • 编织Spring autowiring
 类似资料:
  • 这是我的Spring Boot应用程序相关代码: 另一方面,我有一个关于包的存储库: 因此,我有一个具有依赖关系的服务: 然而,我得到的信息是: NoSuchBeanDefinitionException:没有类型为“net”的合格bean。根卡特。横向的。埃斯帕伊多克。后台。刀。“文件”可用 我也尝试过添加,但它仍然不起作用。 有什么想法吗? 编辑 这是我的类: 编辑2: 我刚刚在spring日

  • 我很难将spring-boot应用程序作为另一个java(非Spring)应用程序的依赖项。 我被要求用一些方法和对Oracle数据库的访问来开发一个新的REST API。为此,我使用了Spring-boot,应用程序本身或部署在服务器上都运行良好。经过几次调整,我把它分成了几个模块,如下所示: models:包含业务代码的模型对象 API:包含业务代码和实际API 对模型有依赖关系 包含连接到数

  • 我有这样一个代码: 存储库 服务 我想知道为什么我可以调用“interface EquipmentRepository”方法。EquipmentRepository是一个接口,对吗?

  • 尽管spring-boot-依赖项-3.0.0-M1.pom将spring-boot-starter-actiemq-3.0.0-M1列为依赖项,但spring-boot-starter-actiemq-3.0.0-M1在https://repo.spring.io/milestone.中不可用

  • 我正试图通过遵循本教程,使用GitHub操作构建docker映像并将其推送到Amazon ECR 我正在尝试为此repo设置GitHub操作,因此我在<代码>中创建了一个新的工作流。github/workflows目录。 DockerFile已成功创建并生成。 现在,在创建

  • 若要禁用作用域存储并使用旧式存储模型,请取消设置这两个标志。 如何解开旗子?还有别的建议吗?