码迷,mamicode.com
首页 > 其他好文 > 详细

Sprinig泛型依赖注入

时间:2017-08-14 10:11:15      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:ring   new   one   rgs   泛型   user   png   ide   dao   

在父类中建立关系 (spring4.x以上版本)

技术分享
package com.spring.annotation.generic;

import org.springframework.beans.factory.annotation.Autowired;

public class BaseService<T> {

    @Autowired
    private BaseRepository<T> dao;
    
    public void add(T entity){
        System.out.println("add by " + dao);
        dao.save(entity);
    }
    
}



package com.spring.annotation.generic;

public class BaseRepository<T> {

    public void save(T entity){
        System.out.println("save:" + entity);
    }
    
}
View Code

子类

技术分享
package com.spring.annotation.generic;

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService<User>{

}


package com.spring.annotation.generic;

import org.springframework.stereotype.Repository;

@Repository
public class UserDao extends BaseRepository<User>{

}
View Code

 

测试

技术分享
package com.spring.annotation.generic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    
    public static void main(String[] args) {
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
        
        UserService userService = (UserService) ctx.getBean("userService");
        userService.add(new User());
        
        
    }
    
}
View Code

结果

技术分享

 

Sprinig泛型依赖注入

标签:ring   new   one   rgs   泛型   user   png   ide   dao   

原文地址:http://www.cnblogs.com/lusufei/p/7355870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!