spring 基于xml的ioc环境配置

Spring基于XML的IOC环境搭建

IOC:控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。

JdbcDemo1:

程序的耦合
耦合:程序间的依赖关系

包括:类之间的依赖
方法间的依赖

 解耦:降低程序间的依赖
 
实际开发中:
应该做到,编译期不依赖,运行时才依赖

解耦思路:
第一步,使用反射来创建对象,避免使用new 关键字
第二步,读取配置文件来获取要创建的对象的全限定类名
public class JdbcDemo1 {

    public static void main(String[] args) throws Exception {
        //1.注册驱动
//   1.     DriverManager.registerDriver(new com.mysql.jdbc.Driver());  高依赖   依赖一个具体得驱动类
        Class.forName("com.mysql.jdbc.Driver");  //依赖的只是一个字符串  减少耦合  但是这是一种写死的方式

        //2.获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/eesy","root","80238023");
        //3.获取操作数据库的预处理对象
        PreparedStatement pstm = conn.prepareStatement("select *from account");
        //4.执行SQL,得到结果集
        ResultSet rs = pstm.executeQuery();
        //5.遍历结果集
        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }
        //6.释放资源
        rs.close();
        pstm.close();
        conn.close();
    }
    }

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">
    
    <bean id="accountService" class="com.benti.service.impl.AccountServiceImpl"></bean>
    <bean id="accountDao" class="com.benti.dao.impl.AccountDaoImpl"></bean>
</beans>

Client:(获取xml来创建具体对象)

 获取spring的Ioc容器,并根据id获取对象
 ApplicationContext的三个常用实现类: 
 ClassPathXmLApplicationContext它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。  (更常用)
 FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
 AnnotationConfigApplicationContext:它是用于读取注解创建容器的,是明天的内容。|
 
 核心容器的两个接口引发出的问题:
 ApplicationContext:    单例对象适用          采用此接口
    它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
 BeanFactory:           例对象使用
 	它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
public class Client {
    public static void main(String[] args) {
        //获取核心容器对象
        ApplicationContext ac  = new ClassPathXmlApplicationContext("bean.xml");
//        ApplicationContext ac  = new FileSystemXmlApplicationContext("F:\\spring\\day_01eesy_03spring\\src\\main\\resources\\bean.xml");

        //根据id获取bean对象  //两种写法
        //IAccountServie as =(IAccountServie)ac.getBean("accountService");//强转
        IAccontDao as= ac.getBean("accountService",IAccontDao.class);//传给他一个字节码

        System.out.println(as);
        
  //          as.saveAccount();
    }
}

spring对bean的管理细节

	1.创建bean的三种方式
	2.bean对象的作用范围
	3.bean对象的生命周期

1.创建Bean的三种方式

第一种方式:使用默认构造函数创建。
       在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
       采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
        <bean id="accountService" class="com.benti.service.impl.AccountServiceImpl"></bean>-->
第二种方法:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入bean容器)
<bean id="instanceFactory" class="com.benti.factory.InstanceFactory"></bean>-->
		下面这个 使用了com.benti.factory.InstanceFactory类中的getAccountService方法创建了一个对象
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
第三种方式        :使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)
<bean id="accountService" class="com.benti.factory.StaticFactory" factory-method="getAccountService"></bean>

2.bean的范围调整

    bean标签的scope属性
    作用:用于指定bean的作用范围
    取值:     常用的就是单例的和多例的
    singleton:  单例的(默认值)
    prototype:  多例的
    request:    作用于web应用的请求范围
    session:    作用于web应用的会话范围
    global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
 <bean id="accountService" class="com.benti.service.impl.AccountServiceImpl" scope="prototype"></bean>

3.bean对象的生命周期

 单例对象
	出生:当容器创建时,对象创建
	活着:只要容器还在,对象一直在
	死亡:容器销毁,对象消亡
	总结:单例对象生命周期和容器一致
多例对象
	出生:当我们使用对象时spring框架为我们创建
	活着:对象只要是在使用过程中就一活着
	死亡:当对象长时间不使用,且没有别的对象引用时,由java垃圾回收机制回收
<bean id="accountService" class="com.benti.service.impl.AccountServiceImpl" scope="prototype" init-method="init" destroy-method="destroy"></bean>
<!--   init-method 初始化方法   destroy-method销毁方法 -->
	public void init(){
        System.out.println("对象初始化了......");
    }
    public void destroy(){
        System.out.println("对象销毁了。。。。。");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值