• Spring新特性_泛型依赖注入


    泛型依赖注入

    package com.tanlei.spring.generic;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class BaseService<T> {
        
          @Autowired
          protected   BaseRepository<T> baseRepository;
          
          public void add() {
              System.out.println("add...");
              System.out.println(baseRepository);
          }
    }
    package com.tanlei.spring.generic;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService extends BaseService<User>{
         
    }
    package com.tanlei.spring.generic;
    
    public class BaseRepository<T> {
    
    }
    package com.tanlei.spring.generic;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class UserReopsitory extends BaseRepository<User>{
        
    }
    package com.tanlei.spring.generic;
    
    public class User {
    
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
        <context:component-scan base-package="com.tanlei.spring.generic"></context:component-scan>
    </beans>
    package com.tanlei.spring.generic;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
       public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("bean.generic.xml");
        UserService userService=(UserService) context.getBean("userService");
        userService.add();
    }
    }

  • 相关阅读:
    php获取随机字符串
    php短网址生成算法
    tp5.1发送邮件
    PHP简单 对象(object) 与 数组(array) 的转换
    PHP获取接下来一周的日期
    swoole 连接池
    PHP静态文件缓存
    php微信分享demo
    生成二维码并指定地址跳转
    tp5依赖注入(自动实例化):解决了像类中的方法传对象的问题
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/10139194.html
Copyright © 2020-2023  润新知