标签:pojo idt address ref 底层原理 rri size book err
控制反转,把创建对象的过程交给Spring进行管理。
控制反转,把创建对象和对象之间的调用过程,交给Spring进行管理。
使用IOC的目的,降低耦合度。
xml解析
工厂模式
反射
IOC思想基于IOC容器完成,IOC容器底层就是对象工厂。
Spring提供IOC容器实现两种方式(两个接口)
IOC容器基本实现,是Spring内部使用的接口,不提供开发人员进行使用。(加载配置文件 的时候不会创建对象,在获取对象(使用)的时候才去创建对象。)
BeanFactory接口的子接口。提供更多更强大的功能,一般由开发人员进行使用(加载配置 文件时候就会把在配置文件对象进行创建。)
IOC操作Bean管理(基于xml方式)
public class Book {
private String bname;
private String bauthor;
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
@Override
public String toString() {
return "Book{" +
"bname=‘" + bname + ‘\‘‘ +
", bauthor=‘" + bauthor + ‘\‘‘ +
‘}‘;
}
}
<bean id="book" class="cn.xupengzhuang.spring5.pojo.Book">
<property name="bname" value="五年高考"></property>
<property name="bauthor" value="张三"></property>
</bean>
public class Order {
//属性
private String oname;
private String address;
//有参数构造
public Order(String oname,String address) {
this.oname = oname;
this.address = address;
}
@Override
public String toString() {
return "Order{" +
"oname=‘" + oname + ‘\‘‘ +
", address=‘" + address + ‘\‘‘ +
‘}‘;
}
}
<bean id="order" class="cn.xupengzhuang.spring5.pojo.Order">
<constructor-arg name="oname" value="电脑"></constructor-arg>
<constructor-arg name="address" value="china"></constructor-arg>
</bean>
null值
属性含有特殊符号
<bean id="book" class="cn.xupengzhuang.spring5.pojo.Book">
<property name="bname">
<value><![CDATA[<<Java编程思想>>]]></value>
</property>
<property name="bauthor">
<null/>
</property>
</bean>
public class UserDao {
public void add(){
System.out.println("dao add");
}
}
public class UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add");
userDao.add();
}
}
<bean id="userDao" class="cn.xupengzhuang.spring5.dao.UserDao"></bean>
<bean id="userService" class="cn.xupengzhuang.spring5.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
public String getDname() {
return dname;
}
@Override
public String toString() {
return "Dept{" +
"dname=‘" + dname + ‘\‘‘ +
‘}‘;
}
}
public class Emp {
private String ename;
private String gender;
private Dept dept;
public String getEname() {
return ename;
}
public String getGender() {
return gender;
}
public Dept getDept() {
return dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp{" +
"ename=‘" + ename + ‘\‘‘ +
", gender=‘" + gender + ‘\‘‘ +
", dept=" + dept +
‘}‘;
}
}
<bean id="emp" class="cn.xupengzhuang.spring5.pojo.Emp">
<property name="ename" value="jack"></property>
<property name="gender" value="男"></property>
<property name="dept">
<bean id="dept" class="cn.xupengzhuang.spring5.pojo.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
<!--级联赋值-->
<bean id="emp" class="cn.xupengzhuang.spring5.pojo.Emp">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="cn.xupengzhuang.spring5.pojo.Dept">
<property name="dname" value="财务部"></property>
</bean>
第二种写法
<!--级联赋值-->
<bean id="emp" class="cn.xupengzhuang.spring5.pojo.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技术部"></property>
</bean>
<bean id="dept" class="cn.xupengzhuang.spring5.pojo.Dept">
<property name="dname" value="财务部"></property>
</bean>
标签:pojo idt address ref 底层原理 rri size book err
原文地址:https://www.cnblogs.com/beanbag/p/13393968.html