Java-Web系列之Spring-Web

文章目录

    • Spring Web
      • 简介
      • 容器初始化
        • 在pom.xml中添加依赖
        • 配置 web.xml
        • 实现 `ApplicationContextAware`
        • Spring-context 配置
        • 创建接口和实现接口
          • 创建UserService接口
          • 创建UserServiceImpl 实现
        • 测试
          • 定义路由,实现doGet方式
      • 基于注解的注入方式
        • spring-context 配置文件
        • 注解方式注入Spring-Context

Spring Web

简介

这个jar 文件包含Web 应用开发时,用到Spring 框架时所需的核心类,包括自动载入Web Application Context 特性的类、Struts 与JSF 集成类、文件上传的支持类、Filter 类和大量工具辅助类。

容器初始化

启动容器时需要自动装载 ApplicationContext,而不是像之前用
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");

来初始化。Spring 提供的 ContextLoaderListener 就是为了自动装配 ApplicationContext 的配置信息

在pom.xml中添加依赖


    org.springframework
    spring-web
    4.3.17.RELEASE


配置 web.xml

web.xml 配置如下




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

  • 其中context-param指定了spring-context的配置文件,listener指定了项目启动时的监听

实现 ApplicationContextAware

当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得 ApplicationContext 中的所有 bean。换句话说,就是这个类可以直接获取 Spring 配置文件中,所有有引用到的 Bean 对象。

package com.domain.ssh.springWeb.commons;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContext implements ApplicationContextAware, DisposableBean {
    private static ApplicationContext applicationContext;

    //销毁时释放applicationContext对象
    public void destroy() throws Exception {
        applicationContext = null;
    }

    //销毁时释放applicationContext对象
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContext.applicationContext = applicationContext;
    }

    /**
     * 获取Spring容器中的对象
     * @param beanId
     * @param 
     * @return
     */
    public static  T getBean(String beanId) {
        return (T) applicationContext.getBean(beanId);
    }

}

Spring-context 配置

注入 实现 ApplicationContextAware的类到Spring




    
    
    
    


创建接口和实现接口

创建UserService接口
package com.domain.hello.spring.service;

public interface UserService {
    public void sayHi();
}
创建UserServiceImpl 实现
package com.domain.hello.spring.service.impl;

import com.domain.hello.spring.service.UserService;

public class UserServiceImpl implements UserService {
    public void sayHi() {
        System.out.println("Hello Spring");
    }
}

测试

自动装载ApplicationContext后来获取Spring容器中的对象

定义路由,实现doGet方式
package com.domain.ssh.springWeb.controller;

import com.domain.ssh.spring.service.UserService;
import com.domain.ssh.springWeb.commons.SpringContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/hello")
public class HelloController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       UserService userService = SpringContext.getBean("userService");
        userService.sayHi();
    }

}

基于注解的注入方式

之前在使用Spring的时候说过(见Spring章节),每一次注入一个对象,都需要在spring-context文件中配置。这种方式是通过xml的方式来注入对象,这里学习通过注解的方式来注入

spring-context 配置文件




    
    

使用注解方式后,配置文件中将不再需要定义一个一个的bean,只需要写上打开注解模式和指定扫描注解的包这两句话

注解方式注入Spring-Context

package com.domain.ssh.springWeb.commons;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContext implements ApplicationContextAware, DisposableBean {
    private static ApplicationContext applicationContext;

    //销毁时释放applicationContext对象
    public void destroy() throws Exception {
        applicationContext = null;
    }

    //销毁时释放applicationContext对象
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContext.applicationContext = applicationContext;
    }

    /**
     * 获取Spring容器中的对象
     * @param beanId
     * @param 
     * @return
     */
    public static  T getBean(String beanId) {
        return (T) applicationContext.getBean(beanId);
    }

}

  • 通过@Component来注入Spring-Context

Spring提供了以下常用的注入注解

  • @Component
    用于注入普通java类
  • @Repository
    用于注入Dao层的实现类
  • @Service
    用于注入Service层的实现类
  • @Controller
    用于注入Controller的实现类

你可能感兴趣的:(java-web,spring-web,注解注入)