Spring集合注入和域属性自动注入byName和byType

本文内容纲要:

- Spring集合的注入

- 域属性自动注入

Spring集合的注入

  步骤一:导入依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.1.5.RELEASE</version>

</dependency>

<!--切面-->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.7.0</version>

</dependency>

  步骤二:创建实体类

public class Studenttest {

//数组

private String [] arrays;

//list集合

private List<Integer> liats;

//set集合

private Set<String> sets;

//map集合

private Map<String,Object> maps;

//配置

private Properties properties;

@Override

public String toString() {

return "Studenttest{" +

"arrays=" + Arrays.toString(arrays) +

", liats=" + liats +

", sets=" + sets +

", maps=" + maps +

", properties=" + properties +

'}';

}

public String[] getArrays() {

return arrays;

}

public void setArrays(String[] arrays) {

this.arrays = arrays;

}

public List<Integer> getLiats() {

return liats;

}

public void setLiats(List<Integer> liats) {

this.liats = liats;

}

public Set<String> getSets() {

return sets;

}

public void setSets(Set<String> sets) {

this.sets = sets;

}

public Map<String, Object> getMaps() {

return maps;

}

public void setMaps(Map<String, Object> maps) {

this.maps = maps;

}

public Properties getProperties() {

return properties;

}

public void setProperties(Properties properties) {

this.properties = properties;

}

}

  步骤三:创建大配置文件

<?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

http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="diTest" class="cn.spring.entity.DiTest">

<!--数组属性-->

<property name="arrays">

<!--数组-->

<array>

<value>张三</value>

<value>李四</value>

</array>

</property>

<!--list-->

<property name="lists">

<list>

<value>18</value>

<value>19</value>

</list>

</property>

<!--set-->

<property name="sets">

<set>

<value>宇宙无敌美少女祝愿是也</value>

<value>快下课了,他想变身</value>

</set>

</property>

<!--Map-->

<property name="maps">

<map>

<entry key="name" value="王洪涛"/>

<entry key="age" value="18"/>

</map>

</property>

<!--properties-->

<property name="properties">

<props>

<prop key="jdbc.driver">com.mysql.jdbc.Driver</prop>

<prop key="jdbc.username">root</prop>

</props>

</property>

</bean>

</beans>

  步骤四:测试

@Test

public void studenttest(){

ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml");

Studenttest sss = (Studenttest)atc.getBean("diTest");

System.out.println(sss.toString());

}

域属性自动注入

  byName与byType

  步骤一:创建两个实体类

public class Student {

private Integer stuid;

private String stuName;

private Teacher teacher;

public Teacher getTeacher() {

return teacher;

}

public void setTeacher(Teacher teacher) {

this.teacher = teacher;

}

public Integer getStuid() {

return stuid;

}

public void setStuid(Integer stuid) {

this.stuid = stuid;

}

public String getStuName() {

return stuName;

}

public void setStuName(String stuName) {

this.stuName = stuName;

}

}

public class Teacher {

private Integer tid;

private String tname;

public Integer getTid() {

return tid;

}

public void setTid(Integer tid) {

this.tid = tid;

}

public String getTname() {

return tname;

}

public void setTname(String tname) {

this.tname = tname;

}

}

  步骤二:创建大配置文件(在bean节点中增加autowire属性,设值为byType)

  要求:给Teacher赋值的bean节点的id与域属性的名字相同

<?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:aop="http://www.springframework.org/schema/aop"

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://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="stuentity" class="com.SpringMckz.entity.Student" autowire="byName">

<property name="stuid" value="221"></property>

<property name="stuName" value="王某"></property>

</bean>

<bean id="teacher" class="com.SpringMckz.entity.Teacher">

<property name="tid" value="999"></property>

<property name="tname" value="张"></property>

</bean>

</beans>

  步骤四:测试(autowire="byName")

@Test

public void testsentitu(){

ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml");

Student sss = (Student)atc.getBean("stuentity");

System.out.println(sss.getTeacher().getTid());

System.out.println(sss.getTeacher().getTname());

}

  步骤五:测试(autowire="byType")

  要求:保证域属性的类型与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"

xmlns:aop="http://www.springframework.org/schema/aop"

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://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="stuentity" class="com.SpringMckz.entity.Student" autowire="byType">

<property name="stuid" value="123"></property>

<property name="stuName" value="张某"></property>

</bean>

<bean id="teacher" class="com.SpringMckz.entity.Teacher">

<property name="tid" value="234"></property>

<property name="tname" value="刘默"></property>

</bean>

</beans>

@Test

public void testsentitu(){

ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml");

Student sss = (Student)atc.getBean("stuentity");

System.out.println(sss.getTeacher().getTid());

System.out.println(sss.getTeacher().getTname());

}

本文内容总结:Spring集合的注入,域属性自动注入,

原文链接:https://www.cnblogs.com/F017/p/11752967.html

以上是 Spring集合注入和域属性自动注入byName和byType 的全部内容, 来源链接: utcz.com/z/296013.html

回到顶部