zoukankan      html  css  js  c++  java
  • Java第三十八天,Spring框架系列,基于XML的IOC环境搭建

    1.新建Maven项目

    2.在Pom.xml文件中添加如下代码,指定打包方式

    <packaging>jar</packaging>
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.huhai</groupId>
        <artifactId>demo12</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
    
    </project>

    3.在pom.xml文件中继续加入以下内容,导入spring依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
    </dependencies>
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.huhai</groupId>
        <artifactId>demo12</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.0.RELEASE</version>
            </dependency>
        </dependencies>
    
    
    </project>

    4.在resources目录下新建bean.xml(文件名可随意)配置文件;该文件的主要用途可以参考上篇博文的bean.properties文件,是用来为工厂类提供关键信息的

    配置文件具体可参考相关开发文档的如下路径:

    spring-framework-5.0.2/spring-framework-5.0.2.RELEASE-docs/spring-framework-reference/core.html#spring-core

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

    5.在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">
    
        <!--把对象的创建交给spring来管理-->
        <bean id="accountServiceImpl" class="com.huhai.Service.Impl.AccountServiceImpl"></bean>
        <bean id="accountDaoImpl" class="com.huhai.Dao.Impl.AccountDaoImpl"></bean>
    </beans>

    6.项目部署

    7.持久层接口代码

    package com.huhai.Dao;
    
    /**
     *持久层接口
     */
    public interface IAccountDao {
        public abstract void save();
    }
    

    8.持久层接口实现类代码

    package com.huhai.Dao.Impl;
    
    import com.huhai.Dao.IAccountDao;
    
    /**
     * 持久层实现类
     */
    public class AccountDaoImpl implements IAccountDao {
        public void save() {
            System.out.println("数据保存成功");
        }
    }
    

    9.业务层接口代码

    package com.huhai.Service;
    
    /**
     *业务层接口
     */
    public interface IAccountService {
        public abstract void save();
    }

    10.业务层接口实现类代码

    package com.huhai.Service.Impl;
    
    import com.huhai.Dao.IAccountDao;
    import com.huhai.Service.IAccountService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    /**
     * 业务层实现类
     */
    public class AccountServiceImpl implements IAccountService {
    
        public void save() {
            /**
             * 获取Spring的IOC核心容器
             * 根据ID获取对象
             * 因为bean.xml配置文件在根资源目录下,所以不需要指定路径
             */
            //可以加载类路径下的配置文件,且配置文件必须在类路径下
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    
            /**可以加载磁盘任意位置的配置文件,且必须有访问权限
            FileSystemXmlApplicationContext
             */
    
            /**用于读取注解创建容器的
            AnnotationConfigApplicationContext
             */
    
            //自己进行强转
            //IAccountService as = (IAccountService) ac.getBean("accountServiceImpl");
            //传入class字节码文件让程序帮自己强转
            IAccountDao as = ac.getBean("accountDaoImpl", IAccountDao.class);
            as.save();
        }
    }
    

    11.表现层代码

    package com.huhai;
    
    import com.huhai.Service.IAccountService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Realize {
    
        public static void main(String[] args) {
            /**
             * 获取Spring的IOC核心容器
             * 根据ID获取对象
             * 因为bean.xml配置文件在根资源目录下,所以不需要指定路径
             */
    
            /**可以加载类路径下的配置文件,且配置文件必须在类路径下
             * 使用项目相对路径,推荐使用
             */
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    
            /**可以加载磁盘任意位置的配置文件,且必须有访问权限
             * 得使用绝对路径,不推荐部署服务器时使用
             * FileSystemXmlApplicationContext
             */
    
            /**用于读取注解创建容器的
             * AnnotationConfigApplicationContext
             */
    
            //自己进行强转
            //IAccountService as = (IAccountService) ac.getBean("accountServiceImpl");
            //传入class字节码文件让程序帮自己强转
            IAccountService as = ac.getBean("accountServiceImpl", IAccountService.class);
            as.save();
        }
    }
    
    作者:蓝月

    -------------------------------------------

    个性签名:能我之人何其多,戒骄戒躁,脚踏实地地走好每一步

  • 相关阅读:
    node.js ---path模块
    es6箭头函数this问题
    Codeforces Round #576 (Div. 2) | CF1199 补题
    Hungary
    CF 1196D2 RGB Substring (hard version) --- 前缀和 + 思维
    康托展开
    POJ1821 Fence --- 单调队列 + DP
    素数筛
    自动化接口面试遇到的问题
    linux遇到的面试问题
  • 原文地址:https://www.cnblogs.com/viplanyue/p/13573754.html
Copyright © 2011-2022 走看看