1. Add Spring's Libraries to the project :
Right click on the project---->Properties---->Java Build Path---->Libraries tab---->Add Library---->MyEclipse Libraries---->Spring xx Core Libraries . What I added here is version 2.5.
2. Create a new Spring configuration file under src: applicationContext.xml , and create a new Date bean for testing.
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="date" class="java.util.Date"/> </beans>
3. Create a new unit test class for testing :
package com.wjl.junit; import java.util.Date; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Junit_demo_11 * spring unit test * **/ public class SpringTest { private static ApplicationContext context = null; @BeforeClass public static void setUpBeforeClass() throws Exception { context = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void test(){ Date date = (Date)context.getBean("date"); System.out.println(date);//Result: Tue Jan 16 10:03:21 CST 2018, indicating that spring has been added to the project } }
Indicates that Spring has been added successfully.