spring Annotation注入笔记

public class AnnotationAutoloadBeanDefinitionParser implements
		BeanDefinitionParser

		ClassAnnotationProcessor processor = new ClassAnnotationProcessor(
				filter);
		for (SpringAnnotationHandler handler : handlers)
		{
			handler.setBeanDefinitionRegistry(parserContext.getRegistry());
			processor.registHandler(handler);
		}


扫描元数据声明

	ClassAnnotationProcessor

		public void process(Class target)
	{
		if (target == null)
		{
			throw new IllegalArgumentException("Target class can't be null");
		}

		Annotation[] annotations = target.getAnnotations();
		for (Annotation a : annotations)
		{
			IAnnotationHandler handler = this.handlers.get(a.annotationType());
			if (handler != null)
			{
				 
				handler.handle(a, target);
			} else
			{
				if (LOG.isDebugEnabled())
				{
					 
				}
			}
			handler = null;
		}
	}

ServiceAnnotationHandler
注册到spring容器

public void handle(Service s, Class target)
	{
		String name = target.getSimpleName();
		if (!name.endsWith("ServiceImpl"))
		{
			throw new IllegalConfigException(
					target.getName()
							+ " is not a service bean.service bean 's class name must be end with 'ServiceImpl'");
		}
		name = name.substring(0, name.length() - "Impl".length());
		name = name.substring(0, 1).toLowerCase()
				+ name.substring(1, name.length());
		RootBeanDefinition definition = new RootBeanDefinition();
		definition.setAbstract(false);
		definition.setBeanClass(target);
		definition.setSingleton(true);
		definition.setLazyInit(s.lazy());
		definition.setAutowireCandidate(true);
		definition.setAutowireMode(s.autoWire().value());

		if (!"".equals(s.init()))
		{
			definition.setInitMethodName(s.init().trim());
		}
		if (!"".equals(s.destroy()))
		{
			definition.setDestroyMethodName(s.destroy().trim());
		}

		if (LOG.isDebugEnabled())
		{
			LOG.debug("Reader Bean Definition[" + definition + "] with name["
					+ name + "]");
		}
		SpringAnnotationUtils.readProperties(target,definition);
		registerBeanDefinition(name, definition);
	}

你可能感兴趣的:(spring,bean)