Spring——总结

简介

  总结一下Spring远离,作用和配置流程

 

原理

  Spring

    作用:存储对象的容器。通过配置标签获取容器对象和容器对象初始化

    标签:

      <bean id="" class="" > 

      <property name="" ref=""|value="" >

      配置标签的思路:标记id,找到class

      通过value或者ref对set方法里属性赋值

      controller使用注解加入,所以要包扫描

    配置:

      就是配置标签,通过从数据源开始,层层配置引用而往容器中添加对象。

      需要加入scan包扫描,扫描controller里需要的对象主动注入

    加载:

      spring不是sevlet,是监听器<listener>监听项目而生成。context-item加载地址

    取用:

      肯定是在controller里取用,因为controller是mvc域。所以我使用注解,通过@AutoWrite和@Qualifier标注成员变量。

    

  spring-mvc

    作用:

      通过servlet拦截请求,作方法映射

    配置:

      1.包扫描,扫描controller层获取方法映射的对象和方法

      2.driver-anotion:识别注解

      3.不认识的资源交给tomcat处理

      @RequestMapping(value="",produces="")

      @ResponseBody

     加载

      通过servlet来加载

      <init-param>ContextLoader

     

  额外配置:

    拦截器---处理中文字符集

 

ObjectMapper mapper = new ObjectMapper(); 
Person person = new Person(); 
person.setName("Tom"); 
person.setAge(40); 
String jsonString = mapper.writerWithDefaultPrettyPrinter() 
.writeValueAsString(person); 
Person deserializedPerson = mapper.readValue(jsonString, Person.class);

// 非web环境获取applicationContext   
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml"); UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");

 




 <!-- 加载Spring  -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

 

  <!-- 加载spring-mvc  -->
  <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-mvc.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>

 

posted @ 2021-12-09 01:31  remix_alone  阅读(23)  评论(0)    收藏  举报