/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String [] conf = new String[2];
conf[0]="applicationContext.xml";
conf[1]="dataAccessContext-jdbc.xml";
ApplicationContext appContext = new ClassPathXmlApplicationContext(conf);
// UserService us = (UserService) appContext.getBean("userService");
Customer cu = new Customer();
cu.setName("11");
cu.setAddress("aa");
cu.setEmail("ss");
cu.setPhone("ssss");
ICustomerService cs = (ICustomerService) appContext.getBean("customerService");
cs.insert(cu);
System.out.println("hello world!");
}
package com.example.test;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import com.example.domain.Customer;
import com.example.service.ICustomerService;
@ContextConfiguration(locations={"test-customerService.xml"})
public class TestCustomerService extends
AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private ICustomerService cs;
@Test
public void handleUserLogin() {
// User user = new User();
// user.setUserId(1);
// user.setLastIp("127.0.0.1");
// Date now = new Date();
// user.setLastVisit(now.getTime());
// userService.handleUserLogin(user);
Customer cu = new Customer();
cu.setName("11");
cu.setAddress("aa");
cu.setEmail("ss");
cu.setPhone("ssss");
cs.insert(cu);
System.out.println("hello world!");
}
}
在同一目录下放入文件:
<?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-2.5.xsd">
<!-- ① 引入清单1定义的Spring配置文件 -->
<import resource="classpath:/applicationContext.xml" />
<import resource="classpath:/dataAccessContext-jdbc.xml" />
</beans>