• Spring基于xml的环境搭建


    创建pom.xlm文件

    <?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.itheima</groupId>
        <artifactId>day01_eesy_03spring</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>
        </dependencies>
    </project>
    

    codeStyleConfig.xml

    <component name="ProjectCodeStyleConfiguration">
      <state>
        <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
      </state>
    </component>
    

    Serivice /IaccountService.java

    package com.itheima.service;
    
    /**
     * 账户业务层的接口
     */
    public interface IAccountService {
    
        /**
         * 模拟保存账户
         */
        void saveAccount();
    }
    

    serivice / impl / IAccountServiceilpm.java

    package com.itheima.service.impl;
    
    import com.itheima.dao.IAccountDao;
    import com.itheima.service.IAccountService;
    
    /**
     * 账户的业务层实现类
     */
    public class AccountServiceImpl implements IAccountService {
    
        private IAccountDao accountDao ;
    
        public AccountServiceImpl(){
            System.out.println("对象创建了");
        }
    
        public void  saveAccount(){
            accountDao.saveAccount();
        }
    }
    

    dao /IaccontDao.java

    package com.itheima.dao;
    
    /**
     * 账户的持久层接口
     */
    public interface IAccountDao {
    
        /**
         * 模拟保存账户
         */
        void saveAccount();
    }
    

    dao / impl / IAccontDaoImpl.java

    package com.itheima.dao.impl;
    
    import com.itheima.dao.IAccountDao;
    
    /**
     * 账户的持久层实现类
     */
    public class AccountDaoImpl implements IAccountDao {
    
        public  void saveAccount(){
    
            System.out.println("保存了账户");
        }
    }
    

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

    ui /client.java

    package com.itheima.ui;
    
    import com.itheima.dao.IAccountDao;
    import com.itheima.service.IAccountService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * 模拟一个表现层,用于调用业务层
     */
    public class Client {
         * @param args
         */
        public static void main(String[] args) {
            //1.获取核心容器对象
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    //        ApplicationContext ac = new FileSystemXmlApplicationContext("C:\Users\zhy\Desktop\bean.xml");
            //2.根据id获取Bean对象
            IAccountService as  = (IAccountService)ac.getBean("accountService");
            IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
    
            System.out.println(as);
            System.out.println(adao);
            as.saveAccount();
    
    
            //--------BeanFactory----------
    //        Resource resource = new ClassPathResource("bean.xml");
    //        BeanFactory factory = new XmlBeanFactory(resource);
    //        IAccountService as  = (IAccountService)factory.getBean("accountService");
    //        System.out.println(as);
        }
    }
    

    获取spring的Ioc核心容器,并根据id获取对象

    ApplicationContext的三个常用实现类

    • ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用)

    • FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)

    • AnnotationConfigApplicationContext:它是用于读取注解创建容器的,是明天的内容。

      核心容器的两个接口引发出的问题

    • ApplicationContext: 单例对象适用 采用此接口

      它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。

    • BeanFactory: 多例对象使用

      它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。

    总目录

  • 相关阅读:
    动态生成java、动态编译、动态加载
    boostrap标签
    Java后端WebSocket的Tomcat实现
    编程过程与规范
    敏捷开发过程
    软件过程模型
    软件过程
    ISO9126 质量模型
    手机淘宝架构演化实践
    蒋勋:知识分子应具备怎样的美学修养
  • 原文地址:https://www.cnblogs.com/Lilwhat/p/12570395.html
Copyright © 2020-2023  润新知