笔记---Spring

本文详细介绍了如何在Spring框架中通过XML配置创建对象,包括bean的定义、属性注入以及别名设置。同时,讲解了依赖注入的多种方式,如set注入、p标签注入和c标签注入。此外,还提及了Bean的作用域和Spring的自动装配机制,如byName和byType。
摘要由CSDN通过智能技术生成

Spring

一、使用Spring创建对象

1.1 xml创建bean

想使用spring配置必须导入spring-context

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.20</version>
</dependency>
  1. 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">
    </beans>
    
  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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" class="com.betterAb.dto.Hello">
        <property name="str" value="hello" />
    </bean>
    <!--bean标签中的name为别名,且能取多个-->
    <bean id="hi" class="com.betterAb.dto.Hello" name="userOne,userFour">
        <property name="str" value="hello2"/>
    </bean>
    <!--bean的别名-->
    <alias name="hello" alias="userTwo"/>
    <alias name="hi" alias="userThree"/>
    <!--import导入多个xml,最后合并为一个bean的配置-->
    <import resource="beans.xml"/>
</beans>

property:字段

​ name: 字段名

​ value:字段值

​ ref:引用(引用spring其他的bean)

  1. 实例化容器

    //获取spring的上下文对象
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    

1.2 依赖注入

  1. 准备

    public class Address {
        private String location;
    
        public String getLocation() {
            return location;
        }
    
        public void setLocation(String location) {
            this.location = location;
        }
    }
    
    public class Student {
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobbies;
        private Map<String,String> card;
        private Set<String> games;
        private String wife;
        private Properties info;
     	...(get(),set())
     }
    
  2. set注入

<?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="student" class="com.betterAb.dto.Student">

        <!--1.普通值注入-->
        <property name="name" value="张三"/>

        <!--2.引用类型使用ref注入-->
        <property name="address" ref="address"/>

        <!--3.数组注入-->
        <property name="books">
            <array>
                <value>三国演义</value>
                <value>水浒传</value>
                <value>西游记</value>
                <value>红楼梦</value>
            </array>
        </property>

        <!--4.List注入-->
        <property name="hobbies">
            <list>
                <value>足球</value>
                <value>篮球</value>
                <value>乒乓球</value>
            </list>
        </property>

        <!--5.Map注入-->
        <property name="card">
            <map>
                <entry key="学生卡" value="1000449563"/>
                <entry key="工卡" value="220373"/>
                <entry key="身份证" value="620103199609200311"/>
            </map>
        </property>

        <!--6.Set注入-->
        <property name="games">
            <set>
                <value>荒野大镖客</value>
                <value>英雄联盟</value>
                <value>坦克世界</value>
            </set>
        </property>

        <!--7.空值注入-->
        <property name="wife">
            <null/>
        </property>

        <!--8.Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">20190525</prop>
                <prop key="性别">男性</prop>
                <prop key="username">staff_id</prop>
                <prop key="password">npmHR1236</prop>
            </props>
        </property>
    </bean>


    <bean id="address" class="com.betterAb.dto.Address">
        <property name="location" value="杭州"/>
    </bean>
</beans>
  1. p标签,c标签注入

    1. p标签注入(------>set注入)

      <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"   <------------p
             
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              https://www.springframework.org/schema/beans/spring-beans.xsd">
      
          <bean name="user" class="com.betterAb.dto.User" p:mess="ok" p:password="npmHR1236" p:status="true" p:username="张三"/>
          
      </beans>
      
    2. c标签注入(-------->构造器注入,需要构造方法)

      <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"   <-------------c
             
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              https://www.springframework.org/schema/beans/spring-beans.xsd">
          
          <bean id="user" class="com.betterAb.dto.User" c:_0="张三" c:_1="npmHR1236" c:_2="ok" c:_3="false"/>
      
      </beans>
      

1.3 Bean作用域

在这里插入图片描述

1.4 Spring自动装配

  1. byName
<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="student" class="com.betterAb.pojo.Student" autowire="byName">
        <property name="name" value="张三"/>
    </bean>
    <bean id="address" class="com.betterAb.pojo.Address">
        <property name="location" value="上海"/>
    </bean>
</beans>
  1. byType
<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="student" class="com.betterAb.pojo.Student" autowire="byType">
        <property name="name" value="张三"/>
    </bean>
    //byType可以省略要装配的id
    <bean class="com.betterAb.pojo.Address">
        <property name="location" value="上海"/>
    </bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值