下面来对现在ioc做的简单测试
UserDao.java 模拟数据库的操作
package com.ajun.test.dao; import com.ajunframework.beans.annotation.Dao; @Dao public class UserDao { public void add(){ System.out.println("UserDao's add method was called"); } }
UserService.java模拟业务层的操作
package com.ajun.test.service; import com.ajun.test.dao.UserDao; import com.ajunframework.beans.annotation.Property; import com.ajunframework.beans.annotation.Service; @Service public class UserService { @Property private UserDao userDao;//注入属性 public void add(){ System.out.println("UserService's add method was called"); userDao.add(); } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
UserAction.java模拟action层得操作
package com.ajun.test.action; import com.ajun.test.service.UserService; import com.ajunframework.beans.annotation.Action; import com.ajunframework.beans.annotation.Property; @Action public class UserAction { @Property private UserService userService;//注入属性 public void add(){ System.out.println("UserAction's add method was called"); userService.add(); } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }
下面是测试方法
package com.ajun.test; import com.ajun.test.action.UserAction; import com.ajunframework.beans.applicationContext.AnnotationClassPathApplicationContext; import com.ajunframework.beans.applicationContext.ClassPathApplicationContext; import com.ajunframework.beans.factory.AnnotationBeanFactory; public class Test { public static void main(String [] ag){ ClassPathApplicationContext cx = AnnotationClassPathApplicationContext.getAnnotationClassPathApplicationContext(); cx.init(); UserAction action = AnnotationBeanFactory.getBeanFactory().getBean("userAction",UserAction.class); action.add(); } }
打印结果:
UserAction's add method was called UserService's add method was called UserDao's add method was called
ioc这个project只是给大家提供个思路而已,现在ioc只是实现singleton 而没有实现prototype ,如果大家有思路 ,可以讨论一下,肯定是会用到prototype模式的。
下一节我会给大家mvc的实现