zoukankan      html  css  js  c++  java
  • Spring入门第十五课

    泛型依赖注入

    看代码:

    package logan.spring.study.generic.di;
    
    public class BaseRepository<T> {
    
    }
    package logan.spring.study.generic.di;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class BaseService<T> {
        @Autowired
        protected BaseRepository<T> repository;
        
        public void add(){
            System.out.println("add...");
            System.out.println(repository);
        }
        
        
    }
    package logan.spring.study.generic.di;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService extends BaseService<User>{
        
    
    }
    package logan.spring.study.generic.di;
    
    public class User{
    
    }
     
    package logan.spring.study.generic.di;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class UserRepository extends BaseRepository<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.3.xsd">
        <context:component-scan base-package="logan.spring.study.generic.di"></context:component-scan>
    
    </beans>
    package logan.spring.study.generic.di;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-generic.xml");
            UserService userService = (UserService) ctx.getBean("userService");
            userService.add();
            
    
        }
    
    }

    输出结果:

    五月 27, 2017 2:04:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@42110406: startup date [Sat May 27 14:04:02 CST 2017]; root of context hierarchy
    五月 27, 2017 2:04:02 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [beans-generic.xml]
    add...
    logan.spring.study.generic.di.UserRepository@43a0cee9
  • 相关阅读:
    nodejs 提示‘xxx’ 不是内部或外部命令解决方法
    nodejs安装express不是内部或外部命令
    菜鸟供应链升级实践
    数据治理框架解读分析
    Canal+Kafka实现MySQL与Redis数据同步
    一条 SQL 引发的事故,同事直接被开除!!
    我用几行 Python 自动化脚本完美解决掉了小姐姐的微信焦虑感
    从 Storm 迁移到 Flink,美团外卖实时数仓建设实践
    供应链管理学习心得
    解密京东智慧供应链
  • 原文地址:https://www.cnblogs.com/LoganChen/p/6886294.html
Copyright © 2011-2022 走看看