BeanFactory and IOC control inversion, beanfactoryioc

Source: Internet
Author: User

BeanFactory and IOC control inversion, beanfactoryioc

I was reading spring and reading IOC in the cloud, including seeing AOP in the cloud. Later I learned Java Web and made a simple web project, after that, I watched the video from instructor Cui xifan. The content of Day27 and Day28 is really necessary and important!

Here I will first explain my understanding of IOC control inversion:

Previously, all the objects we created were generated through a new object, for example:

Student st = new Student();

The constructor is called to create an object. However, a better method is to create an object in the factory mode. The specific content is as follows:

 

1. Create the beans. xml file and add the following Configuration:

<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>    

This is a Student class. All member variables are declared using the <property> </property> label.

Create an object through bean Factory:

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

Add the scope attribute in the <bean> label to beans. xml. If the scope value is singleton, It is the singleton mode. If the scope value is prototype, it is the multi-sample mode.

 

2. If you want to associate two objects, you can add the ref tag under the Property tag to indicate the Property value:

<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"/> <! -- The ref value must be the id of another been --> </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>

That is, the objects obtained in the factory have completed the Assembly function.

 

3. However, the classes under the above domain will not be configured in this way. This method is often applied to Dao, Service, and Action. Decoupling is achieved through interface-oriented programming.

Create the Dao interface under the Dao package and create two implementation classes for this interface:

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()");    }}

Configure in the beans. xml file:

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

To use the second implementation method of this interface, you only need to change the className value in the configuration file:

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

To use these two objects, you only need to convert the bean to the interface type:

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

This ensures that you do not need to change the code when changing the implementation class. Directly call the methods in the interface.

 

4. Create a Service interface and its implementation class under the Service package at the same time:

Previously, we had to create a Dao object in the Service before using the method in Dao, for example:

Private StudentDao studentDao = new StudentDao();

However, through BeanFactory, you do not need to create in it, but who calls the service method, who needs to call the method for creating Dao first:

Public interface StudentService {void login ();} public class StudentServiceImpl implements StudentService {private StudentDao studentDao = null; // whoever calls the service method needs to call this method first, provides dao public void setStudentDao (StudentDao studentDao) {this. studentDao = studentDao;} public void login () {studentDao. add (null); studentDao. update (null );}}

In the configuration file, perform the following configurations:

<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>

Assembly can be realized.

The above is what I personally call IOC to transfer the object creation and assembly functions to the container for completion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.