码迷,mamicode.com
首页 > 编程语言 > 详细

Spring @Autowired注解在utils静态工具类

时间:2016-12-28 12:22:28      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:接口   ice   post   使用   log   如何   struct   ring   静态方法   

【转】

使用场景:在一个静态方法中,如何使用以下注入;

@Autowired 
private ItemMapper itemMapper;
@Component  
public class TestUtils {     
@Autowired    
private ItemService itemService;

@Autowired    
private ItemMapper itemMapper;
          
public static TestUtils testUtils; 
         
@PostConstruct     
public void init() {
testUtils = this;     }          
 //utils工具类中使用service和mapper接口的方法例子,用"testUtils.xxx.方法" 就可以了    
public static void test(Item record){         
testUtils.itemMapper.insert(record); testUtils.itemService.queryAll(); } }

 

Spring @Autowired注解在utils静态工具类

标签:接口   ice   post   使用   log   如何   struct   ring   静态方法   

原文地址:http://www.cnblogs.com/isisbenben/p/6228748.html

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