spring mvc 基础

  • spring mvc 部署在 tomcat 的2种方式(原理一样):
  1. 右键单击项目,使用 Export > WAR File 选项,将 war 文件保存在 Tomcat 的 webapps 文件夹中,启动tomcat(终端命令:sudo sh startup.sh)。
  2. 在 eclipse ee 中新建 tomcat server,将项目部署到server里 (推荐此种方式,eclipse修改能同步发布到tomcat)。
    eclipse EE 配置 tomcat:
    eclipse => preferences => server => runtime environments => 选择要配置的服务器 => 指定服务器安装路径 => finish
  • Spring 只能对接口 AOP

  • 依赖注入: 对应用程序对象相互解耦
    AOP: 从它们所影响的对象中对横切关注点解耦

  • AOP 模块提供拦截器来拦截一个应用程序,例如,当执行一个方法时,你可以在方法执行之前或之后添加额外的功能。

  • Spring 支持 @AspectJ 注解的方法和基于 xml 的方法来实现自定义切面。

  • AOP 有5种通知类型:
    before
    after
    after-returning
    after-throwing
    around

  • Spring 提供了以下的标准事件:
    ContextRefreshedEvent
    ContextStartedEvent
    ContextStoppedEvent
    ContextClosedEvent
    RequestHandledEvent

  • @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。
    @Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。

  • 指定 Bean 的范围:
    默认范围是单实例,但是你可以重写带有 @Scope 注解的该方法,如下所示:

@Configuration
public class AppConfig {
   @Bean
   @Scope("prototype")
   public Foo foo() {
      return new Foo();
   }
}
  • 生命周期回调
    @Bean 注解支持指定任意的初始化和销毁的回调方法,就像在 bean 元素中 Spring 的 XML 的初始化方法和销毁方法的属性:
public class Foo {
   public void init() {
      // initialization logic
   }
   public void cleanup() {
      // destruction logic
   }
}

@Configuration
public class AppConfig {
   @Bean(initMethod = "init", destroyMethod = "cleanup" )
   public Foo foo() {
      return new Foo();
   }
}
  • 当 @Beans 依赖对方时,表达这种依赖性非常简单,只要有一个 bean 方法调用另一个,如下所示:
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class AppConfig {
   @Bean
   public Foo foo() {
      return new Foo(bar());
   }
   @Bean
   public Bar bar() {
      return new Bar();
   }
}
  • 问:spring mvc 中在引包的时候为什么要引入 commons-logging.jar?
    答:因为 spring 的一些类里面用到了 commons-logging.jar 里面的类。

  • Spring 容器是 Spring 框架的核心。容器将创建对象,把它们连接在一起,配置它们,并管理他们从创建到销毁的整个生命周期。
    Spring 提供了两种不同类型的容器:
    BeanFactory 容器
    ApplicationContext 容器

  • Application Context 是 spring 中较高级的容器。
    ApplicationContext 包含 BeanFactory 所有的功能,一般情况下,推荐使用 ApplicationContext 。
    BeanFactory 仍然可以在轻量级应用中使用,比如移动设备或者基于 applet 的应用程序。

  • 依赖注入方式有2种:

  1. setter 注入
  2. 构造函数注入
    这两种方法的不同:
    1.在xml文件下对应使用property和constructor-arg属性。
    2.构造函数方法,有构造函数注入的代码。
    例如:
    property属性:(其中name的值为原类中的属性名)
    constructor-arg属性:(其中index的值为0~n-1,n代表构造函数中的输入参数的数量)
  • 用name和id都可以成功注入。有id时通过id注入,没有id时通过name注入。id只能一个,name可以有多个,根据name属性注入时找第一个name。
    spring依赖注入有byName和byType两种方式,id和name属性都是通过byName注入,type属性通过byType注入

  • property 标签简写

   <bean id="john-classic" class="com.example.Person">
      <property name="name" value="John Doe"/>
      <property name="spouse" ref="jane"/>
   </bean>

   <bean name="jane" class="com.example.Person">
      <property name="name" value="John Doe"/>
   </bean>

等价于:

   <bean id="john-classic" class="com.example.Person"
      p:name="John Doe"
      p:spouse-ref="jane"/>
   </bean>

   <bean name="jane" class="com.example.Person"
      p:name="John Doe"/>
   </bean>
  • 如果想要向一个对象传递一个引用,你需要使用 标签的 ref 属性;如果想要直接传递值,那么应该使用 value 属性。

  • 1.setter方法注入
    setter方法注入即是创建一个普通的JavaBean类,为需要注入的属性通过对应的setter方法即可,如:
    (1)创建一个Id类:

