image.png

    1. // src/beans.xml
    2. <beans>
    3. <context:component-scan base-package="com.lijunyang" />
    4. </beans>
    1. // src/com/lijunyang/model/BaseService.java
    2. public class BaseService<T> {
    3. @Autowired
    4. protected BaseRepository<T> repository;
    5. public void add() {
    6. System.out.println("add...");
    7. System.out.println("repository");
    8. }
    9. }
    10. // src/com/lijunyang/model/BaseRepository.java
    11. public class BaseRepository {
    12. }
    13. // src/com/lijunyang/model/User.java
    14. public class User {
    15. }
    16. // src/com/lijunyang/model/UserService.java
    17. @Service
    18. public class UserService extends BaseService<User> {
    19. }
    20. // src/com/lijunyang/model/UserRepository.java
    21. @Repository
    22. public class UserRepository extends BaseRepository<User> {
    23. }
    24. // src/com/lijunyang/test/Main.java
    25. public class Main {
    26. public static main(String args[]) {
    27. ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    28. UserService userService = (UserService) ctx.getBean("userService");
    29. userService.add();
    30. }
    31. }