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

【转】

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

@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(); } }

 

posted on 2016-12-28 11:19  clo33tt  阅读(323)  评论(0编辑  收藏  举报