创建对象

XML 文件

spring 可以通过 xml 文件配置对象信息,以标签的形式

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:util="http://www.springframework.org/schema/util"
               xmlns:p="http://www.springframework.org/schema/p"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">

  <bean class="springDemo.UserInfo" id="userInfo" name="info">

</beans>

标签有许多属性,对于创建对象来说,比较常用的有:

  1. class,class 属性的值为所创建对象的全限定类名
  2. id 属性为对象的 id

SpringBean

package springDemo;
public class UserInfo {
    private String StudentId;
    private String UserId;
    private String Name;
    private String Section;
    public String getStudentId() {
        return StudentId;
    }
    public void setStudentId(String studentId) {
        StudentId = studentId;
    }

    public String getUserId() {
        return UserId;
    }

    public void setUserId(String userId) {
        UserId = userId;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getSection() {
        return Section;
    }

    public void setSection(String section) {
        Section = section;
    }
    public UserInfo(){
        System.out.println("UseriNfo类初始化");
    }
    public void display(){
        System.out.println(Name+"::"+Section+"::"+StudentId+"::"+UserId);
    }
    public UserInfo(String name,String section,String userid,String StudentId){
        this.Name=name;
        this.Section=section;
        this.UserId=userid;
        this.StudentId=StudentId;
    }
}

当配置了 xml 文件后,UserInfo 类会被识别为 SpringBean

Spring 启动文件

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("container.xml");
UserInfo userInfo=applicationContext.getBean(UserInfo.class);

ApplicationContext 为 spring 的核心接口,通过加载 xml 文件,完成 Spring 对象的配置

属性注入

setter 方法注入

注入值类型

<bean class="springDemo.UserInfo" id="userInfo" name="info">
               <property name="name" value="周治淦"></property>
               <property name="section" value="技术中心"></property>
               <property name="studentId" value="3192010810145"></property>
               <property name="userId" value="1247"></property>
  </bean>

通过 property 标签注入值类型

注入外部 bean

<bean class="springDemo.dao.UserDaoImlp" id="userDaoImlp" ></bean>
        <bean class="springDemo.service.UserService" id="userService" scope="prototype">
          	<--通过property标签的ref属性,注入外部的bean,ref的值为外部bean的id-->
                <property name="userDao" ref="userDaoImlp" ></property>
                <property name="userInfo" ref="userInfo"></property>
                <property name="userInfo.studentId" value="${prog.userid}"></property>
        </bean>

级联注入

<bean class="springDemo.service.UserService" id="userService" scope="prototype">
  						<--在property标签内部注入bean-->
               <property name="userDao"  >
                       <bean class="springDemo.dao.UserDaoImlp" id="userDaoImlp1" ></bean>
               </property>
               <property name="userInfo" ref="userInfo"></property>
               <property name="userInfo.studentId" value="${prog.userid}"></property>
       </bean>
<bean id="emp" class="cn.zsp.spring5.bean.Emp">
       <property name="ename" value="zsp"></property>
       <property name="gender" value=""></property>
       <property name="dept" ref="dept" ></property>
       <property name="dept.dname" value="行政部"></property>
   </bean>
   <bean id="dept" class="cn.zsp.spring5.bean.Dept">

