日期:2014-05-17 浏览次数:20746 次
@Service
public class UserServiceImpl implements UserService {
//...
}
public class UserServiceTest extends BasicTestCase {
@Autowired
private UserService userService;
@Test
public void test() {
//...
}
}
videoInfoService = SpringContextHolder.getBean("videoInfoService",
IVideoInfoService.class);
package com.ustv.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextHolder.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
public static <T>T getBean(String beanName , Class<T>clazz) {
return applicationContext.getBean(beanName , clazz);
}
}