• Spring框架


    Spring框架概述

    1. Spring是轻量级的开源的JavaEE框架,是针对bean的生命周期进行管理的轻量级容器。
    2. 可以解决企业应用开发的复杂性,实现敏捷开发。
    3. 集成各类型的工具,通过核心的Bean factory实现了底层的类的实例化和生命周期的管理。
    4. Spring有两个核心部分:IOC和Aop
      (1)IOC控制反转,把创建对象过程交给Spring进行管理
      (2)Aop面向切面,不修改源代码的情况下进行功能增强
    5. Spring特点
      (1)方便解耦,简化开发
      (2)Aop编程支持
      (3)方便程序的测试
      (4)方便和其它框架进行整合
      (5)方便进行事务操作
      (6)降低API开发难度

    在这里插入图片描述

    入门案例

    • 下载Spring5
    • 创建普通java工程
    • 导入Spring5相关jar包:Beans,Core,Context,Expression
      在这里插入图片描述
    • 创建普通类
    package com.xxxx.lln;
    
    /**
     * 注释内容
     *
     * @author : li.linnan
     * @create : 2022/8/15
     */
    public class User {
    
        public void sout(){
            System.out.println("输出用户信息...");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    配置文件

    
    <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 id="user" class="com.xxxx.lln.User">bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    package com.xxxx.lln.testdemo;
    
    import com.xxxx.lln.User;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import org.junit.Test;
    
    public class Test1 {
    
        @Test
        public void test(){
            //加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
            ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            //获取配置创建的对象
            //User user = context.getBean("user", User.class);
            User user = (User) context.getBean("user");
            System.out.println(user);
            user.sout();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    IOC容器

    概念

    • 控制反转:把对象创建和对象之间的调用过程交给spring进行管理
    • 使用IOC的目的是为了让耦合度降低
    • 上面的案例就是IOC的实现

    底层原理

    xml解析、工厂模式、反射

    在这里插入图片描述
    工厂模式----降低耦合度

    IOC过程
    IOC过程

    IOC (BeanFactory接口)

    1. IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
    2. Spring 提供IOC容器实现的两种方式:(两个接口)

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

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

    (2)ApplicationContext: BeanFactory:接口的自己接口,提供更多更强大的功能,一般由开发人员进行使用。

    • 加载配置文件时就会把配置文件对象时进行创建。
    1. ApplicationContext接口有实现类
      FileSystemXmlApplicationContext();写的是xml文件的系统路径
      ClassPathXmlApplicationContext();写的是xml文件的类路径

    IOC操作Bean管理

    Bean管理指的是两个操作:1、Spring创建对象;2、Spring注入属性

    Bean管理操作有两种方式:
    1、基于xml配置文件方式实现
    2、基于注解方式实现

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

    • 基于xml方式注入属性
        
        <bean id="user" class="com.xxxx.lln.User">bean>
    
    • 1
    • 2

    (1)在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
    (2)在 bean 标签有很多属性,介绍常用的属性 id 属性:唯一标识 class 属性:类全路径(包类路径)
    (3)创建对象时候,默认也是执行无参数构造方法完成对象创建

    基于xml方式注入属性

    使用set注入属性

    (1)创建类、定义属性和对应的set方法
    (2)在spring配置文件中配置对象创建,配置对象注入

        <!--set方法注入属性-->
        <bean id="book" class="com.xxxx.lln.Book">
            <property name="name" value="圆圈正义"></property>
            <property name="author" value="罗翔"></property>
        </bean>    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    测试

        @Test
        public void test2(){
            //加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
            ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            //获取配置创建的对象
            Book book = context.getBean("book", Book.class);
            System.out.println(book);
            System.out.println(book.getName()+"---"+book.getAuthor());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    使用有参构造进行注入

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

        //有参构造
        public Book(String name, String author) {
            this.name = name;
            this.author = author;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

        <!--有参构造-->
        <bean id="book" class="com.xxxx.lln.Book">
            <constructor-arg name="name" value="一千零一夜"></constructor-arg>
            <constructor-arg name="author" value="张良"></constructor-arg>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    p名称空间注入

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

    (1)添加p名称空间在配置文件中

    xmlns:p="http://www.springframework.org/schema/p"
    
    • 1

    (2)进行属性注入,在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"
           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">
    
    	<!--使用p名称空间注入-->
        <bean id="book" class="com.xxxx.lln.Book" p:name="三国演义" p:author="罗贯中"></bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    xml注入其他类型属性

    1、注入属性-字面值

    (1)null值

        <bean id="book" class="com.xxxx.lln.Book">
            <property name="name">
                <null/>
            </property>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (2)属性值包含特殊符号

        <!-- 属性值包含特殊符号
        1<  > 进行转义 &lt; &gt;     
        2 把带特殊符号内容写到 CDATA --> 
        <bean id="book" class="com.xxxx.lln.Book">
    	<!--<property name="name" value="<南京>"></property>-->
            <property name="name">
                <value><![CDATA[<<南京>>]]></value>
            </property>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    2、注入属性-外部bean
    - 创建两个类service类和dao类
    - 在service调用dao里面的方法
    - 在spring配置文件中进行配置
    
    • 1
    • 2
    • 3
    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();     
    	} 
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
        <!-- 1 service 和 dao 对象创建 --> 
        <bean id="userService" class="com.atguigu.spring5.service.UserService">     
            <!-- 注入 userDao 对象         
            name 属性:类里面属性名称         
            ref 属性:创建 userDao 对象 bean 标签 id 值     
            -->     
            <property name="userDao" ref="userDaoImpl"></property> 
        </bean> 
        
        <bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    3、注入属性-内部 bean

    (1)一对多关系:部门和员工 一个部门有多个员工,一个员工属于一个部门 部门是一,员工是多
    (2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

    	// 部门类 
    	public class Dept {     
    		private String dname;     
    		public void setDname(String dname) {         
    			this.dname = dname;     
    		} 
    	} 
    	// 员工类 
    	public class Emp {     
    		private String ename;     
    		private String gender;     
    		// 员工属于某一个部门,使用对象形式表示     
    		private Dept dept;     
    		public void setDept(Dept dept) {         
    			this.dept = dept;     
    		}     
    		public void setEname(String ename) {         
    			this.ename = ename;     
    		}     
    		public void setGender(String gender) {         
    			this.gender = gender; 
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

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

        <!-- 内部 bean --> 
        <bean id="emp" class="com.atguigu.spring5.bean.Emp">     
            <!-- 设置两个普通属性 -->     
            <property name="ename" value="lucy"></property>     
            <property name="gender" value="女"></property>     
            <!-- 设置对象类型属性 -->     
            <property name="dept">         
                <bean id="dept" class="com.atguigu.spring5.bean.Dept">             
                    <property name="dname" value="安保部"></property>         
                </bean>     
            </property> 
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    4、注入属性-级联赋值

    (1)第一种写法

        <!-- 级联赋值 --> 
        <bean id="emp" class="com.atguigu.spring5.bean.Emp">     
            <!-- 设置两个普通属性 -->     
            <property name="ename" value="lucy"></property>     
            <property name="gender" value="女"></property>     
            <!-- 级联赋值 -->     
            <property name="dept" ref="dept"></property> 
        </bean> 
        
        <bean id="dept" class="com.atguigu.spring5.bean.Dept">     
            <property name="dname" value="财务部"></property> 
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (2)第二种写法

    在这里插入图片描述

        <!-- 级联赋值 -->
        <bean id="emp" class="com.atguigu.spring5.bean.Emp">
            <!-- 设置两个普通属性 -->
            <property name="ename" value="lucy"></property>
            <property name="gender" value="女"></property>
            <!-- 级联赋值 -->
            <property name="dept" ref="dept"></property>
            <property name="dept.dname" value="技术部"></property>
        </bean>
    
        <bean id="dept" class="com.atguigu.spring5.bean.Dept">
            <property name="dname" value="财务部"></property>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    5、注入属性-集合属性

    1、注入数组类型属性

    2、注入List集合类型属性

    3、注入Map集合类型属性
    (1)创建类、定义数组、list、map、set类型属性,生成对应set方法

    package com.xxxx.lln;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * 注释内容
     *
     * @author : li.linnan
     * @create : 2022/8/16
     */
    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;
        }
    
        @Override
        public String toString() {
            return "Stu{" +
                    "courses=" + Arrays.toString(courses) +
                    ", list=" + list +
                    ", maps=" + maps +
                    ", sets=" + sets +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

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

        
        <bean id="stu" class="com.xxxx.lln.Stu">
            
            <property name="courses">
                <array>
                    <value>java 课程value>
                    <value>数据库课程value>
                array>
            property>
            
            <property name="list">
                <list>
                    <value>张三value>
                    <value>小三value>
                list>
            property>
            
            <property name="maps">
                <map>
                    <entry key="JAVA" value="java">entry>
                    <entry key="PHP" value="php">entry>
                map>
            property>
            
            <property name="sets">
                <set>
                    <value>MySQLvalue>
                    <value>Redisvalue>
                set>
            property>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    (3)测试

        @Test
        public void test3(){
            //加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
            ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            //获取配置创建的对象
            Stu stu = context.getBean("stu", Stu.class);
            System.out.println(stu);
            stu.toString();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

        
        <bean id="course1" class="com.xxxx.lln.Stu">
            <property name="cname" value="Spring5 框架">property>
        bean>
        <bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
            <property name="cname" value="MyBatis 框架">property> 
        bean> 
         
        <property name="courseList">     
            <list>         
                <ref bean="course1">ref>         
                <ref bean="course2">ref>     
            list> 
        property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    5、把集合注入部分取出来

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

    
    <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">
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

        
        <util:list id="bookList">
            <value>易筋经value>     
            <value>九阴真经value>     
            <value>九阳神功value> 
        util:list> 
        
         
        <bean id="book" class="com.atguigu.spring5.collectiontype.Book">     
            <property name="list" ref="bookList">property> 
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    FactoryBean 工厂Bean

    1、Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)

    2、普通 bean:在配置文件中定义 bean 类型就是返回类型

    3、工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样
    第一步 创建类,让这个类作为工厂 bean,实现接口 FactoryBean
    第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型

    package com.xxxx.lln;
    
    import org.springframework.beans.factory.FactoryBean;
    
    /**
     * 注释内容
     *
     * @author : li.linnan
     * @create : 2022/8/16
     */
    public class MyBean implements FactoryBean<Book> {
    
        //定义返回Bean
        @Override
        public Book getObject() throws Exception {
            Book book = new Book();
            book.setName("abc");
            return book;
        }
    
        @Override
        public Class<?> getObjectType() {
            return null;
        }
    
        @Override
        public boolean isSingleton() {
            return FactoryBean.super.isSingleton();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    <bean id="myBean" class="com.xxxx.lln.MyBean">bean>
    
    • 1
        @Test
        public void test3(){
            //加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
            ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            //获取配置创建的对象
            Book book = context.getBean("myBean", Book.class);
            System.out.println(book);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Bean作用域

    1、在 Spring 里面,设置创建 bean 实例是单实例还是多实例
    2、在 Spring 里面,默认情况下,bean是单实例对象
    在这里插入图片描述

    3、如何设置单实例还是多实例
    (1)在 spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例
    (2)scope 属性值 第一个值 默认值,singleton,表示是单实例对象 第二个值 prototype,表示是多实例对象

    在这里插入图片描述
    (3)singleton 和 prototype 区别
    第一 singleton 单实例,prototype 多实例
    第二 设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象
    设置 scope 值是 prototype 时候,不是在加载 spring 配置文件时候创建 对象,在调用 getBean 方法时候创建多实例对象

    Bean的生命周期

    1、生命周期
    对象从创建到销毁的过程
    2、bean生命周期
    (1)通过构造器创建bean实例
    (2)为bean的属性设置值和对其他bean引用(调用set方法)
    (3)调用bean的初始化方法(需要进行配置初始化的方法)
    (4)bean可以使用了(对象获取到了)
    (5)当容器关闭时,调用bean的销毁方法(需要进行配置销毁的方法)

    3、演示生命周期

    package com.xxxx.lln;
    
    public class Orders {
    
        // 无参数构造
        public Orders() {
            System. out .println("第一步 执行无参数构造创建 bean 实例");
        }
    
        private String oname;
    
        public void setOname(String oname) {
            this.oname = oname;
            System. out .println("第二步 调用 set 方法设置属性值");
        }
    
        // 创建执行的初始化的方法
        public void initMethod() {
            System. out .println("第三步 执行初始化的方法");
        }
    
        // 创建执行的销毁的方法
        public void destroyMethod() {
            System. out .println("第五步 执行销毁的方法");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
        <bean id="orders" class="com.xxxx.lln.Orders" init-method="initMethod" destroy-method="destroyMethod">
            <property name="oname" value="手机">property>
        bean>
    
    • 1
    • 2
    • 3
        @Test
        public void test4(){
            //加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
            //ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            //获取配置创建的对象
            Orders orders = context.getBean("orders", Orders.class);
            System.out.println("第四步 获取创建bean实例对象");
            System.out.println(orders);
            //手动让bean实例销毁
            context.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    4、、bean 的后置处理器,bean 生命周期有七步
    (1)通过构造器创建 bean 实例(无参数构造)
    (2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
    (3)把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization
    (4)调用 bean 的初始化的方法(需要进行配置初始化的方法)
    (5)把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization
    (6)bean 可以使用了(对象获取到了)
    (7)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

    package com.xxxx.lln;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    public class MyBeanPost implements BeanPostProcessor {
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System. out .println("在初始化之前执行的方法");
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System. out .println("在初始化之后执行的方法");
            return bean;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
        
        <bean id="myBeanPost" class="com.xxxx.lln.MyBeanPost">bean>
    
    • 1
    • 2

    在这里插入图片描述

    xml自动装配

    1、根据指定装配规则(属性名称或者属性类型),spring自动将匹配的属性值进行注入

    2、演示自动装配过程

    • 根据属性名称自动注入
    package com.xxxx.lln.autowire;
    
    public class Dept {
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    package com.xxxx.lln.autowire;
    
    public class Emp {
    
        private Dept dept;
    
        public void setDept(Dept dept) {
            this.dept = dept;
        }
    
        @Override
        public String toString() {
            return "Emp{" +
                    "dept=" + dept +
                    '}';
        }
    
        public void test(){
            System.out.println(dept);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
        <bean id="emp" class="com.xxxx.lln.autowire.Emp" autowire="byName">
    
        bean>
        <bean id="dept" class="com.xxxx.lln.autowire.Dept">bean>
    
    • 1
    • 2
    • 3
    • 4
        @Test
        public void test5(){
            //加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
            //ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            //获取配置创建的对象
            Emp emp = context.getBean("emp", Emp.class);
            System.out.println(emp);
            emp.test();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 根据属性类型自动注入
        <bean id="emp" class="com.xxxx.lln.autowire.Emp" autowire="byType">
    
        bean>
        <bean id="dept" class="com.xxxx.lln.autowire.Dept">bean>
    
    • 1
    • 2
    • 3
    • 4

    外部属性文件

    1、直接配置数据库信息
    (1)配置德鲁伊连接池

    (2)引入德鲁伊连接池jar包

    在这里插入图片描述

        
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver">property>
            <property name="url" value="jdbc:mysql://localhost:3306/userDb">property>
            <property name="username" value="root">property>
            <property name="password" value="root">property>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2、引入外部属性文件配置数据库连接池
    (1)创建外部属性文件,properties 格式文件,写数据库信息

    prop.driverClass=com.mysql.jdbc.Driver
    prop.url=jdbc:mysql://localhost:3306/userDb
    prop.userName=root
    prop.password=root
    
    • 1
    • 2
    • 3
    • 4

    (2)把外部 properties 属性文件引入到 spring 配置文件中

    引入 context 名称空间

    <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"
           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/util http://www.springframework.org/schema/util/spring-util.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在spring配置文件中使用标签引入外部属性文件

         
        <context:property-placeholder location="classpath:jdbc.properties"/> 
         
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">     
            <property name="driverClassName" value="${prop.driverClass}">property>     
            <property name="url" value="${prop.url}">property>     
            <property name="username" value="${prop.userName}">property>     
            <property name="password" value="${prop.password}">property> 
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    基于注解方式

    1、什么是注解
    (1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)
    (2)使用注解,注解作用在类上面,方法上面,属性上面
    (3)使用注解目的:简化 xml 配置

    2、Spring 针对 Bean 管理中创建对象提供注解
    (1)@Component
    (2)@Service
    (3)@Controller
    (4)@Repository
    上面四个注解功能是一样的,都可以用来创建 bean 实例

    3、基于注解方式实现对象创建

    第一步 引入依赖

    在这里插入图片描述

    第二步 开启组件扫描

        <!--开启组件扫描:
            1.如果扫描多个包,多个包用逗号隔开
            2.扫描包上层目录-->
        <context:component-scan base-package="com.xxxx.lln"></context:component-scan>
    
    • 1
    • 2
    • 3
    • 4

    第三步 创建类,在类上面添加创建对象注解
    // 在注解里面 value 属性值可以省略不写,
    // 默认值是类名称,首字母小写
    //UserService – userService

    package com.xxxx.lln.service;
    
    import org.springframework.stereotype.Service;
    
    @Service(value = "userService")
    public class UserService {
    
        public void add(){
            System.out.println("service add ......");
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    测试

        @Test
        public void testService(){
            ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            UserService userService = context.getBean("userService",UserService.class);
            System.out.println(userService);
            userService.add();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4、开启组件扫描细节配置

  • 相关阅读:
    【热门前端【vue框架】】——vue框架和node.js的下载和安装保姆式教程
    MySql 数据库【事务】
    面试面经|Java面试RabbitMQ面试题
    【前后端交互与HTTP协议】(HTTP协议、本地存储、Ajax&Fetch 与跨域请求)
    【Vue 开发实战】基础篇 # 3:Vue组件的核心概念:事件
    在九天服务器平台上使用自己上传的数据集文件
    如何在Spring Boot中记录用户系统操作流程?
    http协议各个版本的详细介绍
    @Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法
    创维E900V22E_卡刷固件及升级说明
  • 原文地址:https://blog.csdn.net/lln1540295459/article/details/126340711