   </bean>

注入集合属性

package springDemo;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class CollectionTest {
    private int[] arrayTest;
    private List<String> ListTest;
    private Map<String,String> MapTest;
    private Set<String> SetTest;
}
<bean class="springDemo.CollectionTest" id="collectionTest">
  <---->
        <property name="arrayTest">
                <array>
                        <value>1</value>
                        <value>2</value>
                        <value>3</value>
                </array>
        </property>
        <property name="listTest">
                <list>
                        <value>zzg</value>
                        <value>helloworld</value>
                        <value>Test</value>
                </list>
        </property>
        <property name="mapTest">
                <map>
                        <entry key="123" value="132"></entry>
                        <entry key="kkk" value="lll"></entry>
                </map>
        </property>
        <property name="setTest">
                <set>
                        <value>kkk</value>
                        <value>lll</value>
                </set>
        </property>
</bean>

将集合属性从 property 里抽出来

导入 util 标签
<beans xmlns="http://www.springframework.org/schema/beans"
       		声明util
              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">
使用 util:list 包裹 list 并命名
<util:list id="listTest">
               <value>zzg</value>
               <value>lll</value>
               <value>llll</value>
       </util:list>
使用 list
 <bean class="springDemo.CollectionTest" id="collectionTest">
   <property name="listTest" ref="listTest"></property>
</bean>

注入特殊符号和空值

注入特殊符号

转义

< < 小于号

大于号
& & 和
‘ ‘ 单引号
“ “ 双引号

<bean id="userInfo" class="springDemo.UserInfo" p:name="${prog.name}" p:section="${prog.section}" p:userId="123" >
               <property name="studentId">
                       <value><<南京>></value>
               </property>
       </bean>
CDATA
<bean id="userInfo" class="springDemo.UserInfo" p:name="${prog.name}" p:section="${prog.section}" p:userId="123" >
                <property name="studentId">
                        <value><![CDATA[<<南京>>]]></value>
                </property>
        </bean>

注入空值

<bean id="userInfo" class="springDemo.UserInfo"  p:section="${prog.section}" p:userId="123" >
                <property name="name">
                        <null></null>
                </property>
        </bean>

构造器注入

<bean class="springDemo.UserInfo" id="userInfo" name="info">
                <constructor-arg name="name" value="123"></constructor-arg>
                <constructor-arg name="StudentId" value="123"></constructor-arg>
                <constructor-arg name="userid" value="123"></constructor-arg>
                <constructor-arg name="section" value="123"></constructor-arg>

        </bean>

P 名称空间注入

导入 p 名称空间

<beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:util="http://www.springframework.org/schema/util"
              xmlns:p="http://www.springframework.org/schema/p">
   <bean id="userInfo" class="springDemo.UserInfo" p:name="${prog.name}" 		p:section="${prog.section}" p:userId="123" >
               <property name="studentId">
                       <value><![CDATA[<<南京>>]]></value>
               </property>
       </bean>
       </beans>

工厂 bean

package springDemo.dao;

import org.springframework.beans.factory.FactoryBean;
import springDemo.UserInfo;

public class factory implements FactoryBean<UserInfo> {

    @Override
    public UserInfo getObject() throws Exception {
        return new UserInfo();
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}

** 工厂 Bean 需要实现 FactoryBean 接口,并在泛型中传入要传回的类型**
** 需要实现接口的方法,并在 getObject 方法中传回对象。**

<bean id="beanfa" class="springDemo.dao.factory" ></bean>
UserInfo userInfo1=applicationContext.getBean("beanfa",UserInfo.class);

bean 的作用域

单例

单例表示无论获取多少次某对象,Spring 都只创建一个对象,并返回

<bean class="springDemo.service.UserService" id="userService" scope="singleton"></bean>

多例

多例表示每次获取某对象,spring 都创建一个对象,并返回。每次获得的对象不同

<bean class="springDemo.service.UserService" id="userService" scope="prototype"></bean>

bean 的生命周期



Bean 的完整生命周期经历了各种方法调用,这些方法可以划分为以下几类:

  1. Bean 自身的方法 : 这个包括了 Bean 本身调用的方法和通过配置文件中的 init-method 和 destroy-method 指定的方法
  2. Bean 级生命周期接口方法 : 这个包括了 BeanNameAware、BeanFactoryAware、InitializingBean 和 DiposableBean 这些接口的方法
  3. 容器级生命周期接口方法 : 这个包括了 InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。
  4. 工厂后处理器接口方法 : 这个包括了 AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer 等等非常有用的工厂后处理器 接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。

自动装配

<bean class="springDemo.service.UserService" id="userService" scope="prototype" autowire="byName">

autowire 属性来配置自动装配

  1. byType
    1. 通过类成员变量的类型来自动注入对象
    2. xml 中只可创建一个同类型 bean,多个会报错,找不到具体注入哪个
  2. byName
    1. 通过 bean 的 id 来注入对象
    2. bean 的 id 只能与成员变量的变量名一样
    3. xml 可以有多个同类型的 bean

导入外部属性文件

导入 context 名称空间

image.png

导入外部属性文件

<context:property-placeholder location="config.properties"></context:property-placeholder>

属性文件配置

prog.name=zzg
prog.section=jishu
prog.userid=1247

使用

“${}”

<bean id="userInfo" class="springDemo.UserInfo"  p:section="${prog.section}" p:userId="123" >