zoukankan      html  css  js  c++  java
  • 1.Spring框架入门案例

    一、简单入门案例

    入门案例:IoC

    1.项目创建与结构

    2.接口与实现类

    User.java接口
    package com.jd.ioc;
    
    /**
     * @author weihu
     * @date 2018/8/8/008 22:29
     * @desc 用户接口
     */
    public interface User {
        void addUser();
    }

    UserImpl.java实现类
    package com.jd.ioc.impl;
    
    import com.jd.ioc.User;
    
    /**
     * @author weihu
     * @date 2018/8/8/008 22:30
     * @desc 用户实现类
     */
    public class UserImpl implements User {
        @Override
        public void addUser() {
            System.out.println("add user!");
        }
    }

    xml配置文件

    beans.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">
        <!-- 配置service
            <bean> 配置需要创建的对象
                id :用于之后从spring容器获得实例时使用的
                class :需要创建实例的全限定类名
        -->
        <bean id="userServiceId" class="com.jd.ioc.impl.UserImpl"></bean>
    </beans>

    测试类

    UserTest.java

    package com.jd.test;
    
    import com.jd.ioc.User;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    /**
     * @author weihu
     * @date 2018/8/8/008 22:33
     * @desc ioc测试类
     */
    public class UserTest {
    
        @Test
        public void testUser(){
            //1.加载配置文件
            String xmlPath= "com/jd/xml/beans.xml";
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
            //根据id获取bean对象
            User user = (User) applicationContext.getBean("userServiceId");
            user.addUser();
    
        }
    }
  • 相关阅读:
    Transact_SQL小手册(各种sql语句大集合)
    矮人DOS工具箱 使用说明
    window.showModalDialog以及window.open用法简介 (转)
    正则表达式(转)
    Ajax.net用户指南(转)
    Java相关的开源GIS系统
    数据库操作之ODBC
    编译第一个OSG程序时候需要注意的
    OSG编译
    VC 多线程编程(转)
  • 原文地址:https://www.cnblogs.com/weihu/p/9446318.html
Copyright © 2011-2022 走看看