启动spring容器几种方法

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>

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值