②怎么进行注入? 也是循环List<BeanDefinition> ,然后循环BeanDefinition里面的属性集合,接着利用java反射调用set方法进行注入操作
- List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
- if(beanDefinitions!=null && !beanDefinitions.isEmpty()){
- for(BeanDefinition bd:beanDefinitions){
- if(bd!=null){
- //初始化bean
- //然后是存储bean
- }
- }
- }
- List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
- for(BeanDefinition bd : beanDefinitions){
- Object bean =获得实例化后的bean根据id
- List<PropertyDefinition> pds = bd.getProperties();
- if(pds!=null && !pds.isEmpty()){
- for(PropertyDefinition pd:pds){
- String name = pd.getName();
- String ref = pd.getRef();
- if(ref!=null && !"".equals(ref)){
- //这里会进行属性的注入操作
- }
- }
- }
- }
- package com.ajunframework.beans.factory;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 存储bean
- * @author harries
- *
- */
- public class BeanMap {
- private static Map<String,Object> beans = new HashMap<String,Object>();
- private BeanMap(){}
- /**
- * 添加bean
- * @param beanName
- * @param value
- */
- public static void put(String beanName ,Object value){
- beans.put(beanName, value);
- }
- /**
- * 获得bean
- * @param beanName
- * @return
- */
- public static Object getBean(String beanName){
- if(!beans.isEmpty()){
- return beans.get(beanName);
- }
- return new HashMap<String,Object>();
- }
- public static Map<String,Object> getBeanMap(){
- return beans;
- }
- }
- package com.ajunframework.beans.factory;
- /**
- *bean工厂
- * @author harries
- * @http://www.liuhaihua.cn
- */
- public interface BeanFactory {
- /**
- * 设置bean
- * @param beanName
- * @param fullClassName
- */
- void setBean(String beanName,String fullClassName);
- /**
- * 获得bean根据beanName
- * @param name
- * @return
- */
- Object getBean(String name);
- <T> T getBean(String name, Class<T> requiredType);
- <T> T getBean(Class<T> requiredType);
- Object getBean(String name, Object... args);
- boolean containsBean(String name);
- boolean isSingleton(String name);
- boolean isPrototype(String name);
- @SuppressWarnings("unchecked")
- boolean isTypeMatch(String name, Class targetType);
- Class<?> getType(String name);
- String[] getAliases(String name);
- }
- package com.ajunframework.beans.factory;
- import com.ajunframework.beans.annotation.Action;
- import com.ajunframework.beans.annotation.Dao;
- import com.ajunframework.beans.annotation.Scope;
- import com.ajunframework.beans.annotation.Service;
- import com.ajunframework.beans.constant.BeanScop;
- import com.ajunframework.beans.utils.BeanUtils;
- /**
- * bean工厂
- * @author harries
- * @http://www.liuhaihua.cn
- */
- public class AnnotationBeanFactory implements BeanFactory {
- private static AnnotationBeanFactory beanFactory = new AnnotationBeanFactory();
- private AnnotationBeanFactory(){}
- public static AnnotationBeanFactory getBeanFactory(){
- if(beanFactory!=null){
- return beanFactory;
- }
- return new AnnotationBeanFactory();
- }
- public void setBean(String beanName,String fullClassName) {
- try {
- Object bean = BeanUtils.instanceClass(Class.forName(fullClassName));
- BeanMap.put(beanName, bean);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public boolean containsBean(String name) {
- Object bean = BeanMap.getBeanMap().get(name);
- if(bean!=null){
- return true;
- }
- return false;
- }
- public Object getBean(String name) {
- return BeanMap.getBeanMap().get(name);
- }
- @SuppressWarnings("unchecked")
- public <T> T getBean(String name, Class<T> requiredType) {
- Object bean = BeanMap.getBeanMap().get(name);
- if(bean!=null){
- Class<?> clazz = bean.getClass();
- if(clazz.getName().equals(requiredType.getName()) ){
- return (T)bean;
- }
- }
- return null;
- }
- @SuppressWarnings("unchecked")
- public <T> T getBean(Class<T> requiredType) {
- if(requiredType.isAnnotationPresent(Dao.class) || requiredType.isAnnotationPresent(Service.class) || requiredType.isAnnotationPresent(Action.class)){
- String fullName = requiredType.getName();
- String beanName = fullName.substring(fullName.lastIndexOf(".")+1).substring(0,1).toLowerCase()+ fullName.substring(fullName.lastIndexOf(".")+1).substring(1);
- Object bean = BeanMap.getBeanMap().get(beanName);
- return (T)bean;
- }
- return null;
- }
- public Object getBean(String name, Object... args) {
- return null;
- }
- public Class<?> getType(String name) {
- Object bean = BeanMap.getBeanMap().get(name);
- if(bean!=null){
- bean.getClass().getClass();
- }
- return null;
- }
- @SuppressWarnings("unchecked")
- public boolean isPrototype(String name) {
- Object bean = BeanMap.getBeanMap().get(name);
- if(bean!=null){
- Class clazz = bean.getClass();
- if(clazz.isAnnotationPresent(Scope.class)){
- String scopeValue = ((Scope) clazz.getAnnotation(Scope.class)).value();
- if(scopeValue!=null && BeanScop.PROTOTYPE.equals(scopeValue.trim())){
- return true;
- }
- }
- }
- return false;
- }
- @SuppressWarnings("unchecked")
- public boolean isSingleton(String name) {
- Object bean = BeanMap.getBeanMap().get(name);
- if(bean!=null){
- Class clazz = bean.getClass();
- if(clazz.isAnnotationPresent(Scope.class)){
- String scopeValue = ((Scope) clazz.getAnnotation(Scope.class)).value();
- if(scopeValue!=null && BeanScop.SINGLETON.equals(scopeValue.trim())){
- return true;
- }else if(scopeValue!=null && BeanScop.PROTOTYPE.equals(scopeValue.trim())){
- return false;
- }else{
- return true;
- }
- }else{
- return true;
- }
- }
- return false;
- }
- @SuppressWarnings("unchecked")
- public boolean isTypeMatch(String name, Class targetType) {
- Object bean = BeanMap.getBeanMap().get(name);
- if(bean!=null){
- Class clazz = bean.getClass();
- if(clazz.equals(targetType)){
- return true;
- }
- }
- return false;
- }
- public String[] getAliases(String name) {
- return null;
- }
- }
- package com.ajunframework.beans.applicationContext;
- /**
- * 初始化上下文bean 接口
- * @author harries
- * @http://www.liuhaihua.cn
- */
- public interface ClassPathApplicationContext {
- /**
- * instance bean
- */
- void instanceBean();
- /**
- * DI bean
- */
- void injectObject();
- /**
- * read class‘s Annotation info to instance definitionBean
- */
- void readAnnotationCLass();
- /**
- * 初始化
- */
- void init();
- }
- package com.ajunframework.beans.applicationContext;
- import java.util.List;
- import com.ajunframework.beans.annotation.Action;
- import com.ajunframework.beans.annotation.Dao;
- import com.ajunframework.beans.annotation.Service;
- import com.ajunframework.beans.definition.BeanDefinition;
- import com.ajunframework.beans.definition.BeandefinitionList;
- import com.ajunframework.beans.definition.PropertyDefinition;
- import com.ajunframework.beans.factory.AnnotationBeanFactory;
- import com.ajunframework.beans.scan.ScanClass;
- import com.ajunframework.beans.utils.BeanWrapper;
- /**
- * 基于注释的上下文实例化bean类
- * @author harries
- * @http://www.liuhaihua.cn
- */
- public class AnnotationClassPathApplicationContext implements ClassPathApplicationContext {
- private static AnnotationClassPathApplicationContext cx = new AnnotationClassPathApplicationContext();
- private AnnotationClassPathApplicationContext(){}
- public static AnnotationClassPathApplicationContext getAnnotationClassPathApplicationContext(){
- if(cx!=null){
- return cx;
- }
- return new AnnotationClassPathApplicationContext();
- }
- public void instanceBean() {
- List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
- if(beanDefinitions!=null && !beanDefinitions.isEmpty()){
- for(BeanDefinition bd:beanDefinitions){
- if(bd!=null){
- //初始化bean
- AnnotationBeanFactory.getBeanFactory().setBean(bd.getId(), bd.getCalssName());
- }
- }
- }
- }
- public void injectObject() {
- List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
- for(BeanDefinition bd : beanDefinitions){
- Object bean = AnnotationBeanFactory.getBeanFactory().getBean(bd.getId());
- List<PropertyDefinition> pds = bd.getProperties();
- if(pds!=null && !pds.isEmpty()){
- for(PropertyDefinition pd:pds){
- String name = pd.getName();
- String ref = pd.getRef();
- if(ref!=null && !"".equals(ref)){
- BeanWrapper b = new BeanWrapper(bean);
- Object propvalue = AnnotationBeanFactory.getBeanFactory().getBean(name);
- b.setPropertyValue(name, propvalue);
- }
- }
- }
- }
- }
- public void readAnnotationCLass() {
- Class<?> [] classes = ScanClass.getScanPackageClasses();
- if(classes != null && classes.length>0){
- for(Class<?> c:classes){
- if(c.isAnnotationPresent(Dao.class) || c.isAnnotationPresent(Service.class) || c.isAnnotationPresent(Action.class)){
- BeandefinitionList.addBeanDefinitionAndSetProperty(c);
- }
- }
- }
- }
- public void init(){
- readAnnotationCLass();
- instanceBean();
- injectObject();
- }
- }