Spring4.x学习笔记 ——Spring概念、IOC概念及基本使用方法

一、Spring框架相关概念

  • 开源的轻量级框架
  • Spring框架主要涉及两部分

    • AOP:面向切面编程(对代码扩展的优化)
    • IOC:控制反转(对象创建方式的改变)
  • Spring为一站式框架,提供javaee三层中的技术支持

    • web层(展现层):springMVC
    • servive层(业务逻辑层):spring的IOC
    • dao层(数据访问层):spring的jdbcTemplate

二、spring的IOC操作基础(配置文件方式)

1、底层原理

  • 将类写入.xml文件,与id(或name)属性绑定
  • dom4j对xml文件解析,完成类的一个映射
  • spring采用工场模式,利用java的反射机制创建对象
    • Class.forName(“类名”).newIntance(); //确保该类的无参构造存在,通过该构造创建对象

2、.xml配置格式

  • 引入核心jar包 beans、context、core、expression
  • 位置名称可以随意
  • 引入schema约束
  • 例:
<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

3、Bean标签的常用属性

  • id:为类绑定一个名字,任意,不可重,不能有符号
  • name:同id的作用,可以用符号,为了兼容其他框架的低版本,先大多以用id替代
  • class:类的完整路径
  • scope:类的属性
    • singleton:默认,单例
    • prototype:多例

4、创建对象,Bean实例化的三种方式

(1)使用类的无参构造创建(最常用)
  • 创建有无参构造的类
public class User {
    public User() {
        super();
    }
    public void show() {
        System.out.println("success");
    }

}
  • 配置
    id随意,class为类的完整路径
<bean id="user" class="com.springtest.createtest.User"></bean>
  • 创建类
public static void main(String[] args) {
        //1、加载spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2、得到对象
        User user;
        user = (User) context.getBean("user"); //无参构造方式创建(最常用)
        user.show();
    }
(2)静态工厂模式
  • 为User类创建工厂类,提供静态方法返回一个User对象
public class UserFactoryStatic {
    public static User getUser() {
        return new User();
    }
}
  • 配置
<bean id="userFactoryStatic" class="com.springtest.createtest.UserFactoryStatic" factory-method="getUser"></bean>
  • 创建
 user = (User) context.getBean("userFactoryStatic"); //静态工厂模式创建
(3)实例工厂模式
  • 为User类创建工厂类,提供普通方法返回一个User对象
public class UserFactory {
    public  User getUser() {
        return new User();
    }
}
  • 配置
<bean id="userFactory" class="com.springtest.createtest.UserFactory"></bean>
<bean id="user" factory-bean="userFactory" factory-method="getUser"></bean>
  • 创建
user = (User) context.getBean("user"); //实例工厂模式创建

5、属性注入

(1)有参构造方式注入(Bean标签下用constructer-arg标签)
  • 测试类
public class Student {
    private String name;
    private int age;
    public Student() {
    }
    public Student(String n , int a) {//有参构造
        this.name = n;
        this.age = a;
    }
    public void show() {
        System.out.println(name+"已经"+age+"岁了");
    }
}
  • 配置
<bean id="student" class="com.springtest.proinput.Student"> -->
        <!-- 构造方法的形参名 -->
       <constructor-arg name="n" value="小明"></constructor-arg>
       <constructor-arg name="a" value="12"></constructor-arg>
</bean>
(2)P名称空间注入
  • 还是以上测试类,beans标签属性加入配置
 xmlns:p="http://www.springframework.org/schema/p"

创建类的配置:

<bean id="student" class="com.springtest.proinput.Student" p:name:小明>
(3)set()注入(最常用)
  • 注入基本数据类型
    配置:
<bean id="student" class="com.springtest.proinput.Student" >
        <property name="name" value="小明"></property>
        <property name="age" value = "13"></property>
</bean>
  • 注入对象
//Student加入属性:
private Grade grade;
//Grade类为:
public class Grade {
    private int Math;
    private int Chinese;
    public int getMath() {
        return Math;
    }
    public void setMath(int math) {
        Math = math;
    }
    public int getChinese() {
        return Chinese;
    }
    public void setChinese(int chinese) {
        Chinese = chinese;
    }
}
<!--配置Grade:-->
<bean id="grade" class="com.springtest.proinput.Grade">
        <property name="Math" value="99"></property>
        <property name="Chinese" value="78"></property>
</bean>
<!-- 配置Student: -->
<bean id="student" class="com.springtest.proinput.Student"> 
        <property name="name" value="小明"></property>
        <property name="age" value = "13"></property>
        <property name="grade" ref="grade"></property>
</bean>
  • 注入复杂数据(以String[]、List、map、properties为例)
//Student加入以下属性:
private String[] arr;
private Map<String,Grade> map;
private List<String> list;
private Properties properties; 
<!--配置:-->
  <!-- String[] -->
        <property name="arr">
            <list>
                <value>小明</value>
                <value>小刚</value>
                <value>小红</value>
            </list>
        </property>

        <!-- List -->
        <property name="list">
            <list>
                <value>小明</value>
                <value>小刚</value>
                <value>小红</value>
            </list>
        </property>

        <!-- Map<String,Grade> -->
        <property name="map">
            <map>
                <entry key="grade1" value-ref="grade1"></entry>
                <entry key="grade" value-ref="grade"></entry>
            </map>
        </property>

        <!-- Properties -->
        <property name="properties">
            <props>
                <prop key="key">value</prop>
                <prop key="key2">value2</prop>
            </props>
        </property>

三、spring的IOC操作基础(注解方式)

1、注解的基础形式 @注解名(属性=值)

2、配置准备:

  • 引入四个基本jar包及spring-aop包
  • 引入新约束context
  • 配置中开启注解扫描
<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"
    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">
    <!-- 扫描包下的所有注解 -->
    <context:component-scan base-package="com.annotest"></context:component-scan>
</beans>

3、创建对象

  • @Component(value=”….”) //value单词可省略,相当于配置方式中的id=”…”
  • @Controller //Web层类
  • @Service //业务层
  • @Repository //持久层
  • 后三个为Component的扩展,现阶段完全等效
  • 附:配置实例模式 @scope(value=”singleton/prototype”)
@Service(value="student")
@Scope(value="prototype")
public class Student {
    public void Show() {
        System.out.println("success");
    }
}
public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = (Student)context.getBean("student");
        stu.Show();
    }

4、注入属性

  • 普通属性 @Value(value=”…”)
  • 注入对象
    • @Autowired 自动注入(直接找类的名字)
    • @Resource(name=”…”) (常用,name的值为注入类的id,可省略)
@Service("grade")
public class Grade {
    private int Math;
    private int Chinese;
    public void Show() {
        System.out.println("grade");
    }
}
@Service(value="student")
@Scope(value="singleton")
public class Student {
    //第一种注入注解
    @Autowired
    private Grade grade;
    //第二种注入注解
    @Resource(name="grade")
    private Grade grade2;
    @Value(value="Tom")
    private String name;
    public Grade getGrade2() {
        return grade2;
    }
    public Grade getGrade() {
        return grade;
    }
    public void Show() {
        System.out.println("success");
    }
}
public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = (Student)context.getBean("student");
        stu.Show();
        stu.getGrade().Show();
        stu.getGrade2().Show();
    }

5、spring一般采用配置方式创建对象,注解方式注入属性!

6、IOC与DI的关系

  • IOC:控制反转
  • DI:依赖注入
  • DI不能单独操作,需在IOC的基础上进行

猜你喜欢

转载自blog.csdn.net/qq_37969433/article/details/80027314