脱离tomcat容器在单独的java application的main函数中初始化spring
package com.zuidaima.test;import org.springframework.context.support.GenericXmlApplicationContext;import com.service.UserService;public class Main {public static void main(String[] args) throws InterruptedException {GenericXmlApplicationContext context = new GenericXmlApplicationContext();context.setValidating(false);context.load("classpath*:applicationContext*.xml");context.refresh();UserService userService = context.getBean(UserService.class);while (true) {System.out.println(userService.findUser());Thread.sleep(10000);}}}
package com.zuidaima.service;import org.springframework.stereotype.Service;@Servicepublic class UserService {public String findUser() {return "javaniu";}}
本文转载自:CSDN博客
欢迎加入我爱机器学习QQ14群:336582044
微信扫一扫,关注我爱机器学习公众号