1.導入jar包
commons-logging-1.1.1.jar
spring-beans-3.2.8.RELEASE.jar
spring-core-3.2.8.RELEASE.jar
spring-context-3.2.8.RELEASE.jar
spring-expression-3.2.8.RELEASE.jar
junit4.jar
2.src/bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.spring.study.impl.UserServiceImpl" />
</beans>
3.java代碼
package com.spring.study;
public interface UserService {
public void saveUser();
}
package com.spring.study.impl;
import com.spring.study.UserService;
public class UserServiceImpl implements UserService {
@Override
public void saveUser() {
System.out.println("saveUser()方法執行!");
}
}
public class SpringTest {
@SuppressWarnings("resource")
@Test
public void testSpring() {
ApplicationContext context = new <strong>ClassPathXmlApplicationContext</strong>(
new String[] { "bean.xml" });
UserService userService = (UserService) context.getBean("userService");
userService.saveUser();
}
}