pring Boot  在注入第三方的Service或Repository时出现错误的解决办法

宗政博
2023-12-01

先说一下错误出现的场景: 前几天在写一个项目的时候,需要使用到第三方的底层RepositoryService.所以就将将要引入的代码使用idea打包成jar.通过pom依赖进来.代码编写时没有出现任何问题,jar正常引入.但是代码写完之后编译时,出现错误. 

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
[com.newtank.scorpio.jpa.MsgLogRepository] 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)}

问题是Spring 注入失败,所以就使用单纯的方法.在Application上扫描组件的注解上加入该jar的包,希望能够扫描到,然后能够依赖进来

使用@ComponentScan注解

@ComponentScan(basePackages = {"com.newtank.scorpio.web","com.newtank.scorpio.jpa"})

 

代码 

 

package com.newtank.scorpio.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@SpringBootApplication
@ComponentScan(basePackages = {"com.newtank.scorpio.web","com.newtank.scorpio.jpa"})
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableAsync
public class MainSiteApplication extends SpringBootServletInitializer implements SchedulingConfigurer {

  public static void main(String[] args) {
    SpringApplication.run(MainSiteApplication.class, args);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MainSiteApplication.class);
  }

  @Override
  public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    taskRegistrar.setScheduler(taskExecutor());
  }

  @Bean(destroyMethod = "shutdown")
  public Executor taskExecutor() {
    return Executors.newScheduledThreadPool(10);
  }

}


添加@ComponentScan 后面的特定扫描的包后,编译 还是相同的问题,问题没有解决,很不理解.在Google了一下,找到一个方法.问题解决如下

package com.newtank.scorpio.web;

import com.newtank.scorpio.web.conf.SharedConfigurationReference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@SpringBootApplication
@Import(SharedConfigurationReference.class)
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableAsync
public class MainSiteApplication extends SpringBootServletInitializer implements SchedulingConfigurer {

  public static void main(String[] args) {
    SpringApplication.run(MainSiteApplication.class, args);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MainSiteApplication.class);
  }

  @Override
  public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    taskRegistrar.setScheduler(taskExecutor());
  }

  @Bean(destroyMethod = "shutdown")
  public Executor taskExecutor() {
    return Executors.newScheduledThreadPool(10);
  }

}
package com.newtank.scorpio.web.conf;

import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 * Created by looper on 2017/8/30.
 */
@Configuration
@ComponentScan(basePackages = {"com.newtank.scorpio.jpa","com.newtank.scorpio.web"})
@EnableJpaRepositories(basePackages = {"com.newtank.scorpio.jpa.repository","com.newtank.scorpio.web.repository"})
@EntityScan(basePackages = {"com.newtank.scorpio.jpa.model","com.newtank.scorpio.web.entity"})
public class SharedConfigurationReference {
}

 

 

这个方法是新增了一个Configuration类,对使用到的每一个包 进行扫描 .  本例中新增的是SharedConfigurationReference类  然后在MainSiteApplication类(Application类)中添加@Import(SharedConfigurationReference.class)注解.

 

 

 

编译通过 .问题解决.

 

现在贴上原链接的解决方法.希望能给同样错误的人 节约一些时间 

原链接

 

 类似资料: