实现Map集合、List集合注入
applicationContext2.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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- <util:list id="list">
<ref bean="ps1" />
<ref bean="ps2" />
</util:list> -->
<bean id="javaCourse" class="com.spring.JavaCourse">
<property name="name" value="Java课程"></property>
</bean>
<bean id="ps1" class="com.spring.Profession">
<property name="name" value="班长"></property>
</bean>
<bean id="ps2" class="com.spring.Profession">
<property name="name" value="团支书"></property>
</bean>
<bean id="student" class="com.spring.Student" >
<!-- 通过property中value属性或者value子元素都可以实现向某一个属性注入值 -->
<property name="sno" value="117333440114">
<!-- <value>117333440114</value> -->
<!-- <null></null>对某个属性赋值为空 -->
</property>
<property name="name" value="赖文卓"></property>
<property name="age" value="21"></property>
<!-- ref属性可以指定参考对象 ,它的值时改对象在容器中id元素对应的值 -->
<property name="jc" ref="javaCourse"></property>
<!-- 属性类型是List集合 -->
<property name="ps" > <!-- ref="list" -->
<list>
<bean id="ps1" class="com.spring.Profession">
<property name="name" value="班长"></property>
</bean>
<bean id="ps2" class="com.spring.Profession">
<property name="name" value="团支书"></property>
</bean>
<!-- <ref bean="ps1"></ref>
<ref bean="ps2"></ref> -->
</list>
</property>
<!-- 属性类型是Map集合 -->
<property name="pf">
<map>
<entry key="first" value-ref="ps1"></entry>
<entry key="second" value-ref="ps2"></entry>
</map>
</property>
</bean>
</beans>
实现自动注入
这里我并没有显式为Student对象注入JavaCourse属性,而是使用autowired="byName"代替,这样一来Spring会帮我们处理这些细节,将名字是JavaCourse的组件注入到Student对象中。
applicationContext3.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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<bean id="javaCourse" class="com.spring.JavaCourse">
<property name="name" value="Java课程"></property>
</bean>
<bean id="jc" class="com.spring.JavaCourse">
<property name="name" value="javaee课程"></property>
</bean>
<!-- 自动注入:
autowire属性设置自动注入方式
byType:根据实体类中属性对应的类型
byName:根据实体类中属性名称
是否使用自动注入,根据你想实现的功能,如果想把实体类中的全部对象类型注入到该实体对象,那就使用自动注入
反之,不使用,因为自动注入不灵活
-->
<bean id="student" class="com.spring.Student" autowire="byName">
<!-- 通过property中value属性或者value子元素都可以实现向某一个属性注入值 -->
<property name="sno" value="117333440114">
<!-- <value>117333440114</value> -->
<!-- <null></null>对某个属性赋值为空 -->
</property>
<property name="name" value="赖文卓"></property>
<property name="age" value="21"></property>
<!-- ref属性可以指定参考对象 ,它的值时改对象在容器中id元素对应的值 -->
<property name="jc" ref="jc"></property>
</bean>
</beans>
实体类1
package com.spring;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Student {
private String sno;
private String name;
private int age;
private JavaCourse jc;
private List<Profession> ps;
private Map<String, Profession> pf;
public Student() {
}
public Student(String sno, String name, int age, JavaCourse jc, List<Profession> ps, Map<String, Profession> pf) {
super();
this.sno = sno;
this.name = name;
this.age = age;
this.jc = jc;
this.ps = ps;
this.pf = pf;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public JavaCourse getJc() {
return jc;
}
public void setJc(JavaCourse jc) {
this.jc = jc;
}
public List<Profession> getPs() {
return ps;
}
public void setPs(List<Profession> ps) {
this.ps = ps;
}
public Map<String, Profession> getPf() {
return pf;
}
public void setPf(Map<String, Profession> pf) {
this.pf = pf;
}
// @Override
// public String toString() {
// return "Student [sno=" + sno + ", name=" + name + ", age=" + age + ", jc=" + jc + ", ps=" + ps + ", pf=" + pf
// + "]";
// }
@Override
public String toString() {
return "Student [sno=" + sno + ", name=" + name + ", age=" + age + ", jc=" + jc + "]";
}
// 学习课程(一)
public void studyJavaCourse() {
Course course = new JavaCourse();
course.getCourse();
}
public void studyHtmlCourse() {
Course course = new HtmlCourse();
course.getCourse();
}
// 学习课程(二),通过调用课程工厂对象实现选课功能
public void studyCourseByFactory(String name){
CourseFactory cf=new CourseFactory();
cf.getCourse(name);
// System.out.println(course);
}
//学习课程(三),通过IOC方式实现
public void studyCourseByIOC(String name){
//1.实例化Spring容器
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
/*
* 2.获取对象方式
* getBean("bean元素的id值")
* getBean(*.class);
* getBean("bean元素的id值",*.class);
*/
JavaCourse jc=(JavaCourse)ac.getBean(name);
System.out.println(name);
// JavaCourse jc=(JavaCourse)ac.getBean(JavaCourse.class);
// JavaCourse jc=(JavaCourse)ac.getBean(name,JavaCourse.class);
jc.getCourse();
}
}
职业实体类
package com.spring;
/**
*
*<p>Title:Profession</p>
*<p>Description:职业</p>
* @author 赖文卓
* @date 2019年11月20日
*/
public class Profession {
private String name;
public Profession(){
}
public Profession(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Profession [name=" + name + "]";
}
}
测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCollection {
public static void main(String args[]) {
// //1.实例化Spring容器
// ApplicationContext ac=new
// ClassPathXmlApplicationContext("applicationContext2.xml");
// Student student= (Student) ac.getBean("student");
// System.out.println(student);
// 1.实例化Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext3.xml");
Student student = (Student) ac.getBean("student");
System.out.println(student);
}
}
Spring注解实现自动注入