1,Dao层
package com.songyan.zhujie; public interface UserDao { public void say(); }
package com.songyan.zhujie; import org.springframework.stereotype.Repository; @Repository("userDao") public class UserDaoImpl implements UserDao { public void say() { System.out.println("dao say "); } }
2,Service层
package com.songyan.zhujie; public interface UserService { public void say(); }
package com.songyan.zhujie; import org.springframework.stereotype.Repository; @Repository("userDao") public class UserDaoImpl implements UserDao { public void say() { System.out.println("dao say "); } }
3,Controller层
package com.songyan.zhujie; import javax.annotation.Resource; import org.springframework.stereotype.Controller; @Controller("contraller") public class UserController { @Resource(name="userService") private 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"> <context:component-scan base-package="com.songyan.zhujie" /> </beans>
5,测试
package com.songyan.zhujie; 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/zhujie/beans6.xml"); UserController userController=(UserController)applicationContext.getBean("contraller"); userController.say(); } }
6,结果