SPring课程学习2
SpringIOC
spring注入
set注入
纯粹的set注入。都是简单的数据类型,在IOC容器中使用<property接定义即可
<bean id="student" class="com.lean_spring.entity.Student">
<property name="gender" value="男"></property>
<property name="intro" value="shuai"></property>
<property name="salary" value="10.00"></property>
<property name="ID" value="100"></property>
<property name="name" value="name"></property>
</bean>
在测试中运行即可得到数据
在student中添加list类型
我们再创建一个Book类,在student中用list存储,
package com.lean_spring.entity;
public class Book {
private String no;
private String name;
private String auther;
@Override
public String toString() {
return "Book{" +
"no='" + no + '\'' +
", name='" + name + '\'' +
", auther='" + auther + '\'' +
'}';
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuther() {
return auther;
}
public void setAuther(String auther) {
this.auther = auther;
}
}
student:
package com.lean_spring.entity;
import java.util.List;
public class Student {
private Integer ID;
private String name;
private char gender;
private Double salary;
private String intro;
private Computer computer;
private List<Book> book;
public Student() {
}
public Student(Integer ID, String name, char gender, Double salary, String intro, Computer computer) {
this.ID = ID;
this.name = name;
this.gender = gender;
this.salary = salary;
this.intro = intro;
this.computer = computer;
}
@Override
public String toString() {
return "Student{" +
"ID=" + ID +
", name='" + name + '\'' +
", gender=" + gender +
", salary=" + salary +
", intro='" + intro + '\'' +
", computer=" + computer +
", book=" + book +
'}';
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
public Computer getComputer() {
return computer;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
public List<Book> getBook() {
return book;
}
public void setBook(List<Book> book) {
this.book = book;
}
}
再IOC中声明它:
<bean id="student4" class="com.lean_spring.entity.Student">
<property name="gender" value="男"></property>
<property name="intro" value="shuai"></property>
<property name="salary" value="10.00"></property>
<property name="ID" value="100"></property>
<property name="name" value="name"></property>
<property name="computer" ref="computer"></property>
<property name="book" >
<list>
</list>
</property>
</bean>
在测试中我们同时测试student2与student4
@Test
public void testStudentTest4(){
Student student2=app.getBean("student2",Student.class);
System.out.println(student2);
Student student4=app.getBean("student4",Student.class);
System.out.println(student4);
}
}
运行后:
可以看到student2中的book是null而student4中则是空数据,student4多做一步new arrylist[];
我们把book添加入数据(bean数据添加与ref的使用参考下文构造器注入),在IOC中添加:
<bean id="book" class="com.lean_spring.entity.Book">
<property name="name" value="book1"></property>
<property name="no" value="1"></property>
<property name="auther" value="auther1"></property>
</bean>
<bean id="student4" class="com.lean_spring.entity.Student">
<property name="gender" value="男"></property>
<property name="intro" value="shuai"></property>
<property name="salary" value="10.00"></property>
<property name="ID" value="100"></property>
<property name="name" value="name"></property>
<property name="computer" ref="computer"></property>
<property name="book" >
<list>
<ref bean="book"></ref>
<bean id="book2" class="com.lean_spring.entity.Book">
<property name="name" value="book2"></property>
<property name="no" value="2"></property>
<property name="auther" value="auther2"></property>
</bean>
</list>
</property>
</bean>
在测试中再次运行testStudentTest4()
在student4中book已经有了数据而student2仍然是null。
在student中添加map类型
在student中添加map属性。增加get(),set()方法,更新toString()方法,其他照旧
package com.lean_spring.entity;
import java.util.List;
import java.util.Map;
public class Student {
private Integer ID;
private String name;
private char gender;
private Double salary;
private String intro;
private Computer computer;
private List<Book> book;
private Map<String ,Object> map;
private Map<String ,Object> map2;
@Override
public String toString() {
return "Student{" +
"ID=" + ID +
", name='" + name + '\'' +
", gender=" + gender +
", salary=" + salary +
", intro='" + intro + '\'' +
", computer=" + computer +
", book=" + book +
", map=" + map +
", map2=" + map2 +
'}';
}
public Map<String, Object> getMap2() {
return map2;
}
public void setMap2(Map<String, Object> map2) {
this.map2 = map2;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public Student() {
}
public Student(Integer ID, String name, char gender, Double salary, String intro, Computer computer) {
this.ID = ID;
this.name = name;
this.gender = gender;
this.salary = salary;
this.intro = intro;
this.computer = computer;
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
public Computer getComputer() {
return computer;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
public List<Book> getBook() {
return book;
}
public void setBook(List<Book> book) {
this.book = book;
}
}
在IOC中为map添加数据(在student4上进行添加):
<bean id="student4" class="com.lean_spring.entity.Student">
<property name="gender" value="男"></property>
<property name="intro" value="shuai"></property>
<property name="salary" value="10.00"></property>
<property name="ID" value="100"></property>
<property name="name" value="name"></property>
<property name="computer" ref="computer"></property>
<property name="book" >
<list>
<ref bean="book"></ref>
<bean id="book2" class="com.lean_spring.entity.Book">
<property name="name" value="book2"></property>
<property name="no" value="2"></property>
<property name="auther" value="auther2"></property>
</bean>
</list>
</property>
<property name="map">
<map>
<entry key="性别" value="女"></entry>
<entry key="电脑" value-ref="computer"></entry>
<entry key="书籍" >
<bean id="book3" class="com.lean_spring.entity.Book">
<property name="name" value="book3"></property>
<property name="no" value="3"></property>
<property name="auther" value="auther3"></property>
</bean>
</entry>
<entry key="mp">
<map>
<entry key="子map" value="测试"></entry>
</map>
</entry>
</map>
</property>
<property name="map2" ref="map01"></property>
</bean>
<util:map id="map01">
<entry key="key01" value="mp01"></entry>
<entry key="key02" value="mp02"></entry>
<entry key="key03" value="mp03"></entry>
<entry key="key04" value="mp04"></entry>
</util:map>
继续运行测试testStudentTest4(),map的数据就可以被引出来了:
对数据进行修改(以student示例):
<bean id="student" class="com.lean_spring.entity.Student">
<property name="gender" value="男"></property>
<property name="intro" value="shuai"></property>
<property name="salary" value="10.00"></property>
<property name="ID" value="100"></property>
<property name="name" value="name"></property>
<property name="computer" ref="computer"></property>
<property name="computer.brand" value="shako"></property>
</bean>
运行testStudentTest4()后:
可以看到student2与student4中computer的brand数据都被修改了。
构造器注入
首先在类中写入无常构造
package com.lean_spring.entity;
public class Student {
private Integer ID;
private String name;
private char gender;
private Double salary;
private String intro;
private Computer computer;
public Student() {
}
public Student(Integer ID, String name, char gender, Double salary, String intro, Computer computer) {
this.ID = ID;
this.name = name;
this.gender = gender;
this.salary = salary;
this.intro = intro;
this.computer = computer;
}
public Computer getComputer() {
return computer;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
@Override
public String toString() {
return "Student{" +
"ID=" + ID +
", name='" + name + '\'' +
", gender=" + gender +
", salary=" + salary +
", intro='" + intro + '\'' +
", computer=" + computer +
'}';
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
}
在IOC中使用<constructor-arg定义
<bean id="computer" class="com.lean_spring.entity.Computer">
<property name="id" value="dll"></property>
<property name="brand" value="dsy"></property>
</bean>
<bean id="student2" class="com.lean_spring.entity.Student">
<constructor-arg name="ID" value="1000"></constructor-arg>
<constructor-arg name="name" value="name"></constructor-arg>
<constructor-arg name="gender" value='s'></constructor-arg>
<constructor-arg name="salary" value="1000"></constructor-arg>
<constructor-arg name="intro" value="评价"></constructor-arg>
<constructor-arg name="computer" ref="computer"></constructor-arg>
</bean>
值得注意的是这里的computer对象使用ref标签可以指向注入它即可。
我们还可以利用bean来为数据注入对象:
<bean id="student3" class="com.lean_spring.entity.Student">
<constructor-arg name="ID" value="1000"></constructor-arg>
<constructor-arg name="name" value="name"></constructor-arg>
<constructor-arg name="gender" value='s'></constructor-arg>
<constructor-arg name="salary" value="1000"></constructor-arg>
<constructor-arg name="intro" value="评价"></constructor-arg>
<constructor-arg name="computer"><bean id="computer" class="com.lean_spring.entity.Computer">
<property name="id" value="dll"></property>
<property name="brand" value="dsy"></property>
</bean></constructor-arg>
</bean>
在测试中运行:
@Test
public void testStudentTest3(){
Student student3=app.getBean("student3",Student.class);
System.out.println(student3);
}
依然可以得到computer的数据:
注解注入
大部分使用注解注入,相互依赖,需要开启自动扫描:
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.lean_spring.entity"/>
bean的作用域
prototype表示多实例,每次获取对线的时候,IOC会创建一个对象
创建一个类C用于测试:
package com.lean_spring.entity;
public class C {
public C() {
}
}
在IOC中添加关于C的定义:
<bean id="c" class="com.lean_spring.entity.C" scope="prototype"></bean>
在测试中添加testC():
@Test
public void testC(){
C c=app.getBean("c",C.class);
System.out.println(c);
C c2=app.getBean("c",C.class);
System.out.println(c2);
}
运行后可以看到:
两个对象并不是同一个
singleton单实例,默认的,每次容器启动的时候,就会创建一个对象,每次获取的时候,都是获取这个对象
修改IOC中的scope属性就可开始测试:
<bean id="c" class="com.lean_spring.entity.C" scope="singleton"></bean>
再次运行测试:
两个c,c2是相同的一个对象。
request在web环境下,每次请求都会创建一个对象
session在web环境下,每一个会话周期会创建一个对象
bean的自动装配
autowire自动装配
default 默认值,关闭自动装配,使用手动装配
新建两个D,E类进行测试
package com.lean_spring.entity;
public class D {
private E e;
@Override
public String toString() {
return "D{" +
"e=" + e +
'}';
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
}
package com.lean_spring.entity;
public class E {
private String name;
@Override
public String toString() {
return "E{" +
"name='" + name + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在IOC容器中来声明:
<bean id="d" class="com.lean_spring.entity.D" autowire="default">
</bean>
<bean id="e" class="com.lean_spring.entity.E">
<property name="name" value="eeee"></property>
</bean>
在进行测试
@Test
public void testDE(){
D d=app.getBean("d",D.class);
System.out.println(d);
}
}
可以发现其运行出的是空值
byname通过bean的名称进行自动装配,要求名称与bean的名称一致
更改IOC中d的autowire标签
<bean id="d" class="com.lean_spring.entity.D" autowire="byName">
</bean>
<bean id="e" class="com.lean_spring.entity.E">
<property name="name" value="eeee"></property>
</bean>
再次运行textSE():
e中的数据已经被装配好了
bytype通过类型来进行自动装配,要求同一个类型在IOC容器中只能有一个
同样修改IOC后运行依然可以装配
如果IOC中E类型有2个以上那么bean会报错:
通过构造器进行注入
先在D类中加入构造器
package com.lean_spring.entity;
public class D {
private E e;
public D() {
}
public D(E e) {
this.e = e;
}
@Override
public String toString() {
return "D{" +
"e=" + e +
'}';
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
}
constructor先通过名称来查找,然后对照类型进行查找,当有两个形同类型时不会报错
<bean id="d" class="com.lean_spring.entity.D" autowire="constructor">
</bean>
<bean id="e" class="com.lean_spring.entity.E">
<property name="name" value="eeee"></property>
</bean><bean id="e2" class="com.lean_spring.entity.E">
<property name="name" value="eeee2"></property>
</bean>
运行textDE():
Autowire 自动装配根据类型实现自动装配
Controller 控制器,推荐给控制器层的组件使用,相当于servlet
Service业务逻辑层推荐给业务逻辑层的组件使用,也是service
Repository 持久化层推荐给数据库继承的组件使用,就是dao层
Component 组件,给上述之外的其他层使用
|