spring核心及常用技术

一、核心内容

1、依赖注入(控制反转)

  1)什么是依赖注入

  spring将实例的创建交给spring容器(BeanFactory或ApplicationContext)管理,当实例创建后通过设值或构造注入的方式将实例赋值给调用程序的成员变量。对程序而言由原来的主动创建实例到被动被赋值实现实例化,这一过程称为控制反转;而对于spring容器而言,将被依赖对象赋值给调用者的成员变量,给调用者注入了它依赖的实例,因此这种方式又被称作依赖注入。

  2)bean注入方式(设值注入、构造注入)

  区别注入时机不同:设值注入通过无参构造器创建bean实例,然后调用对应的setter方法注入依赖关系;构造注入则直接调用有参构造器,当bean实例创建完成后已完成依赖关系的注入。

  注意:对于依赖关系固定的注入,尽量采用构造注入,这样对于组件的调用者而言,组件内部依赖关系完全透明,更符合高内聚原则;其它依赖关系注入则采用设置注入,更直观。

  3)BeanFactory和ApplicationContext的区别

  实例化容器中bean的时机不同,BeanFactory等到程序需要bean实例时才创建;ApplicationContext会在容器创建时预初始化容器中所有的singleton bean,因此实例化过程时间和内存开销大,但可再容器时初始化阶段检验出配置错误。

  4)spring容器作用域

  a、ApplicationContext容器的作用域有五种,通过scope属性指定

  singleton:单例模式,在整个spring ioc容器中,bean将只生成一个实例

  prototype:每次通过容器的geBean()方法获取bean时,都将产生一个新的实例

  request:对于一次HTTP请求,bean将生成一个新的实例。web应用中才有效。

  session:对于一次HTTP会话,bean只生成一个实例。web应用中才有效。

  global session:每个全局的HTTP session对应一个bean实例。web应用中才有效。

  b、测试

  spring-mvc.xml文件

   <bean id="user1" class="com.example.entity.User"></bean>
   <bean id="user2" class="com.example.entity.User" scope="prototype"></bean>

  java代码

@Controller
@RequestMapping(value = "/user")
public class UserCotroller {
    @Autowired
    private ApplicationContext applicationContext;
    @RequestMapping(value = "/loginView")
    public String loginView() {
        System.out.println(applicationContext.getBean("user1") == applicationContext.getBean("user1"));//true
        System.out.println(applicationContext.getBean("user2") == applicationContext.getBean("user2"));//false
        return "login";
    }
}

2、spring aop

   AOP(Aspect Orient Programming)是对OOP(Object-Oriented Programing,面向对象编程)的一种补充,解决java语言中存在的交叉关注点的问题。所谓交叉关注点问题就是解决为分散的对象引入公共的行为,如日志输出、权限判断、事务管理、缓存等共性操作。这些操作如果采用OOP设计,会产生大量冗余代码、不利于系统维护,AOP则利用aspect(横切)技术很好的解决了这些问题,AOP不与特定的代码耦合,而是通过特定切入点实现业务的增强处理。

  1)AOP术语:

  切面(Aspect):用于组织多个advice(增强处理)

  连接点(Joinpoint):程序之执行过程中明确的点,如方法的调用或异常的抛出

  增强处理(Advice):特定切入点执行的增强处理,有before、around、after等。

  切入点(Pointcut):可以插入增强处理的连接点。

  2)测试

  java代码 

public class AspectTest {
    public void log(JoinPoint jb, Object obj) {
        System.out.println("AfterReturning [" + " class name:" + jb.getTarget()+ ", method name:" + jb.getSignature().getName() + ", back vlue:" + obj + "]");
    }
}

  spring-mvc.xml文件

<bean id="aspectTest" class="com.example.spect.AspectTest"></bean>
    <aop:config>
        <aop:aspect id="aspectTest" ref="aspectTest" order="2">
            <aop:after-returning method="log" pointcut="execution(* com.example.controller.*.*(..))" returning="obj"></aop:after-returning>
        </aop:aspect>
    </aop:config>

  pom.xml文件(仅aop所需依赖)

   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.10</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.10</version>
    </dependency>

  运行结果

AfterReturning [ class name:com.example.controller.UserCotroller@447f8cce, method name:loginView, back vlue:login]

二、具体搭建

1、idea创建web项目

  Intellij IDEA创建Maven Web项目

2、springmvc实现页面跳转

  web.xml中添加

<!-- Spring MVC servlet -->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  创建spring-mvc.xml文件resource或WEB-INF目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="com.example.*"/>

    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
        <property name="redirectHttp10Compatible" value="false"/>
    </bean>
</beans>

  pom.xml中添加依赖

    <!--spring-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${org.springframework-version}</version>
    </dependency>

    <!--servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

 3、常见错误跳转页面

  web.xml中添加如下代码

  <!-- 出错页面定义 -->
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/WEB-INF/views/500.jsp</location>
  </error-page>
  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/views/404.jsp</location>
  </error-page>

4、springmvc国际化

  spring-mvc.xml中添加

    <!--国际化-->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>message.message</value>
            </list>
        </property>
        <property name="defaultEncoding" value="utf-8"/>
    </bean>

  创建message_en_US.properties,message_zh_CN.properties文件再resource目录下

  message_zh_CN.properties

hello=hello

  message_zh_CN.properties

hello=你好

  后台获取代码

    @Autowired
    private ResourceBundleMessageSource messageSource;

    @RequestMapping(value = "/loginView")
    public String loginView() {
        System.out.println(messageSource.getMessage("hello", null, null));
        return "login";
    }

  页面获取

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
    <title>test</title>
</head>
<body>
    <h1><spring:message code="hello"/></h1>
</body>
</html>

5、日志处理

  springmvc之log4j

6、ApplicationContext的事件机制

  对ApplicationContext容器中的事件进行监听。

     Spring中ApplicationContext的事件机制

7、spring事务管理

  springmvc+hibernate4事务管理配置

8、spring定时器

  springmvc之定时器

9、spring拦截器

8、常见面试题

  SpringMvc面试题

  Spring MVC之@RequestMapping 详解

原文地址:https://www.cnblogs.com/sunjf/p/6437781.html