英文:
Initialize class bean from external JAR - constructor issue
问题
以下是您要翻译的内容:
"我正在从外部服务导入一个JAR。这是服务类的代码:
private final PermissionsRepository permissionsRepository;
@Autowired
public PermissionsServiceImpl(PermissionsRepository permissionsRepository) {
this.permissionsRepository = permissionsRepository;
}
public Collection<Permission> findAll() {
System.out.println("inside get all permissions");
return permissionsRepository.findAll();
}
而存储库是一个常规的JPA接口:
public interface PermissionsRepository extends JpaRepository<Permission, PermissionTable> {}
我正在尝试在我另一个程序中使用“findAll”方法,所以我已经从上述项目创建了一个JAR,导入了它,但我很难初始化服务bean,因为我无法将存储库作为参数传递。有什么最好的方法来做到这一点?
这是我当前在BeanConfig类中拥有的内容(在导入了JAR的另一个项目中):
@Configuration
@EntityScan(basePackageClasses = {PermissionsRepository.class})
public class BeansConfig {
// 需要创建permissionRepository
@Bean
public PermissionsService permissionsService() {
return new PermissionsServiceImpl(???);
}
}
谢谢。"
英文:
I'm importing a JAR from an external service. this is the service class code:
private final PermissionsRepository permissionsRepository;
@Autowired
public PermissionsServiceImpl(PermissionsRepository permissionsRepository) {
this.permissionsRepository = permissionsRepository;
}
public Collection<Permission> findAll() {
System.out.println("inside get all permissions");
return permissionsRepository.findAll();
}
while the repository is a regular JPA interface
public interface PermissionsRepository extends JpaRepository<Permission, PermissionTable> {}
I'm trying to use the "findAll" method in other program I have so I've created a JAR from the above project, imported it and I'm having difficulty to initialize the service bean because I cannot pass the repository as an argument. what's the best way to do so?
This is what I currently have in my BeanConfig class (inside the other project which imported the JAR):
@Configuration
@EntityScan(basePackageClasses = {PermissionsRepository.class})
public class BeansConfig {
// need to create permissionRepository
@Bean
public PermissionsService permissionsService() {
return new PermissionsServiceImpl(???);
}
}
Thanks
答案1
得分: 0
已解决。
所需更改位于导入项目中的service类内部。
@Service
@EntityScan(basePackages = {"com.demo.internal.myapp"})
@EnableJpaRepositories("com.demo.internal.myapp.permissions")
@ComponentScan(basePackages = { "com.demo.internal.myapp" })
public class PermissionsServiceImpl implements PermissionsService {
private PermissionsRepository permissionsRepository;
@Autowired
public PermissionsServiceImpl() {
}
@Autowired
public void setPermissionsService(PermissionsRepository permissionsRepository) {
this.permissionsRepository = permissionsRepository;
}
public Collection<Permission> findAll() {
System.out.println("inside get all permissions");
return permissionsRepository.findAll();
}
}
基本上,您需要告诉Spring在哪里查找实体模型对象、存储库等,因此我添加了3个注释:EntityScan、EnableJpaRepositories和ComponentScan,以实现这一目标。
我还为存储库变量进行了构造函数依赖注入。
英文:
Solved.
The needed changes are inside the service class inside the imported project.
@Service
@EntityScan(basePackages = {"com.demo.internal.myapp"})
@EnableJpaRepositories("com.demo.internal.myapp.permissions")
@ComponentScan(basePackages = { "com.demo.internal.myapp" })
public class PermissionsServiceImpl implements PermissionsService {
private PermissionsRepository permissionsRepository;
@Autowired
public PermissionsServiceImpl() {
}
@Autowired
public void setPermissionsService(PermissionsRepository permissionsRepository) {
this.permissionsRepository = permissionsRepository;
}
public Collection<Permission> findAll() {
System.out.println("inside get all permissions");
return permissionsRepository.findAll();
}
}
Basically you need to tell spring where to look for entities- model objects, repositories and more. so i've added 3 annotations:
EntityScan, EnableJpaRepositories and ComponentScan to do just that.
Also did a constructor dependency injection for the repository variable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论