01-Spring5框架之IOC(xml)注入属性操作

CSDN话题挑战赛第2期
参赛话题:学习笔记

学习之路,长路漫漫,写学习笔记的过程就是把知识讲给自己听的过程。这个过程中,我们去记录思考的过程,便于日后复习,梳理自己的思路。学习之乐,独乐乐,不如众乐乐,把知识讲给更多的人听,何乐而不为呢?

目录

1、Spring框架概述

1.1、Spirng是轻量级的开源的JavaEE框架

1.2、Spring可以解决企业应用开发的复杂性

1.3、Spirng有两个核心部分:IOC和AOP

1.4、Spring特点

2、Spring入门案例

3、IOC

3.1、IOC(概念和原理)

3.1.1、什么是IOC

3.1.2、IOC底层原理

3.2、IOC操作Bean管理(概念)

3.2.1、什么是Bean管理

3.2.2、Bean管理操作有两种方式

3.3、IOC操作Bean管理(基于XML方式)

3.3.1、基于xml方式创建对象

3.3.2、基于xml方式注入属性

3.3.3、第一种注入方式:使用set方式进行注入

3.3.4、第二种注入方式:使用有参数构造进行注入

3.3.5、p名称空间注入(了解)

4、IOC操作Bean管理(xml注入其他类型属性)

4.1、字面

4.2、注入属性--外部bean

4.3、注入属性 -- 内部bean

4.4、注入属性--级联赋值

4.5、注入属性--集合属性

4.5.1、注入数组类型属性

4.5.2、注入List集合类型属性

4.5.3、注入Map集合类型属性

4.5.4、在集合里面设置对象类型值

4.5.5、把集合注入部分提取出来


1、Spring框架概述

1.1、Spirng是轻量级的开源的JavaEE框架

1.2、Spring可以解决企业应用开发的复杂性

1.3、Spirng有两个核心部分:IOC和AOP

           (1) IOC:控制反转,把创建对象过程交给Spring进行管理

           (2) AOP:面向切面,不修改源代码进行功能增强

1.4、Spring特点

  1. 方便解耦,简化开发
  2. AOP编程支持
  3. 方便程序测试
  4. 方便和其他框架进行整合
  5. 方便进行事务操作
  6. 降低API开发难度

2、Spring入门案例

                

  • 3、在maven里面创建简单Java模块

                

 

  • 4、导入spring相关的包

  • 5、创建普通类,在这个类创建普通方法
/**
 * @User: 老潘
 * @date 2022年09月28日10:44
 */
public class Student {
    public void add(){
        System.out.println("add......bean.xml");
    }
}
  • 6、创建Spring配置文件,在配置文件配置创建的对象

        注意bean.xml文件的位置:

                

<?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">
    <!--配置Student对象创建-->
    <bean id="student" class="com.pan.gh.Student"></bean>
</beans>
  • 7、进行测试代码编写
/**
 * @User: 老潘
 * @date 2022年09月28日10:50
 */
public class testSpring {
    @Test
    public void testAdd(){
        //1、加载spring配置文件
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean.xml");
        //2、获取配置创建的对象
        Student student=context.getBean("student",Student.class);
        System.out.println(student);
        student.add();
    }
}

运行结果:

com.pan.gh.Student@3d921e20
add......bean.xml

3、IOC

3.1、IOC(概念和原理)

3.1.1、什么是IOC

  • 控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理
  • 使用IOC目的,为了耦合度降低
  • 做入门案例就是IOC的具体实现

3.1.2、IOC底层原理

        1)xml解析、工厂模式、反射

        2)讲解IOC底层原理

       3) IOC(BeanFactory 接口)

                1、IOC 思想基于 IOC 容器完成,IOC 容器底层就是对象工厂

                2、Spring 提供 IOC 容器实现两种方式:(两个接口)

                        (1)BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用

                 * 加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象

                        (2)ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人 员进行使用 * 加载配置文件时候就会把在配置文件对象进行创建                 3、 ApplicationContext 接口有实现类

3.2、IOC操作Bean管理(概念)

3.2.1、什么是Bean管理

        1)Bean管理指的是两个操作

        2)Spring创建对象

        3)Spring注入属性

3.2.2、Bean管理操作有两种方式

        1)基于XML配置文件方式实现

        2)基于注解方式实现

