1,Dao
package com.songyan.autoZhuangpei; public interface UserDao { public void say(); }
package com.songyan.autoZhuangpei; import org.springframework.stereotype.Repository; public class UserDaoImpl implements UserDao { public void say() { System.out.println("dao say "); } }
2,Service
package com.songyan.autoZhuangpei; public interface UserService { public void say(); }
package com.songyan.autoZhuangpei; import javax.annotation.Resource; import org.springframework.stereotype.Service; public class UserServiceImpl implements UserService { private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void say() { userDao.say(); System.out.println("service say"); } }
3,Servlet
package com.songyan.autoZhuangpei; import javax.annotation.Resource; import org.springframework.stereotype.Controller; public class UserController { private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public void say(){ userService.say(); System.out.println("controller say"); } }
4,配置
<?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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="userDao" class="com.songyan.autoZhuangpei.UserDaoImpl" autowire="byName"/> <bean id="userService" class="com.songyan.autoZhuangpei.UserServiceImpl" autowire="byName"/> <bean id="userContraller" class="com.songyan.autoZhuangpei.UserController" autowire="byName"/> </beans>
5,Test
package com.songyan.autoZhuangpei;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestZhujie {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/autoZhuangpei/beans6.xml");
UserController userController=(UserController)applicationContext.getBean("userContraller");
userController.say();
}
}
6,结果