Spring学习笔记(一)

1.简介

简介:(作者:Rod Johnson)
Spring的主要特性仅仅是依赖注入(DI)和面向切面编程(AOP),最根本使命是:简化Java开发
为了降低Java开发的复杂性,Spring采用了一下四种关键策略:

  • 基于POJO的轻量级和最小侵入性编程
  • 通过依赖注入和面向接口实现松耦合
  • 基于切面和惯例进行声明式编程
  • 通过切面和模版减少样板式代码

IOC(Inversion of Controler):控制反转
控制:指谁来控制对象的创建;传统的应用程序对象的创建是由程序本身控制的,使用Spring后,是由Spring来创建对象的
反转:正转是指由程序来创建,反转指程序本身不去创建对象,而变为被动接收的对象
总结:以前对象是由程序本身来创建,使用Spring后,程序变为被动接收Spring创建好的对象

IOC的实现是通过ioc容器来实现的,ioc容器其实就是一个BeanFactory

2.IOC创建对象的三种方式

通过无参数的构造方法来创建

//User类
package com.spring.learning.vo;

public class User{
    private String name;

    public User(){
        System.out.println("User的无参数构造方法");
    }

    public void setName(String name) {
        this.name = name;
    }

    public void show(){
        System.out.println("name = " + name);
    }
}
//测试类
package com.spring.learning.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.learning.vo.User;

public class Test {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User)ac.getBean("user");
        user.show();
    }

}


<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.xsd">

    <bean id="user" class="com.spring.learning.vo.User">
        <property name="name" value="张三">property>
    bean>
beans>

通过有参数的构造方法来创建

//User类
package com.spring.learning.vo;

public class User{
    private String name;

    public User(String name){
        super();
        this.name = name;
    }   
    public void show(){
        System.out.println("name = " + name);
    }
}


<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.xsd">


    
    <bean id="user" class="com.spring.learning.vo.User">
        <constructor-arg index="0" value="李四" />
    bean>

    
    <bean id="user" class="com.spring.learning.User">
        <constructor-arg name="name" value="李四">constructor-arg>
    bean>

    
    <bean id="user" class="com.spring.learning.User">
        <constructor-arg type="java.lang.String" value="李四">constructor-arg>
    bean>

beans>

通过工厂方式来创建

静态工厂:
在第二种有参数构造方法的基础上,新建一个UserFactory类

package com.spring.learning.factory;

import com.spring.learning.vo.User;

public class UserFactory {
    public static User newInstance(String name){
        return new User(name);
    }
}

<bean id="user" class="com.spring.learning.factory.UserFactory" factory-method="newInstance">
        <constructor-arg index="0" value="王五">constructor-arg>
    bean>

动态工厂:
在第二种有参数构造方法的基础上,新建一个UserDynamicFactory类

package com.spring.learning.factory;

import com.spring.learning.vo.User;

public class UserDynamicFactory {
    public User newInstance(String name){
        return new User(name);
    }
}

    <bean id="userFactory" class="com.spring.learning.factory.UserDynamicFactory" />
    <bean id="user" factory-bean="userFactory" factory-method="newInstance">
        <constructor-arg index="0" value="王五">constructor-arg>
    bean> 

bean.xml的配置

-- id是bean的标识符,是唯一的,如果没有配置id,则name为默认标识符
         如果配置了id,又配置了name,则name是别名
         name可以设置多个别名,分隔符可以是空格,分号,逗号
         如果没有配置idname,也可以根据applicationContext.getBean(class)获取对象
     -->
    id="h1" name="helo"class="com.spring.learning.Hello">
    <property name="name" value="aaa">property>
    

3.DI注入的几种方式

DI —— 依赖注入
依赖是指:bean对象的创建依赖容器,bean对象的依赖资源
注入:指bean对象依赖的资源由容器来设置和装配

Spring注入
构造器注入:与IOC的对象的创建类似

setter注入:要求被注入的属性必须有set方法,方法名由set+属性首字母大写。如果属性是boolean,则没有set方法,是is方法

  • 常量注入
id="user" class="com.spring.learning.vo.User">
        <property name="name" value="张三">property>
    
  • bean注入
id="addr" class="com.spring.learning.vo.Address">
id="user" class="com.spring.learning.vo.User">
        <property name="name" value="张三">property>
        <property name="addr" ref="addr">property>
    
  • 数组注入
<bean id="addr" class="com.spring.learning.vo.Address">
<bean id="user" class="com.spring.learning.vo.User">
        <property name="name" value="张三">property>
        <property name="addr" ref="addr">property>
        <property name="books">
            <array>
                <value>aaavalue>
                <value>bbbvalue>
                <value>cccvalue>
            array>
        property>
    bean>
  • list注入
<bean id="addr" class="com.spring.learning.vo.Address">
<bean id="user" class="com.spring.learning.vo.User">
        <property name="name" value="张三">property>
        <property name="addr" ref="addr">property>
        <property name="books">
            <array>
                <value>aaavalue>
                <value>bbbvalue>
                <value>cccvalue>
            array>
        property>
        <property name="hobbies">
            <list>
                <value>dddvalue>
                <value>eeevalue>
                <value>fffvalue>
            list>
        property>
    bean>
  • map注入
