自己动手实现IOC和MVC(六)

本文通过模拟UserDao、UserService和UserAction三个层次的操作,展示了IoC容器的使用过程。从数据库层到业务层再到Action层,详细说明了依赖注入的过程及其实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面来对现在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的实现


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Shark

感谢打赏,thanks

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值