当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

Spring3: How to set up dependency jars for basic IoC function

发表于: 2013-04-17   作者:DavyJones2010   来源:转载   浏览:
摘要: 1. Import relevant jars for basic IoC purpose         1) Download spring-framework-3.2.2.RELEASE-dist.zip from http://www.springsource.org/download/community     &

1. Import relevant jars for basic IoC purpose

        1) Download spring-framework-3.2.2.RELEASE-dist.zip from http://www.springsource.org/download/community

        2) Open eclipse and set up user library

        3) Import relevant jars including:

            (1) spring-core-3.2.2.RELEASE.jar

            (2) spring-beans-3.2.2.RELEASE.jar

            (3) spring-expression-3.2.2.RELEASE.jar

            (4) spring-context-3.2.2.RELEASE.jar

        4) Download commons-logging-1.1.2-bin.zip from http://commons.apache.org/proper/commons-logging/download_logging.cgi

        5) Import relevant jars including:

            (1) commons-logging-1.1.2.jar

 

2. Create new java project and import user library created before. And import JUnit4 lib for unit test.

3. Start coding:

        1) beans.xml [In the root directory of src folder]

 

<?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-3.0.xsd">

  <bean id="student" class="edu.xmu.domain.Student">
    <!-- collaborators and configuration for this bean go here -->
    <property name="id" value="1"/>
    <property name="name" value="Davy"/>
    <property name="age" value="23"/>
  </bean>
</beans>

         2) POJO

package edu.xmu.domain;

public class Student {
	private Integer id;
	private String name;
	private Integer age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

         3) Test case

package edu.xmu.domain;

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

public class StudentTest
{

	@Test
	public void test()
	{
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

		Student student = (Student) ac.getBean("student");

		System.out.println("Student info: ");
		System.out.println("ID: " + student.getId() + ", Name: "
				+ student.getName() + ", Age: " + student.getAge());
	}

}

Spring3: How to set up dependency jars for basic IoC function

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号