Spring学习笔记——day01

前言

记录我的Spring学习之路

开始

一、Spring概念

1.Spring是一个开源的轻量级框架

2.Spring是一个分层的JavaEE一站式框架
(1)Spring在JavaEE三层结构中,每一层都提供了不同的解决技术
web层:SpringMVC
service层:Spring的IOC
dao层:Spring的jdbcTemplate

3.Spring的核心有两部分:
(1)AOP:面向切面编程,扩展一个功能不是修改源代码实现
(2)IOC:控制反转,对象的创建不是通过new实现,而是通过Spring配置创建对象

4.Spring版本:
(1)Hebernate 5.x
(2)Spring 4.x

二、Spring的IOC

1.把对象的创建直接交给Spring进行管理

2.IOC操作两部分:
(1)IOC的配置文件方式
(2)IOC注解方式

3.IOC底层原理
IOC底层原理使用的技术:
(1)XML配置文件
(2)dom4j解决xml
(3)工厂设计模式
(4)反射
画图分析:
这里写图片描述
这里写图片描述

三、Spring的IOC入门案例

1.导入jar包
这里写图片描述
2.创建类,再类中创建方法
3.创建Spring配置文件,配置创建类
(1)Spring核心配置文件的名字和位置不是固定的
一般建议放到src下面,官方建议名字叫applicationContext.xml
(2)引入schema约束

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

(3)配置对象创建

<!-- 告诉Spring,bean都放在com.how2java.pojo这个包下 -->
    <context:component-scan base-package="com.how2java.pojo"/>

    <!-- 表示告诉Spring要用注解的方式进行配置 -->
    <context:annotation-config/> 
    <!-- IOC配置 -->
    <!-- applicationContext.xml是Spring的核心配置文件,
    通过关键字c即可获取Category对象,
    该对象获取的时候,即被注入了字符串"category 1“到name属性中 -->
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>

    <!-- 在创建Product的时候注入一个Category对象 -->
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
        <!-- 这个行为在后面将使用注解来完成<property name="category" ref="c" />   -->
    </bean>

4.写代码测试对象创建

 //1加载Spring配置值文件,根据创建对象
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });

 /*使用注解的方式完成注入对象中的效果*/
        //的到配置创建的对象
        Product p = (Product) context.getBean("p");
        System.out.println(p.getName());
        System.out.println(p.getCategory().getName());             

后记

先写到这里,下周继续

猜你喜欢

转载自blog.csdn.net/zyw644451/article/details/80366473
今日推荐