/**
* 将任意类型转换成字符串
* @param value
* @param <T>
* @return
*/
public static <T> String beanToString(T value) {
if(null == value) {
return null;
}
Class<?> clazz = value.getClass();
if (clazz == int.class || clazz == Integer.class) {
return value + "";
} else if (clazz == String.class) {
return (String) value;
} else if (clazz == long.class || clazz == Long.class) {
return value + "";
} else {
return JSON.toJSONString(value);
}
}
spring 任务的部分源码
private <T> T resolveSchedulerBean(Class<T> schedulerType, boolean byName) {
if (byName) {
T scheduler = this.beanFactory.getBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, schedulerType);
if (this.beanFactory instanceof ConfigurableBeanFactory) {
((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(
DEFAULT_TASK_SCHEDULER_BEAN_NAME, this.beanName);
}
return scheduler;
}
else if (this.beanFactory instanceof AutowireCapableBeanFactory) {
NamedBeanHolder<T> holder = ((AutowireCapableBeanFactory) this.beanFactory).resolveNamedBean(schedulerType);
if (this.beanFactory instanceof ConfigurableBeanFactory) {
((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(
holder.getBeanName(), this.beanName);
}
return holder.getBeanInstance();
}
else {
return this.beanFactory.getBean(schedulerType);
}
}
* Copyright 2002-2016 the original author or authors.
package org.springframework.beans.factory.config;
import org.springframework.beans.factory.NamedBean;
import org.springframework.util.Assert;
/**
* A simple holder for a given bean name plus bean instance.
*
* @author Juergen Hoeller
* @since 4.3.3
* @see AutowireCapableBeanFactory#resolveNamedBean(Class)
*/
public class NamedBeanHolder<T> implements NamedBean {
private final String beanName;
private final T beanInstance;
/**
* Create a new holder for the given bean name plus instance.
* @param beanName the name of the bean
* @param beanInstance the corresponding bean instance
*/
public NamedBeanHolder(String beanName, T beanInstance) {
Assert.notNull(beanName, "Bean name must not be null");
this.beanName = beanName;
this.beanInstance = beanInstance;
}
/**
* Return the name of the bean (never {@code null}).
*/
@Override
public String getBeanName() {
return this.beanName;
}
/**
* Return the corresponding bean instance (can be {@code null}).
*/
public T getBeanInstance() {
return this.beanInstance;
}
}