• 1.spring:IOC-ssg


    基于xml方式创建对象

    IOC:控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理

    Spring 提供 IOC 容器实现两种方式(两个接口):
    BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用,加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象.
    ApplicationContext(推荐): BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人员进行使用,加载配置文件时候就会把在配置文件对象进行创建

    IOC操作中的Bean管理(两个操作):
    1.spring创建对象
    2.spring创建属性

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

    java代码:

    getBean(“User”,User.class)中,前一个形参(User)是为了拿到xml配置文件中创建的对象,后一个形参(User.class)是为了通过反射拿到User中的方法和属性,这样就拿到了以一个类的实例和其方法和属性

    此处xml配置文件是通过无参构造来创建对象的

    public class User {
        public User(){
            System.out.println("bbb");
        }
        public void add(){
            System.out.println("aaa");
        }
    }
    
    class Demo{
        public static void main(String[] args){
        	//这里的路径是xml文件相对于src的相对路径.
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring1_xml/Test_spring创建对象1/bean1.xml");
            //通过getbean()获取xml配置文件创建的对象,这里的User是xml中的id值,User.class所创建的实例对象的类对象
            User user = context.getBean("User",User.class);
            user.add();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    xml代码:

    id 属性:唯一标识(getBeam()通过此值获取创建的对象)
    class 属性:类全路径(在src下的相对路径)

    <!--1 配置User对象创建-->
        <bean id="User" class="Spring1_xml.Test_spring创建对象1.User"></bean>
    
    • 1
    • 2

    基于 xml的DI(依赖注入,即注入属性(给属性赋值))

    java代码:

    public class Book {
        public String bookName;
        public String id;
    
    	由xml 2 使用
        public Book(String bookName,int id){
            this.bookName = bookName;
            this.id = id;
        }
    
        由xml 1 使用
        public Book(){}
    
    	由xml 1 使用
        public void setBookName(String bookName) {
            this.bookName = bookName;
        }
    	由xml 1 使用
        public void setId(String id) {
            this.id = id;
        }
        public void show(){
            System.out.println(bookName+":"+id);
        }
    }
    class Demo{
        public static void main(String[] args){
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring1_xml/注入属性2/bean_book.xml");
            Book book = context.getBean("Book",Book.class);
            book.show();
        }
    }
    
    • 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

    第一种注入方式:使用 set 方法进行注入.即:要在java代码中创建要注入(赋值)的属性的set()方法
    在xml配置文件通过java代码中的set方法来注入属性.

    xml 1 代码:

    使用 property 标签完成属性注入
    name:类里面属性名称
    value:向属性注入的值

        <bean id="Book" class="Spring1_xml.注入属性2.Book">
            <property name="bookName" value="计算机"></property>
            <property name="id" value="20"></property>
        </bean>
    
    • 1
    • 2
    • 3
    • 4

    第二种注入方式:创建类,定义属性,创建属性对应有参数构造方法
    xml 2 代码:

    使用 constructor-arg 标签完成属性注入
    name:类里面属性名称
    value:向属性注入的值

        <bean id="Book" class="Spring1_xml.有参构造注入属性3.Book">
            <constructor-arg name="bookName" value="足球"></constructor-arg>
            <constructor-arg name="id" value="18"></constructor-arg>
        </bean>
    
    • 1
    • 2
    • 3
    • 4

    xml注入其他类型属性:
    注入null值:

    <property name="address">
    <null/>
    </property>
    
    • 1
    • 2
    • 3

    (2)属性值包含特殊符号
    属性值包含特殊符号:
    1 把<>进行转义 < >
    2 把带特殊符号内容写到 <![CDATA[ ]]>中:

    <property name="address">
    <value><![CDATA[<<abc>>]]></value>
    </property>
    
    • 1
    • 2
    • 3

    注入属性-外部bean:
    即:要想让一个对象调用另一个对象的属性或方法,并给另一个对象的属性赋值(级联赋值)

    如果a对象给b对象的属性赋值,且使用的是xml中的ref,则a对象必须提供 b 对象的get()和set()方法,b对象的实现类中必须有该属性的set()方法.
    java代码:

    class UserDAO {
        public String name;
        public void setName(String name) {
            this.name = name;
        }
        public void show() {
            System.out.print("aaa");
        }
    }
    
    class UserService{
        public UserDAO userDAO;
        
        public void setUserDAO(UserDAOI userDAO) {
            this.userDAO = userDAO;
        }
        public UserDAO getUserDAO() {
            return userDAO;
        }
        public void add(){
            userDAO.show();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    注入属性-外部bean:
    即:要想让一个对象调用另一个对象的属性或方法,并给另一个对象的属性赋值(级联赋值)

    xml代码:
    注入 userDao 对象
    name 属性:另一个bean标签下的id的值.
    ref 属性:创建 userDao 对象 bean 标签 id 值

        <bean id="UserDAO" class="Spring1_xml.注入属性_外部bean5.UserDAO"></bean>
        
        <!--    注入属性外部bean-->
        <bean id="UserService" class="Spring1_xml.注入属性_外部bean5.UserService">
            <property name="userDAO" ref="UserDAO"></property>
    <!--        这里需要提供userDAO的get()方法-->
            <property name="userDAO.name" value="呱呱"></property>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    注入属性-内部 bean 并赋值(级联赋值):
    xml:

        <bean id="UserService" class="Spring1_xml.注入属性_外部bean5.UserService">
            <property name="userDAO">
            
                <bean id="UserDAOImpl" class="Spring1_xml.注入属性_外部bean5.UserDAOImpl">
                    <property name="name" value="ccc"></property>
                </bean>
                
            </property>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    xml注入集合属性:
    java代码:

    public class Course {
        private String cname; //课程名称
        public void setCname(String cname) {
            this.cname = cname;
        }
    
        @Override
        public String toString() {
            return "Course{" +
                    "cname='" + cname + '\'' +
                    '}';
        }
    }
     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;
         //5 向集合中注入对象
         private List<Ball> ballList;
         public void setCourse(List<Course> couseList) {
             this.couseList= couseList;
         }
        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;
        }
        public void show(){
            System.out.println(Arrays.toString(courses));
            System.out.println(list);
            System.out.println(maps);
            System.out.println(sets);
            System.out.println(couseList);
        }
    }
    public class Demo {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring1_xml/注入集合属性_向集合中注入对象_提取集合为公共属性7/bean_注入集合属性.xml");
            Stu stu = context.getBean("Stu",Stu.class);
            stu.show();
        }
    }
    
    • 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
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    xml代码:

        <!--1 集合类型属性注入-->
        <bean id="stu" class="com.atguigu.spring5.collectiontype.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>
            <!--注入list集合类型,值是对象-->
            <property name="courseList">
                <list>
                    <ref bean="course1"></ref>
                    <ref bean="course2"></ref>
                </list>
            </property>
        </bean>
    
        <!--创建多个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>
    
    • 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

    把集合注入部分提取出来:

    <?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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    基于注解方式创建对象

    2、 Spring 针对 Bean 管理中创建对象提供注解
    (1) @Component
    (2) @Service
    (3) @Controller
    (4) @Repository

    • 上面四个注解功能是一样的,都可以用来创建 bean 实例

    在注解里面 value 属性值可以省略不写,
    默认值是类名称,首字母小写

    举例:
    java代码:

    @Component
    public class User {
        public void add(){
            System.out.println("add..");
        }
    }
    class Main{
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring_注解/Test1/bean_注解.xml");
            User user = context.getBean("user",User.class);
            user.add();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    检查该包下所有类:
    开启组件扫描:
    1 如果扫描多个包,多个包使用逗号隔开
    2 扫描包上层目录

     <context:component-scan base-package="Spring_注解.Test1"></context:component-scan>
    
    • 1

    use-default-filters=“false” 表示现在不使用默认 filter,自己配置 filter
    context:include-filter ,设置扫描哪些内容
    只检查Controller注解的类

        <context:component-scan base-package="Spring_注解" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>>
    
    • 1
    • 2
    • 3

    不检查Controller注解的类

        <context:component-scan base-package="Spring_注解">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
    • 1
    • 2
    • 3

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

    @Autowired:根据属性类型进行自动装配
    第一步 把 service 和 dao 对象创建,在 service 和 dao 类添加创建对象注解
    第二步 在 service 注入 dao 对象,在 service 类添加 dao 类型属性,在属性上面使用注解

    @Autowired
    private UserDao userDao;
    
    • 1
    • 2

    @Qualifier:根据名称进行注入
    这个@Qualifier 注解的使用,和上面@Autowired 一起使用

    //添加注入属性注解
    @Autowired //根据类型进行注入
    @Qualifier(value = "userDaoImpl1") //根据名称进行注入
    private UserDao userDao;
    
    • 1
    • 2
    • 3
    • 4

    @Resource:可以根据类型注入,可以根据名称注入

    //@Resource //根据类型进行注入
    @Resource(name = "userDaoImpl1") //根据名称进行注入
    private UserDao userDao;
    
    • 1
    • 2
    • 3

    @Value:注入普通类型属性

    @Value(value = "abc")
    private String name;
    
    • 1
    • 2

    创建配置类,替代 xml 配置文件:

    @Configuration //作为配置类,替代 xml 配置文件
    @ComponentScan(basePackages = {"com.atguigu"})
    public class SpringConfig {
    }
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    开学要考IB经济怎么办?
    CentOS7.9安装elasticsearch-8.3.1和window 10安装elasticsearch-8.3.1
    《HelloGitHub》第 77 期
    本地MQTT协议消息服务远程连接教程介绍
    软考-系统架构师-计算机与网络基础知识-数据库系统基础知识
    酒店售货机系统架构分析贴牌定制开发oem搭建源码
    Spring源码解析——事务的回滚和提交
    asyncawait和promise的区别
    视频剪辑方法:一键批量调整色调的高效技巧
    Python Opencv实践 - FAST关键点检测
  • 原文地址:https://blog.csdn.net/m0_56182317/article/details/125321899