直接加载spring配置文件方式,可用于单元测试

package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.etone.service.ICacheService;

 

public class junitTest {

 

 @Test
 public void test() throws Exception {


  // String[] clapath = { "classpath:applicationContext-hibernate.xml" };


  // ApplicationContext context= new
    ClassPathXmlApplicationContext(clapath

);//加载classpath下文件也就是Web-INF/classes下的文件
  
  String local = "WebRoot/WEB-INF/applicationContext-hibernate.xml";


  ApplicationContext context = new FileSystemXmlApplicationContext(local);
  //cacheService为applicationContext-hibernate.xml中的bean的id 可以对应Dao也可以对应service层bean的id
  ICacheService cs = (ICacheService) context.getBean("cacheService");
  cs.createBussinessCacheData();
 }
}

你可能感兴趣的:(spring配置)