2021-04-13----spring5----IOC容器(IOC中的bean管理)

1.IOC容器(底层原理)

1.1 什么是IOC

      1.1.1 控制反转,把对象创建和对象之间的调用过程交给spring管理

      1.1.2 目的:降低耦合度。

1.2 IOC底层原理

     xml解析,工场模式,反射。

2.IOC接口(BeanFactory)

3.IOC操作bean管理

  3.1 什么是bean管理(指的是两个操作)

         (1)spring创建对象

         (2)spring注入属性

  3.2 bean创建的两种方式

        3.2.1 基于xml

                  1)基于xml创建对象

                  2)基于xml方式注入属性--DI(依赖注入)---属性注入的前提是对象已经创建好。

                        2-1)使用set方法实现DI

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

    public void testUser(){
        System.out.println(name);
    }
}
<?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.xsd">

    <!--创建user对象-->
    <bean id="user" class="com.cap.test.User">

        <!--使用property标签完成属相注入
          name:属性名
          value:属性值
        -->
        <property name="name" value="伊佐奈美須八尾" />
    </bean>
</beans>
public class UserTest {
    @Test
    public  void testAdd(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        User user = context.getBean("user", User.class);
        user.testUser();
    }
}

 

                      2-2)使用有参数的构造器实现DI

public class User {
    String name;
    int age;

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

    public void testUser(){
        System.out.println(name);
        System.out.println(age);
    }
}
<?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.xsd">

    <!--创建user对象-->
    <bean id="user" class="com.cap.test.User">

        <!--使用constructor-arg标签完成属相注入
          name:属性名
          value:属性值
          或者
          index:参数顺序
          value:属性值
        -->
        <constructor-arg name="name" value="伊佐奈美須八尾"/>
        <constructor-arg name="age" value="15"/>
        <constructor-arg index="0" value="伊佐奈美須八尾"/>
        <constructor-arg index="1" value="16"/>
    </bean>
</beans>

              2-3)基于xml属性注入特殊值

                    あ:null值注入

    <!--创建Conact对象-->
    <bean id="conact" class="com.cap.test.Conact">
        <property name="age" value="15"/>
        <!--在property标签中使用<null/>标签代表null值-->
        <property name="address">
            <null/>
        </property>
    </bean>

                い:注入包含特殊字符的值 

    <!--创建Conact对象-->
    <bean id="conact" class="com.cap.test.Conact">
        <property name="age" value="15"/>
        <property name="address">
            <!--在value标签中使用CDATA注入特殊字符
             ![CDATA[value]]
            -->
            <value><![CDATA[<<南京>>]]></value>
        </property>
    </bean>

            う:set方法实现外部bean注入。

import com.cap.test.Dao.UserDao;

public class UserService {

    private UserDao userDao;

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

    public void  service(){
        System.out.println("service************");
        userDao.update();
    }
}
public interface UserDao {
    public void update();
}

 

public class UserDaoImpl implements UserDao {

    @Override
    public void update() {
        System.out.println("UserDao*******");
    }
}
<?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.xsd">

    <!--创建UserService和UserDaoImpl对象-->
    <bean name="userService" class="com.cap.test.Service.UserService">

        <!--注入外部bean,通过set方法注入
           ref:需要导入外部bean
        -->
        <property name="userDao" ref="userDao"/>
    </bean>
    <bean name="userDao" class="com.cap.test.Dao.UserDaoImpl"></bean>
</beans>

     え:set方法实现内部bean注入。

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

    <!--创建UserService和UserDaoImpl对象-->
    <bean name="userService" class="com.cap.test.Service.UserService">

        <!--注入内部bean,通过set方法注入
           内部bean  VS  外部bean
           内部bean :生成bean的过程另外一个bean内部
           外部bean :生成bean的过程另外一个bean外部
        -->
        <property name="userDao">
            <bean name="userDao" class="com.cap.test.Dao.UserDaoImpl"></bean>
        </property>
    </bean>
</beans>

                お:实现级联赋值 

                さ:集合类型属性注入(单个值,对象注入) 

                そ:IOC中的bean管理(bean的类型)

        た:IOC中的bean管理(bean的作用域)

     ち:IOC中的bean管理(bean生命周期)

     つ: IOC中的bean管理(自动装配)

     て:IOC中bean的管理(外部属性文件的引入)

        3.2.2 基于注解(需要引入spring-aop-5.3.5.jar包)

    あ:Spring针对bean的创建提供的注解

                       ① @Componet

               ② @Service

               ③ @Controller

               ④ @Repository

    い:基于注解的方式创建bean实例准备

                       ①:引入依赖 spring-aop-5.3.5.jarS 

                       ②:开启组件扫描

                       ③:添加注解测试

    う:基于注解方式实现属性注入

    え:完全注解开发,去除xml文件

4 AOP

    4.1 AOP的基本概念

    4.2  AOP底层原理

            4.2.1 JDK动态代理的实现

    4.3 专业术语

            4.3.1 连接点:类里面哪些方法可以被增强,这些方法就叫连接点

            4.3.2 切入点:实际上被增强的方法,叫做切点

            4.3.3 通知(增强):实际上添加的逻辑部分叫通知,增加了什么内容的部分

                              あ:前置通知

                              い:后置通知

                              う:环绕通知

                             え:异常通知

                             お:最终通知

            4.3.4 切面:是一个动作,把通知加入到切入点的过程。

      4.4 AOP操作(准备)

5.JDBCTemplate

    5.1 spring对JDBC进行了封装,简化对数据库的操作。

    5.2 准备工作需要的jar包 

com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.2.jar
druid-1.1.9.jar
mysql-connector-java-5.1.7-bin.jar

spring-aop-5.3.5.jar
spring-aspects-5.3.5.jar
spring-beans-5.3.5.jar
spring-context-5.3.5.jar
spring-core-5.3.5.jar
spring-expression-5.3.5.jar
spring-jdbc-5.3.5.jar
spring-orm-5.3.5.jar
spring-tx-5.3.5.jar

     

 

 

 

              

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值