BaseService<T>: There are two subclasses of RoleService and UserService
BaseRepepositry<T>: There are two subclasses of UserRepository and RoleRepositry
because BaseService<T> inherits BaseRepepositry<T> , it is concluded that the following subclasses also have such a relationship
Code :
User.java
public class User { }
BaseService.java
import org.springframework.beans.factory.annotation.Autowired; public class BaseService<T> { @Autowired protected BaseRepository<T> repository; public void add(){ System.out.println("adding...."); System.out.println(repository); } }
UserService.java
import org.springframework.stereotype.Repository; @Repository public class UserService extends BaseService<User>{ }
BaseRepository.java
public class BaseRepository<T> {}
UserRepository.java
import org.springframework.stereotype.Repository; @Repository //Hand over to the IOC container to manage public class UserRepository extends BaseRepository<User>{}
Main.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); UserService userService = (UserService) applicationContext.getBean("userService"); userService.add(); } }
Output result:
adding.... com.spring.txing.UserRepository@7174d93a