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

先说一下错误出现的场景: 前几天在写一个项目的时候,需要使用到第三方的底层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)注解.

 

 

 

编译通过 .问题解决.

 

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

原链接

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值