spring 手动使用代码注入bean

直接上代码 很简单一看就懂了

 

 

这是我自写的一个与mybatis类似的orm框架与spring集成的两个主要的类,希望能给和同样想自造轮子的童鞋抛出一块砖头大笑

 

BeanFactory

 

package org.smile.orm.spring;

import org.smile.orm.dao.MapperProxy;
import org.springframework.beans.factory.FactoryBean;

public class MapperBeanFactory<T> implements FactoryBean<T>{

	private MapperProxy<T> proxy; 
	
	private Class<T> type;
	
	private boolean singleton;
	@Override
	public T getObject() throws Exception {
		if(singleton){
			return proxy.getInterfaceDao();
		}else{
			return MapperProxy.copy(proxy).getInterfaceDao();
		}
		
	}

	@Override
	public Class<T> getObjectType() {
		return type;
	}

	@Override
	public boolean isSingleton() {
		return singleton;
	}
	
	
	public void setSingleton(boolean singleton) {
		this.singleton = singleton;
	}

	public void setProxy(MapperProxy<T> proxy){
		this.proxy=proxy;
	}

	public void setType(Class<T> type) {
		this.type = type;
	}

}

 

SpringXmlApplication 

package org.smile.orm.spring;

import org.smile.orm.dao.DaoMapper;
import org.smile.orm.dao.MapperProxy;
import org.smile.orm.load.ClassPathXmlApplication;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class SpringXmlApplication extends  ClassPathXmlApplication implements BeanDefinitionRegistryPostProcessor,ApplicationContextAware{
	
	private DefaultListableBeanFactory acf ;
	
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefin)
			throws BeansException {
		initOrmApplication();
		BeanDefinitionBuilder bdb;  
		for(DaoMapper<?> mapper:application.getAllMapper()){
			bdb = BeanDefinitionBuilder.rootBeanDefinition(MapperBeanFactory.class.getName());  
            bdb.getBeanDefinition().setAttribute("id", mapper.getName());  
            bdb.addPropertyValue("proxy", new MapperProxy(application, mapper));
            bdb.addPropertyValue("type", mapper.getDaoClass());
            bdb.addPropertyValue("singleton", mapper.isSingle());
	        //  注册bean  
	        acf.registerBeanDefinition(mapper.getName(), bdb.getBeanDefinition());  
		}
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
			throws BeansException {
	}


	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		this.acf=(DefaultListableBeanFactory)context.getAutowireCapableBeanFactory();
	}
	
}

 

 ClassPathXmlApplication

package org.smile.orm.load;

import java.util.List;

import javax.naming.NamingException;
import javax.sql.DataSource;

import org.smile.db.DbManager;
import org.smile.db.Dialect;
import org.smile.orm.OrmApplication;
import org.smile.orm.dao.BaseExecutor;
import org.smile.orm.dao.Executor;


public class ClassPathXmlApplication implements Application{
	
	protected DataSource dataSource;
	
	protected List<String> packageString;
	
	protected OrmApplication application;
	
	protected Dialect dialect;
	
	
	@Override
	public void setDataSource(Object dataSource) {
		if(dataSource instanceof DataSource){
			this.dataSource=(DataSource)dataSource;
		}else{
			try {
				this.dataSource = DbManager.getDataSource(String.valueOf(dataSource));
			} catch (NamingException e) {
				e.printStackTrace();
			}
		}
	}
	@Override
	public void setPackageString(List<String> packageString) {
		this.packageString = packageString;
	}
	@Override
	public void initOrmApplication(){
		application=new OrmApplication();
		application.setPackageString(packageString);
		Executor executor=new BaseExecutor();
		executor.setDataSource(dataSource);
		executor.setDialect(dialect);
		application.setExecuter(executor);
		application.init();
	}
	@Override
	public OrmApplication getOrmApplication() {
		return application;
	}
	@Override
	public void setDialect(String dialect) {
		this.dialect=Dialect.of(dialect);
	}
	
}

  

spring 中的配置

<bean id="dataSource" class="org.smile.db.DataSourceBean">
       <property name="name" value="mysql"></property>
	</bean>
	
	<bean id="ormappliction" class="org.smile.orm.spring.SpringXmlApplication">
		<property name="dataSource" ref="dataSource"></property>
		<property name="dialect" value="mysql"></property>
		<property name="packageString">
			<list>
				<value>dbmapper</value>
			</list>
		</property>
	</bean>

 

你可能感兴趣的:(pring,手动注入)