1 spring通过对Junit框架的整合功能来启动Spring非Web容器(推荐)
junit测试方法时使用:需要引入junit4、spring的jar包、spring-test.jar
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-config.xml"})
public class TestBeanImplTest {
@Resource TestBean testBean;
@Test
public void getBeanTest(){
system.out.print(testBean);
}
}
2.在main方法中初始化spring容器:
public class TestBeanImpl{
public static void main(String[] args){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
context.start();
TestBean test= (TestBean) context.getBean("testBean");
System.out.println(test);
}
}
3.调用静态方法时初始化
package com.data.test;
import com.data.test.monitor.ServiceFacade;
import org.slf4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @description: single
* @author: feng
* @create: 2018-12-21 17:49
**/
public class SingleTest {
public static Logger logger = org.slf4j.LoggerFactory.getLogger(SingleTest.class);
private static SingleTest sin = new SingleTest ();
public static ServiceFacade serviceFacade;
private SingleTest (){
logger.info("初始化Spring容器开始。");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-config.xml");
serviceFacade =(ServiceFacade) ac.getBean("ebMonitorInnerServiceFacade");
logger.info("获取bean-ServiceFacade:{}",serviceFacade );
}
public static SingleTest get(){
return sin;
}
}
4.web容器启动
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>hotel</display-name>
<!-- Spring Loader Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ContextConfigLocation 初始化spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-config.xml</param-value>
</context-param>
<!-- CharacterEncoding -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Welcome -->
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
<!-- Http请求 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>