Java 2.Spring的Helloword

一、创建web项目

二、引用jar包

三、引入日志配置文件

四、创建Java类

五、创建bean容器

名字和路径没有特殊要求。

ApplicationContext.xml 放在src上

<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>

六、配置bean对象

        <!-- bean: 是容器创建Person的对象 -->
        <!-- name:相当于变量名person p = new person(); -->
        <!-- class: 类的全限定名 -->
        
        <bean name="p" class="com.Spring.pojo.Person"></bean>

 七、测试用例

package com.Spring.pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCTest {
	@SuppressWarnings("resource")
	@Test
	public void testCreatePerson() {
		//创建容器
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		//查找对象
		Person p=(Person) context.getBean("p");
		
		System.out.println(p);
	}

}

猜你喜欢

转载自www.cnblogs.com/yuzhenfu/p/12144398.html