Spring知识点(手动装配、自动装配)

2021/10/24 23:40:14

本文主要是介绍Spring知识点(手动装配、自动装配),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

有时候我们在使用spring建立实体类的时候还需要为属性赋值,比如(多参数构造)

首先我们需要一个实体类

image

public class Student {
//属性
private String Name;
private String Sex;
private String Phone;
private int Age;
//无参数构造
public Student() {
	super();
}
//全参构造
public Student(String name, String sex, String phone, int age) {
	super();
	Name = name;
	Sex = sex;
	Phone = phone;
	Age = age;
}
//get、set方法
    
public String getName() {
		return Name;
	}
public void setName(String name) {
		Name = name;
	}
	
public String getSex() {
		return Sex;
	}
	
public void setSex(String sex) {
		Sex = sex;
	}
	
public String getPhone() {
		return Phone;
	}
	
public void setPhone(String phone) {
		Phone = phone;
	}
	
public int getAge() {
		return Age;
	}
	
public void setAge(int age) {
		Age = age;
	}
   @Override
public String toString() {
	return "Student [Name=" + Name + ", Sex=" + Sex + ", Phone=" + Phone + ", Age=" + Age + "]";
}
  
}

现在我们要做的就是在构建实体类Student时赋值给它。

手动装配(三种)

构造注入--按顺序注入

 <!--构造注入注册-按顺序丢值-类型要和全参构造函数的参数类型一一对应 -->
    <bean id="student00" class="entity.Student">
      <constructor-arg>
         <value>"张三"</value>
      </constructor-arg>
      <constructor-arg>
         <value>"sda"</value>
      </constructor-arg>
      <constructor-arg>
         <value>"男"</value>
      </constructor-arg>
      <constructor-arg>
         <value>12</value>
      </constructor-arg>   
    </bean>

构造注入--按参数名注入

<!--构造注入-使用参数名注入-这里的name,phone,age,sex不能是属性Name,Phone,Sex,Age一定需要和全参数构造的参数名相同  -->
    <bean id="student" class="entity.Student">
        <constructor-arg name = "name" value ="张三"></constructor-arg>
        <constructor-arg name = "phone" value ="123465"></constructor-arg>
        <constructor-arg name = "age" value ="22"></constructor-arg>
        <constructor-arg name = "sex" value ="男"></constructor-arg>
    </bean>

构造注入--按下标注入

<!--构造注入-使用下标的构造注入-从0开始对应全构造函数的参数 -->
    <bean id="student1" class="entity.Student">
        <constructor-arg index="0" value ="张三"></constructor-arg>
        <constructor-arg index="1" value ="123465"></constructor-arg>
        <constructor-arg index="2" value ="男"></constructor-arg>
        <constructor-arg index="3" value ="22"></constructor-arg>
    </bean>

Set注入--按属性名注入

 <!--set注入(先调用空构造建立一个实例,之后调用set get方法)不区分大小写-->
 <bean id="student2" class="entity.Student">
        <property name="Name">
          <value>"张三"</value>
        </property>
        <property name="Age">
          <value>22</value>
        </property>
        <property name="Sex">
          <value>"男"</value>
        </property>
        <property name="Phone">
          <value>"13465465"</value>
        </property>
 </bean>

这里可以把value写在里面(效果一样)

 <bean id="student2" class="entity.Student">
        <property name="Name" value="张三"></property>
        <property name="Age" value="12"></property>
        <property name="Sex" value="男"></property>
        <property name="Phone" value="123456"></property>
    </bean>

注意:set注入使用的时property标签这个标签没有index属性,并且一定要为它带上name属性。

P空间注入

<!--使用p空间要先引入-->
”xmlns:p="http://www.springframework.org/schema/p"
<!--set注入(先调用空构造 之后调用set get方法)不区分大小写-->
<bean id="student2" class="entity.Student" p:name="张三" p:phone="123456" p:age="12" p:sex="女">


</bean>

自动装配(两种)

通过xml

我们只需要在xml配置文件中的bean标签中加入一个属性autowire即可

image

我们通过提示发现有5中参数可以选

  • No:即不启用自动装配

  • byName

    首先我们新建立一个类里面包含一个Student属性

    public class people {
       private Student syudent2;
    
    public Student getSyudent2() {
    	return syudent2;
    }
    
    public void setSyudent2(Student syudent2) {
    	this.syudent2 = syudent2;
    }
    
    public people(Student syudent2) {
    	super();
    	this.syudent2 = syudent2;
    }
    
    public people() {
    	super();
    }
    
    }
    
    

    自动装配时我们需要先把属性Student2注册成一个bean

    <!--set注入(先调用空构造 之后调用set get方法)不区分大小写-->
    <bean id="student2" class="entity.Student" p:name="张三" p:phone="123456" p:age="12" p:sex="女">
    
    
    </bean>
    

    接着注册people类

    <bean id="people" class="entity.people" autowire="byName"></bean>
    

    byname会将属性名student2和已经注册过的bean的id进行匹配省去了我们的代码量

  • byType

    还是用到了上面的people类

    同理先注册一个Student的bean

    <bean id="student2" class="entity.Student" p:name="张三" p:phone="123456" p:age="12" p:sex="女"> </bean>
    

    接着注册people类

    <bean id="people" class="entity.people" autowire="byType"></bean>
    

    byType会根据属性的类型和现有的bean类型做比较同类型就注册进去,另一方面如果匹配了多个bean就会报错

  • constructor:与byType方式类似,不同之处在于它应用的不是属性了而是应用于构造器参数。如果容器中没有找到与构造器参数类型一致的bean,那么抛出异常。它会匹配构造器参数和已经注册的bean类型。

  • default:由上级标签的default-autowire属性确定。beans上面也可以设置autowire、default会继承beans上面的配置

通过注解

你可能会想自动装配能不能更自动一点,我还是要写东西。

有那就是注解

使用前需要打开注解扫描

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd ">
        <!-- 这里需要引入很多的http,并不完全这里我们只引入了spring-beans --><!-- 头文件最后加空格防止不被识别 -->
    
    <!-- 这个代表开启注解扫描会扫描包下面的类去装配bean -->
    <context:annotation-config/>

</beans>
<conteext:annotation-config/> 必须要写在xml中,这是用来开启注解的支持,如果不加上注解就无效。

我们回到people类

public class people {
//这回我直接在这里注入 
@Autowired 
    
private Student syudent2;

public Student getSyudent2() {
	return syudent2;
}

public void setSyudent2(Student syudent2) {
	this.syudent2 = syudent2;
}

public people(Student syudent2) {
	super();
	this.syudent2 = syudent2;
}

public people() {
	super();
}

}

回到beans如下

<bean id="student2" class="entity.Student" p:name="张三" p:phone="123456" p:age="12" p:sex="女"></bean>
<!--发现这里的autowire不用写了也能把student2注册到people里面-->
<bean id="people" class="entity.people"></bean>

那么注解

@Autowired 首先掉用byType类型、没有找到后使用byName类型去匹配

我们可以使用

public class people {
@Autowired 
@Qualifier(value = "student2")
private Student syudent2;
}

这样就会强制使用byName类型去匹配id=student2的bean
@Resource是autowire的简化版本只会使用byName类型去匹配



这篇关于Spring知识点(手动装配、自动装配)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程