3.3、IOC操作Bean管理(基于XML方式)

3.3.1、基于xml方式创建对象

        <!--配置Student对象创建-->

        <bean id="student" class="com.pan.gh.Student"></bean>

        1)在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建

        2)在bean标签有很多属性,介绍常用的属性

                * id属性:唯一标识(一般为类的小写)

                * class属性:类路径名称(包含路径)

        3)创建对象时候,默认也是执行无参构造方法完成对象创建

3.3.2、基于xml方式注入属性

        1)DI:依赖注入,就是注入属性

3.3.3、第一种注入方式:使用set方式进行注入

        1)创建类,定义属性和对应的set方法

/**
 * @User: 老潘
 * @date 2022年09月28日19:36
 * 演示使用set方法进行注入属性
 */
public class Book {
    // 创建属性
    private String bname;
    private String bauthor;
    // 创建属性对应的set方法
    public void setBname(String bname){
        this.bname=bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    public void testB(){
        System.out.println(bname+"::"+bauthor);
    }
}

     2)在spring配置文件中配置对象,配置属性注入

<?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">
    <!--2、set方法注入属性-->
    <bean id="book" class="com.pan.gh.Book">
        <!--使用property完成属性注入
            name:类里面属性名称
            value:向属性注入的值
        -->
        <property name="bname" value="葵花宝典"></property>
        <property name="bauthor" value="隔壁老王"></property>
    </bean>
</beans>

     3)测试结果

    

3.3.4、第二种注入方式:使用有参数构造进行注入

        1)创建类,定义属性:创建属性对应有参构造方法

/**
 * @User: 老潘
 * @date 2022年09月28日19:47
 * 使用有参构造注入
 */
public class Order {
    // 属性
    private String oname;
    private String address;
    
    // 有参构造
    public Order(String oname, String address) {
        this.oname = oname;
        this.address = address;
    }

    public void  testO(){
        System.out.println(oname+"::"+address);
    }
}

        2)在spring配置文件中进行配置

<?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">
    <!--2、有参构造注入属性-->
    <bean id="order" class="com.pan.gh.Order">
        <constructor-arg name="oname" value="电脑"></constructor-arg>
        <constructor-arg name="address" value="中国"></constructor-arg>
    </bean>
</beans>

        3)测试结果

3.3.5、p名称空间注入(了解)

        1)使用p名称空间注入,可以简化基于xml配置方式

        第一步  添加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标签里面进行操作

    <!--2、set方法注入属性-->
    <bean id="book" class="com.pan.gh.Book" p:bname="九阳神功" p:bauthor="无名氏">

    </bean>

      第三步 结果测试

4、IOC操作Bean管理(xml注入其他类型属性)

4.1、字面量

  • null值
 <!--null值-->

<property name="address">

<null/>

</property>
  • 属性值包含特殊符号
<?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">

    <!--2、有参构造注入属性-->
    <bean id="school" class="com.pan.gh.School">

<!--            <property name="address">-->
<!--            <null/>-->
<!--            </property>-->
        <!--属性值包含特殊符号:
            1、把<>进行转义 &lt; &gt;
            2、把带特殊符号内容写到CDATA-->
        <property name="name" value="江西"></property>
            <property name="address">
            <value><![CDATA[<<南京>>]]></value>
            </property>

    </bean>
</beans>

4.2、注入属性--外部bean

        1)创建两个类UserService和UserDao类

        2)在UserService调用UserDao里面的方法

        3)在spring配置文件中进行配置

/**
 * @User: 老潘
 * @date 2022年09月28日20:28
 */
public class UserService {
    // 创建UserDao类型属性,生成set方法
    private UserDao userDao;

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

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

/**
 * @User: 老潘
 * @date 2022年09月28日20:29
 */
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">
    <!--1、service和dao对象创建-->
    <bean id="userService" class="com.pan.gh.outBean.UserService">
        <!--注入userDao对象
            name属性:类里面属性名称
            ref属性:创建userDao对象bean标签id值
        -->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="userDaoImpl" class="com.pan.gh.outBean.UserDaoImpl"></bean>
</beans>

运行结果

4.3、注入属性 -- 内部bean

1)一对多关系:班级和学生

        一个班级有多个学生,一个学生属于一个班级 班级是一 ,学生是多

