二、从工厂模式到Spring的分析(二)

现在接上一章从工厂模式到Spring的分析(一)

我们现在来开始写spring的xml配置文件,先来分析一下:

  • 对于我们先前的工厂模式,自己在工厂里面来通过反射创建对象,spring核心容器通过它的配置文件帮我们做了封装,我们用id来指定唯一标志,class来指定类路径
    pom依赖
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>

spring配置文件

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

    <bean id="userService" class="com.lp.service.impl.UserServiceImpl"/>
    <bean id="userDao" class="com.lp.dao.impl.UserDaoImpl"/>

</beans>
  • 好了,上一步我们已经通过spring帮给我们创建好了对象,放在容器里面,现在我们来测试一下
package com.lp.ui;

import com.lp.service.UserService;
import com.lp.service.impl.UserServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @Date 2020/5/26 21:21
 * @Author luopeng
 */
public class test {
    public static void main(String[] args) {
        //获取核心容器对象
        //BeanFactory bean = new ClassPathXmlApplicationContext("bean.xml");
        ApplicationContext bean = new ClassPathXmlApplicationContext("bean.xml");
        //ApplicationContext bean = new FileSystemXmlApplicationContext("计算机的绝对路径");
        //根据id获取bean对象
        UserService userService = bean.getBean("userService", UserService.class);
        userService.saveUser();
    }
}

现在我们来分析一下这个类:
  • 首先我们要获取这个核心容器,然后通过id来获取bean对象,这都很好理解!
  • 获得了这个bean对象后我们就可以对他进行操作了
我们来分析一下我代码中写到的几句不同代码的区别:
  • BeanFactory和ApplicationContext 之间对于ApplicationContext 来说他是立即加载的机制,适用于单例创建对象,另一个则是延迟加载,适用于多例!我们通常使用ApplicationContext的单例 。
  • ClassPathXmlApplicationContext和FileSystemXmlApplicationContext之间FileSystemXmlApplicationContext是当前计算机的绝对路径,ClassPathXmlApplicationContext是类路径下的位置,这两者很好选择!
我们来分析一下创建bean的三种方式

配置文件中有注释

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

<!--    第一种创建bean的方法,直接通过默认构造函数来创建对象-->
<!--    <bean id="userDao" class="com.lp.dao.impl.UserDaoImpl"/>-->

<!--    第二种:通过userService中的方法getUserDao来创建-->
    <!--
    <bean id="userService" class="com.lp.service.impl.UserServiceImpl"/>
    <bean id="userDao" factory-bean="userService" factory-method="getUserDao"/>
    -->

<!--    使用静态方法来创建,静态方法可以在一个工厂类中来写-->
    <bean id="userDao" class="com.lp.dao.impl.UserDaoImpl" factory-method="getUserDao"/>

</beans>

文章开头使用的就是第一种,现在我们看一看第二种在userServiceImpl中的代码

package com.lp.service.impl;

import com.lp.dao.UserDao;
import com.lp.dao.impl.UserDaoImpl;
import com.lp.service.UserService;

/**
 * @Date 2020/5/26 21:21
 * @Author luopeng
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao= new UserDaoImpl();

    public UserDaoImpl getUserDao() {
        return new UserDaoImpl();
    }

    public void saveUser() {
        userDao.saveUser();

    }
}

对于第三种方式我们可以在任何一个类中创建一个静态的方法,只需要指明class在哪,静态方法名是什么即可!

再来看一看我们bean的作用域

在bean标签内用scope来指明

  • singleton:单例的(默认就是单例)
  • peototype:多例的
  • request:作用于web应用的请求范围
  • session:作用于web应用的session会话范围
  • global-session:作用于集群环境下的会话范围(全局会话范围),不是集群环境时,就是session
对于global-session,可以这样来理解,集群环境下你有很多个服务器来支撑,如果说你不用全局session的话,如果一个登录页面有验证码,你的验证码其实是在session中的,负载均衡机制会让你去空闲的服务器的session里面拿验证码作比对,而这个过程需要来回的操作,详细讲解一下这个过程:
  • 首先你要登录页面,页面刷新时有一个验证码,这个验证码是你访问页面时服务器A发送过来的,存在于它的session中,这时你访问的是服务器A
  • 当你输入验证码进行校验时,会再次发送请求,如果服务器A不再处于空闲状态,负载均衡机制将会让你访问服务器B,这时问题来了,你先前的验证码存在于服务器A的session中,服务器B怎么来做验证?
  • 此时就需要的是global-session了,即全局session,只有这样全部服务器共享一个session,才能得到数据,不然数据校验就会出现null!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值