【问题标题】:Spring Boot Autowiring From Another ModuleSpring Boot 从另一个模块自动装配
【发布时间】:2020-12-01 10:40:26
【问题描述】:

我正在尝试在我的项目中的 3 个模块之间建立连接。当我尝试使用@Autowired 到达我的对象时出现错误。我会稍微解释一下我的场景。

模块

所有这些模块都已连接到 pom.xml 中。说说我的问题吧。

C -> ROUTE.JAVA

.
.
.
@Autowired
public CommuniticationRepository;

@Autowired
public Core core;
.
.
.

B -> 核心

public class Core {
    private int id;
    private String name;
    private Date date;

    public Core(int id, String name, Date date) {
        this.id = id;
        this.name = name;
        this.date = date;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

错误

com.demo.xyz.A.RestControllers.Route 中的字段 communicationRepository 'com.demo.xyz.A.CommunicationRepository' 类型的 bean,它不能 找到了。

行动:

考虑定义一个“com.demo.xyz.A.CommunicationRepository”类型的bean 你的配置。

A -> REPOSITORY.JAVA

@Component
@Repository
public interface CommunicationRepository extends CrudRepository<Communication, Date> {

    List<Communication> findByDate(Date date);

    void countByDate(Date date);
}

【问题讨论】:

  • 确保您将所需的包裹提供给@ComponentScan,请参阅相关问答here
  • 我已经试过了。当我使用@ComponentScan 时,它不会在运行语句之前标记下划线。当我运行项目时,它返回一个错误,我写在黄色框中
  • 你的意思是“它在运行语句之前没有标记下划线”?它是否显示编译错误?
  • 请向我们展示您的“核心”课程
  • @Damith,Intellij Idea 说好的,但是当我运行它时,Spring 返回一个错误

标签: java spring spring-boot


【解决方案1】:

如果 CommunicationRepository 是 Spring Data JPA 存储库,则应从 CommunicationRepository 中删除 @Component@Repository

您应该在模块 AB 中定义配置。

@Configuration
@EnableJpaRepositories(basePackages ={"com.demo.xyz.A"})
@EntityScan(basePackages = {"com.demo.xyz.A"})
@ComponentScan(basePackages = {"com.demo.xyz.A"})
public class ConfigA {

}

// If you have no spring managed beans in B this is not needed
// If Core should be a spring managed bean, add @Component on top of it
@Configuration
@ComponentScan(basePackages = {"com.demo.xyz.B"})
public class ConfigB {

}

然后,在引导应用程序的 C 中,您应该导入模块 A 和模块 B 的配置。此时,来自 A 和 B 的任何 bean 都可用于在 C 中自动装配.

@Configuration
@Import(value = {ConfigA.class, ConfigB.class})
public class ConfigC {

}

【讨论】:

  • 如果另一个模块A和B中的bean是通过beans.xml文件初始化的,那么我们如何将它们放在@Import注解下。
  • 这就是我的答案。
【解决方案2】:

基本上如果你想在任何属性之上使用@Autowired注解并使用它,显然在spring上下文中应该有一个初始化的bean来自动装配它到你的使用中。所以这里你的问题是在你的spring上下文中,没有这样的bean来自动装配。 所以解决方案是你需要将这些 bean 放在你的 spring 上下文中,有多种方法可以完成这项工作,

你需要 bean 的类在 spring 上下文中自动初始化为@Component

例如:

@Component
public class Car{

或者您可以手动拥有一个返回此类 bean 的配置文件

例如:

@Bean
public Car setCarBean(){
    return new Car();
}

并且这个返回的 bean 应该在 @Configuration 类中。

please refer

如果你真的确定你已经完成了这个,那么正确的 @ComponentScan 应该可以工作

编辑

@SpringBootApplication 
@ComponentScan(basePackages = { "com.demo.xyz.A", "com.demo.xyz.B"}) 
public class Application {

【讨论】:

  • 感谢您的回复,但@Component 注释并没有解决我的问题。主要问题在存储库中
【解决方案3】:

尝试在 Application 类中添加 scanBasePackages。 默认扫描的是​​Application类所在的包。

@SpringBootApplication(scanBasePackages = "com.demo.xyz")
public class Application {...}

【讨论】:

    猜你喜欢
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2019-05-03
    • 2014-06-12
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多