Spring-factory class structure

1.1 Project structure

1.2 Illustration of factory architecture

1.3 Interface difference

The difference between BeanFactory and ApplicationContext

BeanFactory是顶层接口,ApplicationContext是子接口。

The biggest difference between them isThe time to create the object is different

  • BeanFactory uses the idea of ​​lazy loading. That is, when to use the object and when to create it .
  • ApplicationContext adopts the idea of ​​immediate creation.That is, as soon as the configuration file is loaded, it is created immediately

1.4 Code example

Persistence layer (Dao layer)

dao interface

package cn.guardwhy.dao;
/**
 * 客户dao接口
 */
public interface CustomerDao {
    
    
    /**
     * 保存客户操作
     */
    void saveCustomer();
}

dao implementation class

package cn.guardwhy.dao.impl;

import cn.guardwhy.dao.CustomerDao;

/**
 * 客户dao实现类
 */
public class CustomerDaoImpl implements CustomerDao {
    
    
    @Override
    public void saveCustomer() {
    
    
        System.out.println("保存客户操作");
    }
}

Service

service interface

package cn.guardwhy.service;
/**
 * 客户service接口
 */
public interface CustomerService {
    
    
    /**
     * 保存客户操作
     */
    void saveCustomer();
}

service implementation class

package cn.guardwhy.service.impl;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.dao.impl.CustomerDaoImpl;
import cn.guardwhy.service.CustomerService;
/**
 * 客户service实现类
 */
public class CustomerServiceImpl implements CustomerService {
    
    
    // 定义客户dao
    private CustomerDao customerDao = new CustomerDaoImpl();

    /**
     * 保存客户操作
     */
    @Override
    public void saveCustomer() {
    
    
        customerDao.saveCustomer();
    }
}

Write bean.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置service,说明:
       标签:
           bean:配置javaBean对象
       属性:
           id:bean的唯一标识名称
           class:类的全路径信息
       细节:默认使用无参数构造方法,创建对象
    -->
    <bean id="customerService" class="cn.guardwhy.service.impl.CustomerServiceImpl"></bean>

    <!--配置dao-->
    <bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl"></bean>
</beans>

Presentation layer (Controller)

ApplicationContext

package cn.guardwhy.controller;

import cn.guardwhy.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {
    
    
    public static void main(String[] args) {
    
    
        /**
          BeanFactory与ApplicationContext区别:
          1.BeanFactory是顶层接口
          2.ApplicationContext是子接口
          3.它们的区别是创建对象的时间点不一样:
            a、BeanFactory采用延迟加载的思想。即什么时候使用对象,什么时候创建
            b、ApplicationContext采用立即创建的思想。即一加载spring配置文件,立即就创建对象
         */

        // 1.加载spring配置文件,创建spring ioc容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // 2.打印容器创建完成的信息
        System.out.println("-----------------start");
        System.out.println("spring IOC容器创建好了");
        System.out.println("----------------end");
        // 2.从容器中获取客户service对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");
        // 3.保存客户
        customerService.saveCustomer();
    }
}

Results of the

Presentation layer (Controller)

BeanFactory

package cn.guardwhy.controller;

import cn.guardwhy.service.CustomerService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * 客户controller
 */
public class CustomerController {
    
    
    public static void main(String[] args) {
    
    
        /**
          BeanFactory与ApplicationContext区别:
          1.BeanFactory是顶层接口
          2.ApplicationContext是子接口
          3.它们的区别是创建对象的时间点不一样:
            a、BeanFactory采用延迟加载的思想。即什么时候使用对象,什么时候创建
            b、ApplicationContext采用立即创建的思想。即一加载spring配置文件,立即就创建对象
         */

       // 1.创建资源对象
        Resource resource = new ClassPathResource("bean.xml");
        BeanFactory context = new XmlBeanFactory(resource);

        // 2.打印容器创建的信息
        System.out.println("------------------start");
        System.out.println("spring IOC容器创建好了");
        System.out.println("-----------------end");

        // 3.从容器中获取客户service对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");
        // 4.保存客户
        customerService.saveCustomer();
    }
}

Results of the

Guess you like

Origin blog.csdn.net/hxy1625309592/article/details/114969247