spring

88 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第20天

spring

1、sring配置

1. 别名

     <!--别名,如果添加了别名,我们也可以使用别名获取到这个对象-->
     <alias name="user" alias="userNew"/>
 ​

2.bean

     <!--
     id:bean的唯一标识符,也就是相当于我们学的对象名
     class:bean对象所对应的全限定名:包名+类名
     name:也是别名,而且name可以同时取多个别名
         -->
     <bean id="userT" class="com.kuang.pojo.UserT" name="user2 u2,u3;u4">
         <property name="name" value="黑心白莲"/>
     </bean>
 ​

1. import

这个import。一般用于团队开发使用,它可以将多个配置文件,导入合并为一个。 假设,现在项目中有多个人开发,这三个人负责不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的!

  • 张三
  • 李四
  • 王五
  • applicationContext.xml
 <import resource="bean.xml"/>
 <import resource="bean2.xml"/>
 <import resource="bean3.xml"/>
 ​

使用的时候,直接使用总的配置就可以了。

2.依赖注入

2.1 构造器注入

前面已经介绍过,参考4、IOC创建对象的方式

2.2 Set方式注入【重点】

  • 依赖注入:Set注入

    • 依赖:bean对象的创建都依赖于容器!
    • 注入:bean对象中所有属性,由容器来注入!

【环境搭建】

  1. 复杂类型
 public class Address {
     private String address;
 ​
     public String getAddress() {
         return address;
     }
 ​
     public void setAddress(String address) {
         this.address = address;
     }
 }
 ​
  1. 真实测试
 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;
 }
 ​
  1. beans.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="student" class="com.kuang.pojo.Student">
         <!--第一种:普通值注入,value        -->
         <property name="name" value="黑心白莲"/>
     </bean>
 </beans>
 ​
  1. 测试类
 public class MyTest {
     public static void main(String[] args) {
         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 ​
         Student student = (Student) context.getBean("student");
         System.out.println(student.getName());
     }
 }
 ​
  1. 完善注入信息
     <bean id="address" class="com.kuang.pojo.Address">
         <property name="address" value="西安"/>
     </bean>
 ​
     <bean id="student" class="com.kuang.pojo.Student">
         <!--第一种:普通值注入,value        -->
         <property name="name" value="黑心白莲"/>
 ​
         <!--第二种:        -->
         <property name="address" ref="address"/>
 ​
         <!--数组        -->
         <property name="books">
             <array>
                 <value>红楼梦</value>
                 <value>西游记</value>
                 <value>水浒传</value>
                 <value>三国演义</value>
             </array>
         </property>
 ​
         <!--List        -->
         <property name="hobbies">
             <list>
                 <value>打篮球</value>
                 <value>看电影</value>
                 <value>敲代码</value>
             </list>
         </property>
 ​
         <!--Map        -->
         <property name="card">
             <map>
                 <entry key="身份证" value="123456789987456321"/>
                 <entry key="银行卡" value="359419496419481649"/>
             </map>
         </property>
 ​
         <!--Set        -->
         <property name="games">
             <set>
                 <value>LOL</value>
                 <value>COC</value>
                 <value>BOB</value>
             </set>
         </property>
 ​
         <!--NULL        -->
         <property name="wife">
             <null/>
         </property>
 ​
         <!--Properties        -->
         <property name="info">
             <props>
                 <prop key="driver">20191029</prop>
                 <prop key="url">102.0913.524.4585</prop>
                 <prop key="user">黑心白莲</prop>
                 <prop key="password">123456</prop>
             </props>
         </property>
 ​
     </bean>
 ​

2.3、拓展方式注入

我们可以使用p命名空间和c命名空间进行注入 官方解释:

image-20221019181031419

使用:

 <?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:c="http://www.springframework.org/schema/c"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans.xsd">
 ​
     <!--p命名空间注入,可以直接注入属性的值:property-->
     <bean id="user" class="com.kuang.pojo.User" p:name="黑心白莲" p:age="20"/>
 ​
     <!--c命名空间注入,通过构造器注入:constructor-args-->
     <bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22"/>
 ​
 </beans>
 ​

测试:

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

注意点:p命名和c命名空间不能直接使用,需要导入xml约束!

        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:c="http://www.springframework.org/schema/c"
 ​

2.4 bean的作用域

image-20221019181146136

  1. 单例模式(Spring默认机制)
 <bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22" scope="singleton"/>
 ​
  1. 原型模式:每次从容器中get的时候,都会产生一个新对象!
 <bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22" scope="prototype"/>
 ​
  1. 其余的request、session、application、这些只能在web开发中使用到!

总结

再次看spring视频,感触还是蛮多的,对spring的自动装配有更深的理解,@overwite的底层演变有了了解。