1、导入spring的dist中的jar包和commons-logging包
2、在src目录下创建相应的beans.xml
3、为beans.xml添加相应的schema
<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">
</beans>
4、新建一个类
5、在beans.xml中创建对象
<!--
创建如下bean等于完成了:HelloWorld helloWorld = new HelloWorld
-->
<bean id="helloWorld" class="org.zttc.itat.spring.model.HelloWorld"/>
6、在测试类中使用这个对象
6.1、创建Spring的工厂
private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
6.2、通过Spring工厂获取相应的对象
//此处getBean中的helloWorld就是beans.xml配置文件中的id
HelloWorld hello = factory.getBean("helloWorld",HelloWorld.class);
//此时的hello对象就是被Spring说管理的对象
System.out.println(hello.hello());
model:
package org.zttc.itat.spring.model;
public class User {
private int id;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public User() {
}
public User(int id, String username) {
super();
this.id = id;
this.username = username;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + "]";
}
}
dao:
//抽象类
package org.zttc.itat.spring.dao;
import org.zttc.itat.spring.model.User;
public interface IUserDao {
public void add(User user);
public void delete(int id);
public User load(int id);
}
//实现类
package org.zttc.itat.spring.dao;
import org.springframework.stereotype.Repository;
import org.zttc.itat.spring.model.User;
//等于完成了<bean id="userDao" class="org.zttc.itat.spring.UserDao"/>
//@Component("userDao")//公共的创建bean的Annotation
@Repository("userDao")//@Repository一般用于DAO的注入
public class UserDao implements IUserDao {
@Override
public void add(User user) {
System.out.println("添加了"+user);
}
@Override
public void delete(int id) {
System.out.println("delete :"+id);
}
@Override
public User load(int id) {
System.out.println("load:"+id);
return null;
}
}
service:
//抽象类
package org.zttc.itat.spring.service;
import org.zttc.itat.spring.model.User;
public interface IUserService {
public void add(User user);
public void delete(int id);
public User load(int id);
}
//实现类
package org.zttc.itat.spring.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.zttc.itat.spring.dao.IUserDao;
import org.zttc.itat.spring.model.User;
//@Component("userService")
@Service("userService")//业务层一般使用@Service
public class UserService implements IUserService {
private IUserDao userDao;
private IUserDao userJDBCDao;
public IUserDao getUserJDBCDao() {
return userJDBCDao;
}
public void setUserJDBCDao(IUserDao userJDBCDao) {
this.userJDBCDao = userJDBCDao;
}
public IUserDao getUserDao() {
return userDao;
}
//默认通过名称注入,在JSR330中提供了@Inject来注入
@Resource
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public void delete(int id) {
userDao.delete(id);
}
@Override
public User load(int id) {
return userDao.load(id);
}
}
Action:
package org.zttc.itat.spring.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.zttc.itat.spring.model.User;
import org.zttc.itat.spring.service.IUserService;
//@Component("userAction")
@Controller("userAction")//MVC的控制层一般使用@Controller
@Scope("prototype")
public class UserAction {
private User user;
private IUserService userService;
private int id;
private List<String> names;
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
public IUserService getUserService() {
return userService;
}
@Resource
public void setUserService(IUserService userService) {
this.userService = userService;
}
public UserAction() {
}
public UserAction(IUserService userService) {
super();
this.userService = userService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
//public IUserService getUserService() {
//return userService;
//}
//
//public void setUserService(IUserService userService) {
//this.userService = userService;
//}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void add() {
userService.add(user);
}
public void delete() {
userService.delete(id);
}
public void load() {
userService.load(id);
}
}
testSpring:
package org.zttc.itat.spring.test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.zttc.itat.spring.action.UserAction;
import org.zttc.itat.spring.model.HelloWorld;
import org.zttc.itat.spring.model.User;
public class TestSpring {
//创建Spring的工厂
private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
@Test
public void testUser01() {
UserAction ua1 = factory.getBean("userAction", UserAction.class);
User u = new User(1,"悟空");
ua1.setUser(u);
ua1.add();
UserAction ua2 = factory.getBean("userAction", UserAction.class);
ua2.add();
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 打开Spring的Annotation支持 -->
<context:annotation-config/>
<!-- 设定Spring 去哪些包中找Annotation -->
<context:component-scan base-package="org.zttc.itat.spring"/>
</beans>