net.jrouter.spring.SpringObjectFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jrouter Show documentation
Show all versions of jrouter Show documentation
jrouter是一个围绕对象方法基于责任链(拦截器)模式设计的开源轻量级Java容器。它专注于方法的映射、调用、拦截和结果处理,采用基于配置和注解的方式来抽取和收集程序中对象的方法(method)以用于路由映射,HTTP控制器,RPC,各种应用等。
The newest version!
/*
* Copyright (C) 2010-2111 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.jrouter.spring;
import net.jrouter.ObjectFactory;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
/**
* 借由 springframework 的工厂对象创建新的对象实例。
*/
public class SpringObjectFactory implements ObjectFactory, ApplicationContextAware {
/**
* {@link AutowireCapableBeanFactory}对象。
*/
protected AutowireCapableBeanFactory autowireCapableBeanFactory;
/**
* springframework 属性注入的策略;默认{@code byName}.
*
* @see AutowireCapableBeanFactory#AUTOWIRE_BY_NAME
* @see AutowireCapableBeanFactory#AUTOWIRE_BY_TYPE
* @see AutowireCapableBeanFactory#AUTOWIRE_CONSTRUCTOR
* @see AutowireCapableBeanFactory#AUTOWIRE_NO
*/
@lombok.Getter
@lombok.Setter
private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
/**
* Empty Constructor.
*/
public SpringObjectFactory() {
super();
}
/**
* Constructor.
*
* @param autowireCapableBeanFactory {@link AutowireCapableBeanFactory}。
*/
public SpringObjectFactory(AutowireCapableBeanFactory autowireCapableBeanFactory) {
this.autowireCapableBeanFactory = autowireCapableBeanFactory;
}
/**
* Constructor.
*
* @param applicationContext ApplicationContext对象。
*/
public SpringObjectFactory(ApplicationContext applicationContext) {
setApplicationContext(applicationContext);
}
@Override
public T newInstance(Class clazz) {
if (AutowireCapableBeanFactory.AUTOWIRE_NO == autowireMode) {
return autowireCapableBeanFactory.createBean(clazz);
}
return (T) autowireCapableBeanFactory.createBean(clazz, autowireMode, false);
}
@Override
public final void setApplicationContext(ApplicationContext applicationContext) {
autowireCapableBeanFactory = findAutoWiringBeanFactory(applicationContext);
}
/**
* If the given context is assignable to AutowireCapableBeanFactory or contains a parent or a factory that is, then
* set the autoWiringFactory appropriately.
*
* @param context the application context
*
* @return the bean factory
*/
protected AutowireCapableBeanFactory findAutoWiringBeanFactory(ApplicationContext context) {
if (context instanceof AutowireCapableBeanFactory) {
// Check the context
return (AutowireCapableBeanFactory) context;
} else if (context instanceof ConfigurableApplicationContext) {
// Try and grab the beanFactory
return ((ConfigurableApplicationContext) context).getBeanFactory();
} else if (context.getParent() != null) {
// And if all else fails, try again with the parent context
return findAutoWiringBeanFactory(context.getParent());
}
return null;
}
@Override
public Class> getClass(Object obj) {
return AopProxyUtils.ultimateTargetClass(obj);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy