Spring(04):依赖注入(DI)

本文详细介绍了Spring框架中的依赖注入(DI)概念,包括Set注入、P命名空间注入和C命名空间注入,并通过示例展示了如何在XML配置文件中进行设置。此外,还讲解了Bean的作用域,包括单例和原型模式,以及在Web开发中的其他作用域选项。
摘要由CSDN通过智能技术生成

Spring(04):依赖注入

概念(Dependency Injection)

  • 依赖注入(Dependency Injection,DI)
  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配

Set 注入 (重点)

被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is

代码

  1. 实体类

    public class Student {
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobbys;
        private Map<String,String> card;
        private Set<String> games;
        private String wife;
        private Properties into;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public String[] getBooks() {
            return books;
        }
    
        public void setBooks(String[] books) {
            this.books = books;
        }
    
        public List<String> getHobbys() {
            return hobbys;
        }
    
        public void setHobbys(List<String> hobbys) {
            this.hobbys = hobbys;
        }
    
        public Map<String, String> getCard() {
            return card;
        }
    
        public void setCard(Map<String, String> card) {
            this.card = card;
        }
    
        public Set<String> getGames() {
            return games;
        }
    
        public void setGames(Set<String> games) {
            this.games = games;
        }
    
        public String getWife() {
            return wife;
        }
    
        public void setWife(String wife) {
            this.wife = wife;
        }
    
        public Properties getInto() {
            return into;
        }
    
        public void setInto(Properties into) {
            this.into = into;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", address=" + address +
                    ", books=" + Arrays.toString(books) +
                    ", hobbys=" + hobbys +
                    ", card=" + card +
                    ", games=" + games +
                    ", wife='" + wife + '\'' +
                    ", into=" + into +
                    '}';
        }
    }
    
    public class Address {
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "Address{" +
                    "address='" + address + '\'' +
                    '}';
        }
    }
    
    
  2. 配置.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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="address" class="com.gener.pojo.Address">
            <property name="address" value="天津"/>
        </bean>
    
        <bean id="student" class="com.gener.pojo.Student">
            <!--普通值注入,value-->
            <property name="name" value="Gener"/>
    
            <!--Bean注入,ref-->
            <property name="address" ref="address"/>
    
            <!--数组-->
            <property name="books">
                <array>
                    <value>斗破苍穹</value>
                    <value>武动乾坤</value>
                    <value>大主宰</value>
                </array>
            </property>
    
            <!--List-->
            <property name="hobbys">
                <list>
                    <value>健身</value>
                    <value>做饭</value>
                    <value>敲代码</value>
                </list>
            </property>
    
            <!--Map-->
            <property name="card">
                <map>
                    <entry key="身份证" value="1231312313123123213"/>
                    <entry key="银行卡" value="6767667667767766666"/>
                </map>
            </property>
    
            <!--Set-->
            <property name="games">
                <set>
                    <value>英雄联盟</value>
                    <value>王者荣耀</value>
                    <value>和平精英</value>
                </set>
            </property>
    
            <!--null-->
            <property name="wife">
                <null/>
            </property>
    
            <!--Properties-->
            <property name="into">
                <props>
                    <prop key="driver">xxxxx</prop>
                    <prop key="url">xxxxx</prop>
                    <prop key="username">xxxxx</prop>
                    <prop key="password">xxxxx</prop>
                </props>
            </property>
    
        </bean>
    
    </beans>
    
  3. 测试

    public class MyText {
        public static void main(String[] args) {
        	ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Student student = (Student) context.getBean("student");
    
            System.out.println(student.toString());
        }
    }
    

P命名空间注入

P(属性: properties)命名空间 , 属性依然要设置set方法

代码

  1. 创建实体类

    public class User {
        private String name;
        private int age;
    
    	//这里没有有参构造器
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
  2. 配置.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"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        
        <bean id="user" class="com.gener.pojo.User" p:age="23" p:name="gener"/>
    
    	<!--
    		P命名空间需要添加的约定文件:
    		xmlns:p="http://www.springframework.org/schema/p"
    	-->
        
    </beans>
    
  3. 测试

     @Test
        public void test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
            User user = context.getBean("user", User.class);
            System.out.println(user);
        }
    

C命名空间注入

C(构造: Constructor)命名空间 , 属性依然要设置set方法

代码

  1. 创建实体类

    public class User {
        private String name;
        private int age;
    
        public User() {
        }
    
        //这里有有参构造
        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
  2. 配合.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"
           xmlns:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        
        <bean id="user2" class="com.gener.pojo.User" c:age="24" c:name="小鸡儿"/>
    
    	<!--
    		C命名空间需要添加的约定文件:
    		xmlns:c="http://www.springframework.org/schema/c"
    	-->
    
    </beans>
    
  3. 测试

    @Test
        public void test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
            User user = context.getBean("user2", User.class);
            System.out.println(user);
        }
    

bean的作用域

bean的作用域

  1. 单例模式(Spring默认)

    <bean id="user2" class="com.gener.pojo.User" c:age="24" c:name="小鸡儿" scope="singleton"/>
    
  2. 原型模式(每次从容器中get时,都会产生一个新的对象)

    <bean id="user2" class="com.gener.pojo.User" c:age="24" c:name="小鸡儿" scope="prototype"/>
    
  3. 剩下3种只能在web开发中使用到;






B站学习狂神说Spring的笔记==>视频入口


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值