一、首先看下源码结构

二、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>
五、测试结果截图:

Spring Ioc (this is my first example)
原文:https://www.cnblogs.com/northeastTycoon/p/9855843.html