Java 如何注入具有其他自动关联依赖项的依赖项?
我在这方面做了我的家庭工作,但是我总是以单层依赖解决方案结束, 我试图做的是,使用基于注释的SpringDI注入依赖项Java 如何注入具有其他自动关联依赖项的依赖项?,java,spring,dependency-injection,Java,Spring,Dependency Injection,我在这方面做了我的家庭工作,但是我总是以单层依赖解决方案结束, 我试图做的是,使用基于注释的SpringDI注入依赖项 public interface SpringJpaStudentRepository extends CrudRepository<StudentEntity, UUID> { } class DataSourceImpl extends StudentDataSource { final SpringJpaStudentRepository repo
public interface SpringJpaStudentRepository extends CrudRepository<StudentEntity, UUID> {
}
class DataSourceImpl extends StudentDataSource {
final SpringJpaStudentRepository repository;
@Autowired
public DataSourceImpl(SpringJpaStudentRepository repository) {
...
}
}
class RepositoryImpl implements StudentRepository {
final DataSourceImpl dataSource;
@Autowired
RepositoryImpl(DataSourceImpl dataSource) {
...
}
}
公共接口SpringJpaStudentRepository扩展了crudepository{
}
类DataSourceImpl扩展StudentDataSource{
最终SpringJPastudeEntrepository存储库;
@自动连线
公共数据源MPL(SpringJPastudeEntrepository存储库){
...
}
}
类RepositoryImpl实现StudentRepository{
最终数据源MPL数据源;
@自动连线
RepositoryImpl(数据源Impl数据源){
...
}
}
据我所知,我必须创建一个bean配置类来创建DatasourceImpl以注入到RepositoryImpl,但是我不能用一个新的类来完成它,因为DatasourceImpl还需要SpringJPastudeEntrepository作为注入依赖项,我如何处理这个场景?是否有1,2,3层或更多层不会改变注入依赖项的方式。只要这些依赖项是Springbean,就可以自动关联它们。您面临的具体问题是什么?当我为DataSourceImpl创建bean提供程序时,我应该使用新的DataSourceImpl(SpringJPastudeEntrepository),但我无法访问SpringJPastudeEntrepository实例。问题是,如何实例化具有另一个依赖项的依赖项对象,该依赖项对象可能具有访问权限,也可能不具有访问权限。假设“bean provider”指的是用
@bean
注释的方法,在用@configuration
注释的类中,您将SpringJpaStudentRepository作为参数添加到该方法中。如果你的意思是其他的,那么把代码贴出来。但是为什么要使用这样的方法而不是用@Component
或@Service
注释DataSourceImpl呢?