从外部JAR初始化类bean – 构造函数问题

huangapple go评论101阅读模式
英文:

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&lt;Permission&gt; findAll() {
		System.out.println(&quot;inside get all permissions&quot;);
		return permissionsRepository.findAll();
}

while the repository is a regular JPA interface

public interface PermissionsRepository extends JpaRepository&lt;Permission, PermissionTable&gt; {}

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 = {&quot;com.demo.internal.myapp&quot;})
@EnableJpaRepositories(&quot;com.demo.internal.myapp.permissions&quot;)
@ComponentScan(basePackages = { &quot;com.demo.internal.myapp&quot; })
public class PermissionsServiceImpl implements PermissionsService {

    private PermissionsRepository permissionsRepository;

	@Autowired
	public PermissionsServiceImpl() {

	}

	@Autowired
	public void setPermissionsService(PermissionsRepository permissionsRepository) {
		this.permissionsRepository = permissionsRepository;
	}

	public Collection&lt;Permission&gt; findAll() {
		System.out.println(&quot;inside get all permissions&quot;);
		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.

huangapple
  • 本文由 发表于 2020年7月22日 23:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63037683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定