[color=darkblue][size=large][b]1.servlet中[/b][/size][/color]
[color=darkblue][size=medium][b]2.普通java类中[/b][/color][/size]
[color=darkblue][size=medium][b]3.用命令行运行java程序[/b][/size][/color]
在main方法里2行代码就可以拿到service,然后直接调用其方法即可
Test是main方法所在的类
ServletContext servletContext = this.getServletConfig().getServletContext();
ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
DownloadService downloadService = (DownloadService) applicationContext.getBean("downloadService");
[color=darkblue][size=medium][b]2.普通java类中[/b][/color][/size]
ApplicationContext context = SpringContext.getContext();
SymUserService ss = (SymUserService) context.getBean("symUserService");
import org.springframework.context.ApplicationContext;
public class SpringContext {
public static ApplicationContext context ;
public static ApplicationContext getContext() {
return context;
}
public static void setContext(ApplicationContext context) {
SpringContext.context = context;
}
}
[color=darkblue][size=medium][b]3.用命令行运行java程序[/b][/size][/color]
在main方法里2行代码就可以拿到service,然后直接调用其方法即可
BeanFactory bf = new ClassPathXmlApplicationContext("spring-*.xml");
GameServiceImpl gameService = (GameServiceImpl) bf.getBean("gameService");
F:\gamespider\web\WEB-INF\classes>java -classpath F:\gamespider\web\WEB-INF\classes -Djava.ext.dirs=F:\gamespider\web\WEB-INF\lib Test
Test是main方法所在的类