Java 有没有办法扩展Spring服务,以便在不复制代码的情况下管理继承的实体?
我正在使用一个Spring应用程序,它有两个控制器和两个管理相关实体的服务,我希望避免重复代码。例如,我有一个Java 有没有办法扩展Spring服务,以便在不复制代码的情况下管理继承的实体?,java,spring,Java,Spring,我正在使用一个Spring应用程序,它有两个控制器和两个管理相关实体的服务,我希望避免重复代码。例如,我有一个Person类和他的PersonRepository @Entity @Inheritance(strategy = InheritanceType.JOINED) public class Person { @Id Long id; String name; String surname; } @Entity @Inheritance(strategy
Person
类和他的PersonRepository
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person {
@Id
Long id;
String name;
String surname;
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class User extends Person {
String login;
String password;
}
他的孩子User
和他的UserRepository
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person {
@Id
Long id;
String name;
String surname;
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class User extends Person {
String login;
String password;
}
我有一个具有业务逻辑的简单Person服务:
@Service
public class PersonService {
@Autowired
PersonRepository repo;
public Iterable<Person> getAll() {
// Busines Logic
return repo.findAll();
}
}
这显然会产生这样的错误:
类型不匹配:无法使用这样的控制器从Iterable转换为Iterable
@RestController
@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class DemoController {
@Autowired
UserService service;
@GetMapping
public void items() {
Iterable<User> persons = service.getAll();
return;
}
}
@RestController
@RequestMapping(value=“/test”,products=MediaType.APPLICATION\u JSON\u value)
公共类DemoController{
@自动连线
用户服务;
@GetMapping
公共物品(){
Iterable persons=service.getAll();
返回;
}
}
尝试以下结构:
公共服务和存储库:
@NoRepositoryBean
public interface PersonRepository<T> extends JpaRepository<T, Long> {
}
@NoRepositoryBean
公共接口PersonRepository扩展了JpaRepository{
}
公共类PersonService{
受保护的个人知识库;
公共人员服务(R存储库){
this.repository=存储库;
}
公共Iterable getAll(){
返回repository.findAll();
}
}
对于用户实体:
public interface UserRepository extends PersonRepository<User> {
}
公共接口UserRepository扩展了PersonRepository{
}
@服务
公共类UserService扩展PersonService{
公共用户服务(用户存储库){
超级(储存库);
}
public void additionalMethod(){
User=repository.getOne(1L);
}
}
我按用户实体添加了公司实体示例:
public interface CompanyRepository extends PersonRepository<Company> {
}
public interface CompanyRepository扩展PersonRepository{
}
@服务
公共类公司服务扩展PersonService{
公共公司服务(公司存储库){
超级(储存库);
}
public void additionalMethod(){
可选公司=repository.findById(1L);
}
}
尝试以下结构:
公共服务和存储库:
@NoRepositoryBean
public interface PersonRepository<T> extends JpaRepository<T, Long> {
}
@NoRepositoryBean
公共接口PersonRepository扩展了JpaRepository{
}
公共类PersonService{
受保护的个人知识库;
公共人员服务(R存储库){
this.repository=存储库;
}
公共Iterable getAll(){
返回repository.findAll();
}
}
对于用户实体:
public interface UserRepository extends PersonRepository<User> {
}
公共接口UserRepository扩展了PersonRepository{
}
@服务
公共类UserService扩展PersonService{
公共用户服务(用户存储库){
超级(储存库);
}
public void additionalMethod(){
User=repository.getOne(1L);
}
}
我按用户实体添加了公司实体示例:
public interface CompanyRepository extends PersonRepository<Company> {
}
public interface CompanyRepository扩展PersonRepository{
}
@服务
公共类公司服务扩展PersonService{
公共公司服务(公司存储库){
超级(储存库);
}
public void additionalMethod(){
可选公司=repository.findById(1L);
}
}