Spring实战 - 使用getBean方式装配Bean

  • 环境: MacOS + IntelliJ IDEA 2019.3.1 (Ultimate Edition)

  • 基于手动创建Spring项目结构,整合Spring Web。

1、在pom.xml中增加spring-web依赖


      org.springframework
      spring-web
      5.2.2.RELEASE

2、在web.xml中引入spring-context

  
    contextConfigLocation
    classpath:spring-context
  
  
    org.springframework.web.context.ContextLoaderListener
  

3、实现ApplicationContextAware和DisposableBean接口,获取ApplicationContext中所有的bean。

package com.codeonline.cats.commons.context;

import com.sun.media.jfxmediaimpl.MediaDisposer.Disposable;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * @author 码出高薪
 * @Desc. 获取bean 实例
 * @date 2020/1/13 08:52
 */

public class SpringContext implements ApplicationContextAware, Disposable {

  private static final Logger logger = LoggerFactory.getLogger(SpringContext.class);

  private static ApplicationContext applicationContext;

  /**
   * 根据beanId 获取实例
   * @param beanId
   * @param 
   * @return 
   */
  public static  T getBean(String beanId){
    assertContextInjected();
    return (T) applicationContext.getBean(beanId);
  }

  /**
   * 根据clazz 获取实例
   * @param clazz
   * @param 
   * @return
   */
  public static  T getBean(Class clazz){
    assertContextInjected();
    return applicationContext.getBean(clazz);
  }
  /**
   * 清空ApplicationContext.
   */
  public void dispose() {
    logger.debug("清空ApplicationContext");
    applicationContext = null;
  }
  /**
   * 实现ApplicationContextAware接口, 注入Context到静态变量中.
   */
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SpringContext.applicationContext = applicationContext;
  }
  /**
   * 检查ApplicationContext不为空.
   */
  private static void assertContextInjected(){
    Validate.validState(applicationContext != null, "在spring-context中没有定义SpringContext对象.");
  }
}

4、在service 目录下创建MyUserService接口

package com.codeonline.cats.service;

/**
 * @author 码出高薪
 * @Desc. 创建MyUserService接口
 * @date 2020/1/13 11:04
 */
public interface MyUserService {
  String sayHello();
}

5、在service 目录中创建impl
选择service->右键->New->Package->impl
6、在impl 目录中创建MyUserService接口实现类MyUserServiceImpl

package com.codeonline.cats.service.impl;

import com.codeonline.cats.service.MyUserService;

/**
 * @author 码出高薪
 * @Desc. 创建MyUserService接口实现类
 * @date 2020/1/13 11:10
 */

public class MyUserServiceImpl implements MyUserService {

  public String sayHello() {
    return "Hello, Spring Web ";
  }
}

7、在spring-context 增加bean,myUserService 和springContext




  
  
  
  

8、在Controller 创建MyUserServiceController

package com.codeonline.cats.web.controller;

import com.codeonline.cats.commons.context.SpringContext;
import com.codeonline.cats.service.MyUserService;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 码出高薪
 * @Desc. 调用MyUserService中的sayHello()
 * @date 2020/1/13 11:18
 */

public class MyUserServiceController extends HttpServlet {

/**
 * 获取Spring容器初始化bean, MyUserService
  */
private MyUserService myUserService = SpringContext.getBean("myUserService");
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String hello = myUserService.sayHello();
    req.setAttribute("hello",hello);
    req.getRequestDispatcher("index.jsp").forward(req,resp);
  }
}

10、 在webapp目录下创建index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    整合Spring web


${hello}


11、在web.xml中增加servlet

  
    MyUserServiceController
    com.codeonline.cats.web.controller.MyUserServiceController
  
  
    MyUserServiceController
    /hello
  

12、运行项目,在浏览器中输入http://localhost:8080/hello

整合Spring web.png

至此,完成Spring web 整合

你可能感兴趣的:(Spring实战 - 使用getBean方式装配Bean)