zoukankan      html  css  js  c++  java
  • Spring Ioc (this is my first example)

    一、首先看下源码结构

       

    二、HelloWord 类

         

    package com.northeasttycoon.bean;
    
    /**
     * 打印出 helloword 参数值
     * 
     * @author tycoon jpa规范,hibernate是对它的一个实现
     */
    public class HelloWord {
    
        // 普通方法
        public void sayHello() {
            
            System.out.println("Hello Word!");
        }
    }

    三、测试类,HelloWordTest源码介绍

      

    /**
     * 
     */
    package com.northeasttycoon.bean;
    
    // gradle 依赖关系插件
    import org.junit.Test;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author northEastTycoon
     *
     */
    public class HelloWordTest {
    
    	@Test
    	public void test() {
    
    		// Ioc 容器一种,使用依赖注入方式获得被调用者信息.无需关心被调用对象的变更信息.
    		// spring容器的一种,从spring中获得了相关对象.
    		// 容器提供生命周期及查找功能
    		BeanFactory  context = new ClassPathXmlApplicationContext("applicationContext.xml");
    	//	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    		HelloWord hello = (HelloWord) context.getBean("helloword");
    		hello.sayHello();
    	}
    }
    

     四、配置文件 applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    	<bean name="helloword" class="com.northeasttycoon.bean.HelloWord"/>
    </beans>
    

    五、测试结果截图:

  • 相关阅读:
    layui的模块化和非模块化使用
    layui实现类似于bootstrap的模态框功能
    ajax下载文件
    【IDEA】IDEA中maven项目pom.xml依赖不生效解决
    主-主数据库系统架构
    MyEclipse x.x各版本终极优化配置指南
    Cactus入门
    有史以来最出彩的编程语言名字
    安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果
    第一次讲课总结
  • 原文地址:https://www.cnblogs.com/northeastTycoon/p/9855843.html
Copyright © 2011-2022 走看看