Spring 是 Java EE 编程领域的一款轻量级的开源框架,它的开发就是为了简化 Java 企业级应用程序的开发难度和周期。那spring注入对象是属于什么引用?下面来我们就来给大家讲解一下。
spring注入对象是属于相互引用关系。
spring注入方式有哪些?
构造方法注入:
index: 参数索引位置, 指定了index可以不写name。
name: 参数名称, 跟构造器传入的参数名称保持一致。
ref: 引用对象。
value: 基本类型(包括String)的值。
set方法注入:
name: set方法名称,首字母要小写。
ref: 引用对象。
value: 基本类型(包括String)的值。
注入的三种数据类型:
基本数据类型
引用数据类型
集合数据类型
1.测试代码
Student类
public class Student { // 基本数据类型 private int id; private String username; private String telephone; // 引用数据类型 private StudentClass studentClass; // List private List < String > scores; private List < StudentClass > list; //Set private Set < String > courses; private Set < StudentClass > set; //Map private Map < String, StudentClass > map; //Properties private Properties properties; public void setId(int id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setTelephone(String telephone) { this.telephone = telephone; } public void setStudentClass(StudentClass studentClass) { this.studentClass = studentClass; } public void setScores(List < String > scores) { this.scores = scores; } public void setList(List < StudentClass > list) { this.list = list; } public void setCourses(Set < String > courses) { this.courses = courses; } public void setSet(Set < StudentClass > set) { this.set = set; } public void setMap(Map < String, StudentClass > map) { this.map = map; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Student{" + "id=" + id + ", username='" + username + '\'' + ", telephone='" + telephone + '\'' + ", studentClass=" + studentClass + ", scores=" + scores + ", list=" + list + ", courses=" + courses + ", set=" + set + ", map=" + map + ", properties=" + properties + '}'; } }
StudentClass类
public class StudentClass { private int cid; private String cname; public StudentClass() {} public StudentClass(int cid, String cname) { this.cid = cid; this.cname = cname; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } @Override public String toString() { return "StudentClass{" + "cid=" + cid + ", cname='" + cname + '\'' + '}'; } }
测试代码
public class Main { @Test public void test() { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) factory.getBean("cloud"); System.out.println(student); } }
Spring配置文件
<?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 name="spring" class="site.zhouyun.entity.StudentClass"> <property name="cid" value="10001"></property> <property name="cname" value="Spring"></property> </bean> <!-- set方法注入普通数据类型 --> <bean name="java" class="site.zhouyun.entity.StudentClass"> <property name="cid" value="10002"></property> <property name="cname" value="Java"></property> </bean> <!-- set方法注入引用类型 --> <bean id="student01" class="site.zhouyun.entity.Student"> <property name="studentClass" ref="java"></property> </bean> <bean name="cloud" class="site.zhouyun.entity.Student"> <property name="id" value="2020110601"></property> <property name="username" value="孤云"></property> <!-- null值注入 --> <property name="telephone"> <null></null> </property> <property name="studentClass" ref="java"></property> <!-- set方法注入List数据类型 --> <property name="scores"> <!-- 有序、允许重复 --> <list> <value>100</value> <value>90</value> <value>87</value> <value>78</value> <value>90</value> </list> </property> <property name="list"> <list> <ref bean="spring"></ref> <ref bean="java"></ref> </list> </property> <!-- set方法注入Set数据类型 --> <property name="courses"> <!-- 无序、不允许重复 --> <set> <value>Spring</value> <value>Spring</value> <value>Java</value> <value>C</value> <value>Python</value> </set> </property> <property name="set"> <set> <ref bean="java"></ref> <ref bean="spring"></ref> </set> </property> <!-- set方法注入Map数据类型 --> <property name="map"> <map> <entry key="001" value-ref="spring"></entry> <entry key="002" value-ref="java"></entry> </map> </property> <!-- set方法注入Properties数据类型 --> <property name="properties"> <props> <prop key="1">properties01</prop> <prop key="2">properties02</prop> <prop key="3">properties03</prop> </props> </property> </bean> </beans>
2. 测试结果
Student{id=2020110601, username=‘孤云’, telephone=‘null’, studentClass=StudentClass{cid=10002, cname=‘Java’}, scores=[100, 90, 87, 78, 90], list=[StudentClass{cid=10001, cname=‘Spring’}, StudentClass{cid=10002, cname=‘Java’}], courses=[Spring, Java, C, Python], set=[StudentClass{cid=10002, cname=‘Java’}, StudentClass{cid=10001, cname=‘Spring’}], map={001=StudentClass{cid=10001, cname=‘Spring’}, 002=StudentClass{cid=10002, cname=‘Java’}}, properties={3=properties03, 2=properties02, 1=properties01}}
spring注入方式有构造方法注入以及set方法注入,我们在选择的时候任意一种都可以,大家可以试试!最后大家如果想要了解更多java架构师知识,敬请关注奇Q工具网。
推荐阅读: