;在Springboot中创建bean时出错(新手问题)

问题描述 投票:0回答:1

我目前正在学习springboot,不懂一个关于bean的问题。

我创建了以下界面:

@Component
interface Employee {
    public void setName(String name); 
    public String getName();
}

然后,我创建了一个类:

@Component
class EmployeeImpl implements Employee {
    private String name;

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

最后我创建了以下配置类:

@Configuration
public class EmployeeConfig {
    @Bean
    public Employee employeeBean() { return new EmployeeImpl(); }
}

完成此操作后,我希望我的控制器能够访问员工:

  • 我添加了
    import tsn.iam.roles.Employee;
  • 然后我添加了
     @Autowired  private final Employee emp;

不幸的是,Eclipse 会引发错误,指出 Employee 类无法解析。 这一定是一个众所周知的错误。有人可以帮忙吗? 谢谢!

java spring-boot javabeans
1个回答
0
投票

您应该将 Employee 接口声明为公共。另外,请确保接口文件开头的包声明与引用它的包结构相匹配。而且我不明白为什么你需要用@Component来注释你的界面?

© www.soinside.com 2019 - 2024. All rights reserved.