package com.loster.li;  
  
public class Id {  
    private int id;  
    private String name;  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
}  

(2)创建配置文件Id_Bean.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
             
    <bean id="id" class="com.loster.li.Id">  
    <property name="id" value="123"></property>  
    <property name="name" value="xiaoli"></property>  
    </bean>  
</beans>  

(3)编写测试类IdTest.java,并运行:

package com.loster.li;  
  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class IdTest {  
    public static void main(String[] args){  
        ClassPathXmlApplicationContext context = new   
                ClassPathXmlApplicationContext("com/loster/li/Id_Bean.xml");  
          
        Id id = (Id)context.getBean("id");  
        System.out.println(id.getId());  
        System.out.println(id.getName());  
    }  
}  

运行结果如下:

123
xiaoli
  • 构造方法注入
    (1)将上面的Id.class修改为:
package com.loster.li;  
  
public class Id {  
    private int id;  
    private String name;  
    public Id(int id,String name){  
        this.id = id;  
        this.name = name;  
    }  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
}  

(2)将上面的Id_Bean.xml修改为:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
             
    <bean id="id" class="com.loster.li.Id">  
    <constructor-arg index="0" value="456"></constructor-arg>  
    <constructor-arg index="1" value="dawang"></constructor-arg>  
    </bean>  
</beans>  

(3)再次运行IdTest.java,运行结果如下:

456
dawang
  • 构造函数注入,其他用法:
    1.想要向一个对象传递一个引用,需要使用标签的 ref 属性
  <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <constructor-arg ref="spellChecker"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

2.传递给构造函数的类型不同

package x.y;
public class Foo {
   public Foo(int year, String name) {
      // ...
   }
}

使用 type 属性显式的指定了构造函数参数的类型,容器也可以使用与简单类型匹配的类型

<beans>
   <bean id="exampleBean" class="examples.ExampleBean">
      <constructor-arg type="int" value="2001"/>
      <constructor-arg type="java.lang.String" value="Zara"/>
   </bean>
</beans>

3.最好的传递构造函数参数的方式:使用 index 属性来显式的指定构造函数参数的索引(推荐)

   <bean id="exampleBean" class="examples.ExampleBean">
      <constructor-arg index="0" value="2001"/>
      <constructor-arg index="1" value="Zara"/>
   </bean>
  • 注入内部 bean
    <bean id="textEditor" class="com.tutorialspoint.TextEditor">
        <property name="spellChecker">
            <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"/>
        </property>
    </bean>
  • Spring 提供了四种集合配置元素: list、set、map、props
         <list>
            <value>INDIA</value>
         </list>
         <set>
            <value>INDIA</value>
        </set>
         <map>
            <entry key="1" value="INDIA"/>
         </map>
         <props>
            <prop key="one">INDIA</prop>
         </props>
  • 分别注入 空字符串 和 null 值
<property name="email" value=""/>
<property name="email"><null/></property>
  • 自动装配有4种:
  1. no
  2. byName
  3. byType
  4. constructor
  5. autodetect
  • autodetect(自动装配模式)下:spring 首先尝试使用 constructor 模式来自动装配,如果不成功,Spring 尝试通过 byType 来自动装配。

  • spring 默认不打开注解装配;我们可以在 spring 配置文件中启用:
    context:annotation-config/

  • 注解,让 spring 自动装配值到属性、方法、构造函数。

  • @Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。

  • Spring 2.5引入了 @Autowired注释, 它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。

  • @Autowired 用在 setter 方法上,xml 配置文件中不用写元素 property。
    @Autowired 用在属性上,可以省略 setter 方法,且xml 配置文件中不用写元素 property。
    @Autowired 用在构造函数上,xml 配置文件中不用写元素 constructor-arg。

  • 当有多个相同类型的 bean 实例时,@Autowired 配合 @Qualifier 使用,@Qualifier 指定具体使用的实例。

  • @PostConstruct 和 @PreDestroy 注解,相当于 xml 配置文件中的 init-method 和 destroy-method。

posted on 2017-07-01 14:00  cag2050  阅读(148)  评论(0编辑  收藏  举报

导航