BeanFactory和IOC控制反转

  之前在看spring,看IOC实在是云里雾里,包括看AOP也是云里雾里的,后来重新学习Java Web,做了一个简单的web项目,再之后看了崔希凡老师的视频,Day27和Day28两天的内容,真的很有必要,很重要!

这里先说一下我对IOC控制反转的理解:

之前我们创建对象,都是通过new一个对象来生成的,比如:

Student st = new Student();

就调用构造器来进行对象的创建,但是有更好的方法,就是通过工厂方式来创建对象,具体的内容如下:

1.创建beans.xml文件并添加如下的配置:

<beans>
    <bean id="stu1" className="cn.seu.domain.Student">
        <property name="number" value="1001"/>
        <property name="name" value="zhangSan"/>
        <property name="age" value="29"/>
        <property name="sex" value="male"/>
    </bean>
</beans>    

这就是一个Student类,其中所有的成员变量通过<property></property>标签来声明。

通过bean工厂创建对象:

BeanFactory bf = new BeanFactory(“beans.xml”);
Student s1= (Student)bf.getBean(“stu1”);

在beans.xml中添加<bean>标签中的scope属性,其中如果scope的值为singleton,就是单例模式,如果scope的值为prototype,就是多例模式。

2.如果希望实现两个对象有关联关系,在Property标签下可以添加ref标签来指示属性的值:

<bean id="stu1" className=" cn.seu.domain.Student ">
        <property name="number" value="1001"/>
        <property name="name" value="zhangSan"/>
        <property name="age" value="29"/>
        <property name="sex" value="male"/>
        <property name="teacher" ref="t1"/><!-- ref的值必须是另一个been的id -->
</bean>
    
<bean id="t1" className=" cn.seu.domain.Teacher">
    <property name="tid" value="TEACHER_2001" />
    <property name="name" value="liSi" />
    <property name="salary" value="1234.56" />
</bean>

即在工厂中得到的对象已经完成了装配的功能.

3.但是上述这种domain下面的类往往不会通过这种方式来进行配置,这样的方式往往应用在Dao,Service和Action中。通过面向接口编程实现解耦。

在Dao包下创建Dao的接口,并创建该接口的两个实现类:

public interface StudentDao {
    void add(Student stu);
    void update(Student stu);
}

public class StudentImpl implements StudentDao {
    @Override
    public void add(Student stu) {
        System.out.println("StudentImpl.add()");
    }
    @Override
    public void update(Student stu) { 
        System.out.println("StudentImpl.update()");
    }
}

public class StudentImpl2 implements StudentDao {
    @Override
    public void add(Student stu) {
        System.out.println("StudentImp2.add()");
    }
    @Override
    public void update(Student stu) { 
        System.out.println("StudentImp2.update()");
    }
}

在beans.xml文件中进行配置:

<bean id="stuDao" className="cn.seu.dao.impl.StudentImpl1">
</bean>

如果想使用该接口的第二种实现方式只需要在配置文件中改变className的值即可:

<bean id="stuDao" className="cn.seu.dao.impl.StudentImpl2">
</bean>

在使用的时候只需要将得到的bean转换成该接口类型就可以实现对这两个对象的使用:

BeanFactory bf = new BeanFactory("beans.xml");
StudentDao stuDao = (StudentDao)bf.getBean("stuDao");
stuDao.add(null);
stuDao.update(null);

保证了换实现类的时候并不需要改变代码。直接调用接口中的方法。

4.同时在Service包下创建Service接口及其实现类:

原来我们在Service中首先要创建Dao对象,才能使用Dao中的方法,如:

Private StudentDao studentDao = new StudentDao();

但是通过BeanFactory可以不需要在其中创建,而是谁调用service方法,谁就需要先调用创建Dao的方法:

public interface StudentService {
    void login();
}

public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao = null;
    // 谁调用service方法,谁就需要先调用本方法,提供dao
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    public void login() {
        studentDao.add(null);
        studentDao.update(null);
    }
}

在配置文件中需要进行相关配置:

<bean id="stuDao" className="cn.itcast.dao.impl.StudentImpl2">
</bean>
    
<bean id="stuService" className="cn.itcast.service.impl.StudentServiceImpl">
    <property name="studentDao" ref="stuDao"/>
</bean>

即可实现装配。

上述就是我个人对于IOC所谓的将对象的创建和装配功能转交给容器来完成。

原文地址:https://www.cnblogs.com/winterfells/p/8616392.html