2) 在实体类之间表示一对多关系,学生表示所属班级,使用对象类型属性进行表示

3) 在spring配置文件中进行配置

/**                                        
 * @User: 老潘                               
 * @date 2022年09月28日20:42                  
 */                                        
public class Student {                     
    private String sname;                  
                                           
    public void setSname(String sname) {   
        this.sname = sname;                
    }                                      
                                           
    public String getSname() {             
        return sname;                      
    }                                      
}                                          
                                           
/**
 * @User: 老潘
 * @date 2022年09月28日20:42
 */
public class ClassRoom {
    private String cname;
    private String gender;
    // 学生属于某一个班级,使用对象形式表示
    private Student student;

    public void setCname(String cname) {
        this.cname = cname;
    }

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

    public void setStudent(Student student) {
        this.student = student;
    }

    public void testRM(){
        System.out.println(cname+"::几年级:"+gender+"::学生名字为:"+student.getSname());
    }
}
<?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">
    <!--内部bean-->
    <bean id="classRoom" class="com.pan.gh.inBean.ClassRoom">
        <!--设置两个普通属性-->
        <property name="cname" value="6班"></property>
        <property name="gender" value="高一"></property>
        <!--设置对象类型属性-->
        <property name="student">
            <bean id="student" class="com.pan.gh.inBean.Student">
                <property name="sname" value="苏幕"></property>
            </bean>
        </property>
    </bean>
</beans>

测试结果

4.4、注入属性--级联赋值

<?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">
    <!--内部bean-->
    <bean id="classRoom" class="com.pan.gh.inBean.ClassRoom">
        <!--设置两个普通属性-->
        <property name="cname" value="6班"></property>
        <property name="gender" value="高一"></property>
        <!--设置对象类型属性-->
        <property name="student" ref="student">
        </property>
    </bean>
    <bean id="student" class="com.pan.gh.inBean.Student">
        <property name="sname" value="苏幕"></property>
    </bean>
</beans>

4.5、注入属性--集合属性

4.5.1、注入数组类型属性

4.5.2、注入List集合类型属性

4.5.3、注入Map集合类型属性

  • 创建类,定义数组、list、map、set类型属性,生成对应set方法
public class Stu {
    //1 数组类型属性
    private String[] courses;
    //2 list 集合类型属性
    private List<String> list;
    //3 map 集合类型属性
    private Map<String,String> maps;
    //4 set 集合类型属性
    private Set<String> sets;
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
    public void setCourses(String[] courses) {
        this.courses = courses;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
}
  • 在spring配置文件进行配置

<?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">
    <!--1 集合类型属性注入-->
    <bean id="stu" class="com.pan.gh.Stu">
    <!--数组类型属性注入-->
    <property name="courses">
        <array>
            <value>java 课程</value>
            <value>数据库课程</value>
        </array>
    </property>
    <!--list 类型属性注入-->
    <property name="list">
        <list>
            <value>张三</value>
            <value>小三</value>
        </list>
    </property>
    <!--map 类型属性注入-->
    <property name="maps">
        <map>
            <entry key="JAVA" value="java"></entry>
            <entry key="PHP" value="php"></entry>
        </map>
    </property>
    <!--set 类型属性注入-->
    <property name="sets">
        <set>
            <value>MySQL</value>
            <value>Redis</value>
        </set>
    </property>
    </bean>
</beans>

4.5.4、在集合里面设置对象类型值

<!--创建多个 course 对象-->
<bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
 <property name="cname" value="Spring5 框架"></property>
</bean>
<bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
 <property name="cname" value="MyBatis 框架"></property>
</bean>
<!--注入 list 集合类型,值是对象-->
<property name="courseList">
 <list>
 <ref bean="course1"></ref>
 <ref bean="course2"></ref>
 </list>
</property>

4.5.5、把集合注入部分提取出来

(1)在 spring 配置文件中引入名称空间 util


<?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"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util.xsd">

(2)使用 util 标签完成 list 集合注入提取


<!--1 提取 list 集合类型属性注入-->
<util:list id="bookList">
 <value>易筋经</value>
 <value>九阴真经</value>
 <value>九阳神功</value>
</util:list>
<!--2 提取 list 集合类型属性注入使用-->
<bean id="book" class="com.atguigu.spring5.collectiontype.Book">
 <property name="list" ref="bookList"></property>
</bean>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值