SpringBoot 多模块 自动装载(@Autowired)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

之前花了一个周写了一个springboot的demo,主要整合了swagger,spring data jpa,还有一些切面日志的应用。

后来直接用来做项目,在写设计文档的时候绞尽脑汁分了好几个模块,为了更好的解耦和扩展。于是就把demo拆成了多个maven模块,但随即就出现了问题,花了一天时间解决。好伤。

各个模块:

SpringBoot 多模块 自动装载(@Autowired)_第1张图片

解决方案:

@SpringBootApplication
@ComponentScan(basePackages = {"cn.kl.eds.web", "cn.kl.eds.service"})
@EnableJpaRepositories(basePackages = "cn.kl.eds.dao")
@EntityScan(basePackages = "cn.kl.eds.entity")
public class App{

	//发射App
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}

 

问题一:无法装载service中的@Service

        App启动类使用 @ComponentScan

主要原理是,@SpringBootApplication这个注解包括了@ComponentScan,但它默认是扫描App.class所在的包,所以,需要显示扫描指定的包。

 

问题二:无法装载 spring data jpa 的接口,如UserRepo

        App启动类使用 @EnableJpaRepositories

@Repository
public interface UserRepo extends JpaRepository {
    @Override
    @Query("select u from #{#entityName} u where u.id = ?1")
    User findOne(Integer id);
}

    

问题三:可以装载jpa接口了,但是实体类 "Not a managed type"

        App启动类使用 @EntityScan

我是在下面的评论中看到的,整个过程差点疯掉 -_-

SpringBoot 多模块 自动装载(@Autowired)_第2张图片

转载于:https://my.oschina.net/u/3035165/blog/903790

你可能感兴趣的:(java,python)