spring Setter方法注入
package entity; public class User { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package dao; public interface UserDao { public void saveUser(); public void getUserById(int id); }
package dao.imp; import dao.UserDao; public class UserDaoImp implements UserDao{ @Override public void saveUser() { // TODO Auto-generated method stub System.out.println("--------save user------"); } @Override public void getUserById(int id) { // TODO Auto-generated method stub } }
package service; public interface UserService { public void saveUser(); public void getUserById(int id); }
package service.imp; import dao.UserDao; import service.UserService; public class UserServiceImp implements UserService{ private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void saveUser() { // TODO Auto-generated method stub System.out.println("SAVE USER"); } @Override public void getUserById(int id) { // TODO Auto-generated method stub } }
package action; import service.UserService; public class UserAction { private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String saveUser(){ userService.saveUser(); return null; } }
<?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="userDao" class="dao.imp.UserDaoImp"></bean> <bean id="userService" class="service.imp.UserServiceImp"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userAction" class="action.UserAction"> <property name="userService" ref="userService"></property> </bean> </beans>