Spring基础

spring基础

1.spring的概念
2.IOC容器
3.Aop
4.JdbcTemplate
5.事务管理
6.spring5新特性

一、spring框架概述

1.Spring是轻量的开源JavaEE框架
2.Spring可以解决企业应用开发的复杂性
3.Spring有两个核心部分:IOCAop
(1)IOC:控制反转,把创建对象过程交给Spring进行管理
(2)Aop:面向切面,不修改源代码进行功能增强
4.Spring的特点
(1)方便解耦,简化开发
(2)Aop编程支持
(3)方便程序测试
(4)方便和其他框架进行整合
(5)方便进行实务操作
(6)降低API开发难度

1.<spring的入门案例>

@Data
public class User {
    public void add(){
        System.out.println("add.....");
    }
}

public class TestSpring5 {
    @Test
    public void test(){
        //1.加载spring配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
        //2.获取配置创建的对象
        User user = context.getBean("user",User.class);
        System.out.println(user);
        user.add();
    }
}


<?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: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名称空间-->
<bean id="user" class="entity.User">

</bean>
</beans>

二、IOC容器

(一)IOC底层原理

1.什么是IOC

  1. (1)控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理
  2. (2)使用IOC目的:为了降低耦合度
  3. (3)入门案例就是IOC实现
  4. (4)底层:XML解析,工厂模式(降低耦合),反射

(二)IOC接口(BeanFactory)

1.IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
2.Spring提供IOC容器实现两种方式(两个接口)
(1)BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员进行使用
加载配置文件时不会创建对象,在获取对象(使用)才会去创建对象
**<什么时候用,什么时候创建>**
(2)ApplicationContext:BeanFactory接口的子接口,提供更多强大的功能,面向开发人员进行使用.
加载配置文件时就会把配置文件对象进行创建
**<先创建,使用的时候直接获取>**
ApplicationContext有实现类

什么是Bean管理
(1)Spring创建对象
(2)Spring注入属性

(三)IOC操作Bean管理(基于XML)

1.基于XML创建对象

<!-- 配置User对象创建-->
<bean id="user" class="entity.User"></bean>

(1)在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
(2)在bean标签有很多属性,介绍常用的属性

  1. id属性:唯一的标识
  2. class属性:类路径(包路径)
  3. name:
    (3)创建对象时,默认是执行无参构造方法完成对象创建
  1. 基于xml方式注入属性

(1)DI:依赖注入,就是注入属性(DI是IOC的一种实现)

public class User {
    private String name;
    private Long age;

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

    public void setAge(Long age) {
        this.age = age;
    }

    public void add(){
        System.out.println("add.....");
    }
}

<!--    set方法注入属性-->
    <bean id="user" class="entity.User">
<!--        使用property完成属性注入
            name:类里面属性名称
            value:向属性注入的值
-->
    <property name="name" value="鸡你太美"></property>
    <property name="age" value="18"></property>
    
<!--    属性为null-->
<property name="age">
<null/> 
</property>


<!-- 属性包含特殊符号-->
<property name="age">
<value>
	<![CDATA[<<鸡你太美>>]]>
</value>
</property>

    </bean>
</beans>
public class User {
    private String name;
    private Long age;

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

    public void setAge(Long age) {
        this.age = age;
    }

    public void add(){
        System.out.println("add.....");
    }

    public User(String name, Long age) {
        this.name = name;
        this.age = age;
    }
}


    <!--    有参构造方法注入属性-->
    <bean id="user" class="entity.User">
        <constructor-arg name="name"  value="鸡你太美"/>
        <constructor-arg name="age"  value="18"/>

    </bean>

public interface UserDao {
}

public class UserDaoImpl implements UserDao {
}

public class UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}




<!--    </bean>-->
<!--        service和dao对象创建 -->
    <bean id="UserService" class="Service.UserService">
<!--        注入userDao对象
            name属性值:类里面属性名称
            ref属性:创建userDao对象bean标签id值。注入对象的外部bean           -->
        <property name="userDao" ref="UserDaoImpl"></property>
    </bean>

    <bean id="UserDaoImpl" class="Dao.UserDaoImpl"></bean>
// 员工类
public class Emp {
    private String ename;
    private String gender;
    private Dept dept;
    // 员工属于某一个部门,使用对象形式表示
    public void setEname(String ename) {
        this.ename = ename;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public Dept getDept() {
        return dept;
    }

    public void add(){
        System.out.println(ename+"::"+gender+"::"+dept);
    }
}

// 部门类
public class Dept {
    private String dName;
    public void setdName(String dName) {
        this.dName = dName;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "dName='" + dName + '\'' +
                '}';
    }
}


    <!--内部bean    -->
    <bean id="emp" class="bean.Emp">
<!--设置两个普通的属性        -->
        <property name="ename" value="cxk"></property>
        <property name="gender" value="女"></property>
<!--        设置对象类型属性-->
        <property name="dept">
            <bean id="dept" class="bean.Dept">
                <property name="dName" value="篮球部"></property>
            </bean>
        </property>
    </bean>


    <!--级联赋值    -->
    <bean id="emp" class="bean.Emp">
<!--设置两个普通的属性        -->
        <property name="ename" value="cxk"></property>
        <property name="gender" value="女"></property>
<!--        设置对象类型属性-->
<!--        级联赋值-->
        <property name="dept" ref="dept"></property>
        <property name="dept.dName" value="中分部"></property>
    </bean>
    <bean id="dept" class="bean.Dept">
        <property name="dName" value="篮球部"></property>
    </bean>

public class TestSpring5 {
    @Test
    public void test(){
        //1.加载spring配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean3.xml");
        //2.获取配置创建的对象
        Emp emp = context.getBean("emp",Emp.class);
        System.out.println(emp);
        emp.add();
    }
}

在这里插入图片描述
在这里插入图片描述

(四)IOC操作Bean管理(基于注释)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值