3-1springDemo入门用依赖

1.看图

2.业务层接口 IAccountService

package com.itheima.service;

public interface IAccountService {

    void saveAccount();

}

2.业务层实现类IAccountServiceImp

package com.itheima.service.lmp;

import com.itheima.dao.IAccountDao;
import com.itheima.dao.Imp.IAccountDaoImp;
import com.itheima.service.IAccountService;

public class IAccountServiceImp implements IAccountService {
//业务层调用——持久层——调用控制层
 IAccountDao accountDao = new IAccountDaoImp();
    public void saveAccount() {
        accountDao.save();
//        System.out.println("对象被创建了");

    }
}

3.IAccountDao持久层接口

package com.itheima.dao;

public interface IAccountDao {
    void save();
}

4.持久层实现类IAccountDaoImp

package com.itheima.dao.Imp;

import com.itheima.dao.IAccountDao;

public class IAccountDaoImp implements IAccountDao {
    public void save() {
        System.out.println("保存成功");
    }
}

5.pom.xml配置依赖

<dependencies>
<!-- 加入依赖自动导入jar包:前提配好镜像即可-->
<!-- spring容器的环境 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    
  </dependencies>

6.spring容器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">
<!--    id:对象名(也是唯一标识)class:全限定类名-->
<!--    传入的是每个层的实现类,因为使用实现类才能创建实例对象-->
    <bean id="accountService" class="com.itheima.service.lmp.IAccountServiceImp"></bean>
    <bean id="accountDao" class="com.itheima.dao.Imp.IAccountDaoImp"></bean>
</beans>

7.表现层Client

package com.itheima.ui;

import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import com.itheima.service.lmp.IAccountServiceImp;
import javafx.application.Application;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

//模拟一个表现层,用于调用业务层
public class Client {
    /*
    * 获取spring的Ioc核心容器,并且根据id获取对象
    */
/*
*   ApplicationContext的三个常用实现类:
*        ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路劲下,不在的话加载不了
*        FileSystemXmlApplicationContext:它可以加载磁盘任意下的配置文件(必须拥有权限:操作系统的问题)
*        AnnotationConfigApplicationContext:它是用于读取注解创建容器的,是明天的内容
*
*   核心容器的两个接口引发出的问题:
*       ApplicationContext:(立即创建立即销毁:单例) 实际开发中:此接口最常用
*           它在构建核心容器时,创建对象的策略是采用立即加载的方式。也就是说,只要一读取完配置文件立马就会创建 配置文件中 配置的对象
*       BeanFactory:    (什么时候用什么时候创建:多例)
*           它构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,时候根据id获取对象了,什么时候才真正的创建对象
*   实际开发:按需用
*
* */

    public static void main(String[] args) {
        //1.获取核心容器对象(获取bean容器)ac 不管你取什么名都可以,然后使用这个ac去找到bean对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //ApplicationContext ac = new FileSystemXmlApplicationContext("C:\\Users\\bean.xml");


        //2.根据id获取bean对象(这里为什么要用IAccountService,那是遵循三层架构思想[])
        IAccountService as = (IAccountService) ac.getBean("accountService");
        //IAccountDao ado = (IAccountDao) ac.getBean("accountDao");
        //强转
        IAccountDao ado = ac.getBean("accountDao",IAccountDao.class);
        System.out.println("as="+as);
        System.out.println("ado="+ado);
        as.saveAccount();
//————————————————————————————————————————————————————————————
        //BeanFactory:核心容器的配置(过时)
        //Resource resource = new ClassPathResource("bean.xml");
        //BeanFactory factory = new XmlBeanFactory(resource);
// 用的时候才去创建
        //IAccountService as = (IAccountService) factory.getBean("accountService");
        //System.out.println(as);
    }
    /*
    *dao: Client调用IAccountServiceImp调用IAccountDaoImp(创建实例化对象的时候)
    * service:业务返回:IAccountDao ——>IAccountService——>显示Client
    */
    /*IAccountService as = new IAccountServiceImp();普通用法:独立就用spring容器*/
}

8.初步使用【赖依和bean.xml来】创建对象,替代了配置工厂的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值