• 泛型依赖注入


    Spring 4.x的新特性

    1.结构

    BaseRepository.java:

    1 package com.hk.beans.generic.di;
    2 
    3 public class BaseRepository<T> {
    4 
    5 }

    BaseService.java:

     1 package com.hk.beans.generic.di;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 
     5 public class BaseService<T> {
     6     
     7     @Autowired
     8     protected BaseRepository<T> repository;
     9     
    10     public void add(){
    11         System.out.println("add...");
    12         System.out.println(repository);
    13     }
    14 }

    User.java:

    1 package com.hk.beans.generic.di;
    2 
    3 public class User {
    4 
    5 }

    UserRepository.java:

    1 package com.hk.beans.generic.di;
    2 
    3 import org.springframework.stereotype.Repository;
    4 
    5 @Repository
    6 public class UserRepository extends BaseRepository<User>{
    7  
    8 }

    UserService.java:

    1 package com.hk.beans.generic.di;
    2 
    3 import org.springframework.stereotype.Service;
    4 
    5 @Service
    6 public class UserService extends BaseService<User>{
    7     
    8 }

    Main.java:

     1 package com.hk.beans.generic.di;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Main {
     7     public static void main(String[] args) {
     8         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-generic-di.xml");
     9         UserService userService = (UserService) ctx.getBean("userService");
    10         userService.add();
    11     }
    12 
    13 }

    运行结果:

    每接触一个新领域,我就像一块掉进水里的海绵,四面八方的养分都让我不断充实。O(∩_∩)O~
  • 相关阅读:
    WebStorm 9 配置 Live Edit 功能与浏览器实现同步
    开源JS图片裁剪插件
    cropper手机使用实例
    Laravel中的Storage::disk
    laravel删除文件
    cropper.js移动端使用
    资本的一些运作规律及启示
    laravel文件存储、删除、移动等操作
    解决div和父div不上对齐
    ubuntu14.04如何卸载qq
  • 原文地址:https://www.cnblogs.com/zhzcode/p/9651514.html
Copyright © 2020-2023  润新知