<bean id="addr" class="com.spring.learning.vo.Address">
<bean id="user" class="com.spring.learning.vo.User">
        <property name="name" value="张三">property>
        <property name="addr" ref="addr">property>
        <property name="books">
            <array>
                <value>aaavalue>
                <value>bbbvalue>
                <value>cccvalue>
            array>
        property>
        <property name="hobbies">
            <list>
                <value>dddvalue>
                <value>eeevalue>
                <value>fffvalue>
            list>
        property>
        <property name="cards">
            <map>
                <entry key="aaa" value="bbb">entry>
                <entry>
                    <key><value>mmmvalue>key>
                    <value>nnnvalue>
                entry>
                <entry key="ccc" value="ddd">entry>
            map>
        property>
    bean>
  • set注入
<bean id="addr" class="com.spring.learning.vo.Address">
<bean id="user" class="com.spring.learning.vo.User">
        <property name="name" value="张三">property>
        <property name="addr" ref="addr">property>
        <property name="books">
            <array>
                <value>aaavalue>
                <value>bbbvalue>
                <value>cccvalue>
            array>
        property>
        <property name="hobbies">
            <list>
                <value>dddvalue>
                <value>eeevalue>
                <value>fffvalue>
            list>
        property>
        <property name="cards">
            <map>
                <entry key="aaa" value="bbb">entry>
                <entry>
                    <key><value>mmmvalue>key>
                    <value>nnnvalue>
                entry>
                <entry key="ccc" value="ddd">entry>
            map>
        property>
        <property name="games">
            <set>
                <value>dddvalue>
                <value>eeevalue>
                <value>fffvalue>
            set>
        property>
    bean>
  • null注入
<bean id="addr" class="com.spring.learning.vo.Address">
<bean id="user" class="com.spring.learning.vo.User">
        <property name="name" value="张三">property>
        <property name="addr" ref="addr">property>
        <property name="books">
            <array>
                <value>aaavalue>
                <value>bbbvalue>
                <value>cccvalue>
            array>
        property>
        <property name="hobbies">
            <list>
                <value>dddvalue>
                <value>eeevalue>
                <value>fffvalue>
            list>
        property>
        <property name="cards">
            <map>
                <entry key="aaa" value="bbb">entry>
                <entry>
                    <key><value>mmmvalue>key>
                    <value>nnnvalue>
                entry>
                <entry key="ccc" value="ddd">entry>
            map>
        property>
        <property name="games">
            <set>
                <value>dddvalue>
                <value>eeevalue>
                <value>fffvalue>
            set>
        property>
        <property name="yyy">
            <null>null>
            set>
        property>
    bean>
  • properties注入
<bean id="addr" class="com.spring.learning.vo.Address">
<bean id="user" class="com.spring.learning.vo.User">
        <property name="name" value="张三">property>
        <property name="addr" ref="addr">property>
        <property name="books">
            <array>
                <value>aaavalue>
                <value>bbbvalue>
                <value>cccvalue>
            array>
        property>
        <property name="hobbies">
            <list>
                <value>dddvalue>
                <value>eeevalue>
                <value>fffvalue>
            list>
        property>
        <property name="cards">
            <map>
                <entry key="aaa" value="bbb">entry>
                <entry>
                    <key><value>mmmvalue>key>
                    <value>nnnvalue>
                entry>
                <entry key="ccc" value="ddd">entry>
            map>
        property>
        <property name="games">
            <set>
                <value>dddvalue>
                <value>eeevalue>
                <value>fffvalue>
            set>
        property>
        <property name="yyy">
            <null>null>
            set>
        property>
        <property name="info">
            <props>
                <prop key="no">2015prop>
                <prop key="sex">maleprop>
                <prop key="name">aaaprop>
            props>
        property>
    bean>
  • p命名空间注入
    需要在头文件中加入:
    xmlns:p=”http://www.springframework.org/schema/p”

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="user" class="com.spring.learning.vo.User" 
 p:name="aaa" p:age="20">bean>
  • c命名空间注入
    需要在头文件中加入:
    xmlns:p=”http://www.springframework.org/schema/c”

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"   
    xmlns:p="http://www.springframework.org/schema/c"      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">



<bean id="user" class="com.spring.learning.vo.User" 
 c:name="aaa" c:age="20">bean>

4.bean的作用域

--bean的作用域
scope="singleton"----单例模式 ,整个容器中只有一个对象实例(默认)
scope="prototype"-----原型模式,每次获取bean都产生一个新的对象
scope="request"----每次请求时产生一个新的对象
scope="session"----在会话范围内产生一个新的对象
scope="global session"----只在portlet下有用,表示application
scope="application"----在应用范围内产生一个新的对象
-->
id="user" class="com.spring.learning.vo.User" scope="singleton">
        <property name="name" value="张三">property>

注意:在整合structs2和spring时需要将action设为scope=”prototype”;

5.bean的自动装配

bean的自动装配—–简化spring配置
在配置bean时,可以配置bean的autowire属性,用于指定装配类型


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"   
    xmlns:p="http://www.springframework.org/schema/c"      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >


<bean id="user" class="com.spring.learning.vo.User" autowire="byName">
        <property name="name" value="张三">property>
    bean>
beans>

可以配置全局的自动装配类型,在头部default-autowire

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"   
    xmlns:p="http://www.springframework.org/schema/c"      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">

你可能感兴趣的:(spring,Spring)