注解的spring模拟

本文介绍了一个使用Java注解实现依赖注入的例子。通过自定义注解`@AutoWire`及其实现类`AnnotationFactory`,文章展示了如何在不修改类的前提下,通过反射机制为对象的字段注入依赖。

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

package com.wyj.annotation;

import java.lang.annotation.Target;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)// 注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Documented
@Inherited
public @interface AutoWire
{

    public String name();

}


注解的工厂类

package com.wyj.annotation.factory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;
import com.wyj.annotation.AutoWire;

/**
 * 注解的工厂类
 * 
 * @author 王宜君
 */
public class AnnotationFactory
{
    public static Object getBean( Class<?> t )

    throws InstantiationException, IllegalAccessException, SecurityException,
            NoSuchMethodException, IllegalArgumentException,

            InvocationTargetException, ClassNotFoundException
    {

        Object obj = t.newInstance();

        Field[] fields = t.getDeclaredFields();

        for ( Field f : fields )
        {

            // 首先判断改域有没有AutoWire的注解:

            if ( f.isAnnotationPresent( AutoWire.class ) )
            {

                // 有的话便得到它的注解类:
                AutoWire wire = f.getAnnotation( AutoWire.class );

                // 得它的注解类name的属性值,我们要利用他反射成一个对象赋予传进来的Class类型的对象:
                String clazzName = wire.name();

                // 下面便是反射的一些基本用法:
                Object inject = Class.forName( clazzName ).newInstance();

                String fieldName = f.getName();

                String mName = fieldName.substring( 0, 1 ).toUpperCase()

                + fieldName.substring( 1 );

                Method method = t.getMethod( "set" + mName, f.getType() );

                method.invoke( obj, inject );

                return obj;

            }

        }

        return null;

    }

}

注解的dao类

package com.wyj.dao;

import com.wyj.annotation.entity.Employee;

public class EmployeeDao
{
    public void save( Employee emp )
    {

        System.out.println( emp );
        System.out.println( emp.getId() + "," + emp.getName() + ","
                + emp.getSalary() );

        System.out.println( "emp have saved." );

    }

}


注解的业务类

package com.wyj.service;

import com.wyj.annotation.AutoWire;
import com.wyj.annotation.entity.Employee;
import com.wyj.dao.EmployeeDao;

public class EmployeeService
{
    @AutoWire( name = "com.wyj.dao.EmployeeDao" )
    private EmployeeDao dao;

    public EmployeeDao getDao()
    {
        return dao;
    }

    public void setDao( EmployeeDao dao )
    {

        this.dao = dao;

    }

    public void save()
    {

        Employee emp = new Employee();

        emp.setId( 246 );

        emp.setName( "brzone@126.com" );

        emp.setSalary( 8888 );

        dao.save( emp );

    }

}


注解的测试类

package com.wyj.annotation.test;

import com.wyj.annotation.factory.AnnotationFactory;
import com.wyj.service.EmployeeService;

public class AnnotationClientTest
{
    public static void main( String[] args ) throws Exception
    {

        EmployeeService service = (EmployeeService)
        
        AnnotationFactory.getBean( EmployeeService.class );

        service.save();

    }

}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值