本文整理匯總了Java中org.springframework.beans.BeanWrapper.getPropertyDescriptor方法的典型用法代碼示例。如果您正苦於以下問題:Java BeanWrapper.getPropertyDescriptor方法的具體用法?Java BeanWrapper.getPropertyDescriptor怎麽用?Java BeanWrapper.getPropertyDescriptor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.beans.BeanWrapper
的用法示例。
在下文中一共展示了BeanWrapper.getPropertyDescriptor方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: autowireByType
import org.springframework.beans.BeanWrapper; //導入方法依賴的package包/類
/**
* Abstract method defining "autowire by type" (bean properties by type) behavior.
* <p>This is like PicoContainer default, in which there must be exactly one bean
* of the property type in the bean factory. This makes bean factories simple to
* configure for small namespaces, but doesn't work as well as standard Spring
* behavior for bigger applications.
* @param beanName the name of the bean to autowire by type
* @param mbd the merged bean definition to update through autowiring
* @param bw BeanWrapper from which we can obtain information about the bean
* @param pvs the PropertyValues to register wired objects with
*/
protected void autowireByType(
String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {
TypeConverter converter = getCustomTypeConverter();
if (converter == null) {
converter = bw;
}
Set<String> autowiredBeanNames = new LinkedHashSet<String>(4);
String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
for (String propertyName : propertyNames) {
try {
PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName);
// Don't try autowiring by type for type Object: never makes sense,
// even if it technically is a unsatisfied, non-simple property.
if (!Object.class.equals(pd.getPropertyType())) {
MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd);
// Do not allow eager init for type matching in case of a prioritized post-processor.
boolean eager = !PriorityOrdered.class.isAssignableFrom(bw.getWrappedClass());
DependencyDescriptor desc = new AutowireByTypeDependencyDescriptor(methodParam, eager);
Object autowiredArgument = resolveDependency(desc, beanName, autowiredBeanNames, converter);
if (autowiredArgument != null) {
pvs.add(propertyName, autowiredArgument);
}
for (String autowiredBeanName : autowiredBeanNames) {
registerDependentBean(autowiredBeanName, beanName);
if (logger.isDebugEnabled()) {
logger.debug("Autowiring by type from bean name '" + beanName + "' via property '" +
propertyName + "' to bean named '" + autowiredBeanName + "'");
}
}
autowiredBeanNames.clear();
}
}
catch (BeansException ex) {
throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, propertyName, ex);
}
}
}
示例2: convertForProperty
import org.springframework.beans.BeanWrapper; //導入方法依賴的package包/類
/**
* Convert the given value for the specified target property.
*/
private Object convertForProperty(Object value, String propertyName, BeanWrapper bw, TypeConverter converter) {
if (converter instanceof BeanWrapperImpl) {
return ((BeanWrapperImpl) converter).convertForProperty(value, propertyName);
}
else {
PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName);
MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd);
return converter.convertIfNecessary(value, pd.getPropertyType(), methodParam);
}
}
示例3: introspectPropertyOfObjectStrict
import org.springframework.beans.BeanWrapper; //導入方法依賴的package包/類
/**
* Return String representations of the values of all of the properties of the object. If there
* is an error it will throw an error.
*
* @param <T> Type of the returned property.
* @param propertyName properyName to look for
* @param object object to introspect
* @return Item to return
* @throws InvocationTargetException thrown by the invoke reflection on the object
* @throws IllegalAccessException thrown if the property doesn't exist / not reabable
*/
public static <T> T introspectPropertyOfObjectStrict(final String propertyName, final Object object)
throws InvocationTargetException, IllegalAccessException {
T readValue = null;
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(object);
PropertyDescriptor des = wrapper.getPropertyDescriptor(propertyName);
Method readMethod = des.getReadMethod();
if (readMethod != null) {
readValue = (T) readMethod.invoke(object);
} else {
throw new IllegalAccessException(propertyName + " is not a readable property");
}
return readValue;
}
示例4: test1
import org.springframework.beans.BeanWrapper; //導入方法依賴的package包/類
@Test
public void test1() {
BeanWrapper beanWrapper = new BeanWrapperImpl(Demo.class);
PropertyDescriptor descriptor = beanWrapper.getPropertyDescriptor("args");
Class<?> type = descriptor.getPropertyType();
Assert.assertTrue(type.isArray());
Assert.assertEquals(String.class, type.getComponentType());
}
示例5: autowireByType
import org.springframework.beans.BeanWrapper; //導入方法依賴的package包/類
/**
* Abstract method defining "autowire by type" (bean properties by type) behavior.
* <p>This is like PicoContainer default, in which there must be exactly one bean
* of the property type in the bean factory. This makes bean factories simple to
* configure for small namespaces, but doesn't work as well as standard Spring
* behavior for bigger applications.
* @param beanName the name of the bean to autowire by type
* @param mbd the merged bean definition to update through autowiring
* @param bw BeanWrapper from which we can obtain information about the bean
* @param pvs the PropertyValues to register wired objects with
*/
protected void autowireByType(
String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {
TypeConverter converter = getCustomTypeConverter();
if (converter == null) {
converter = bw;
}
Set<String> autowiredBeanNames = new LinkedHashSet<String>(4);
String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
for (String propertyName : propertyNames) {
try {
PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName);
// Don't try autowiring by type for type Object: never makes sense,
// even if it technically is a unsatisfied, non-simple property.
if (Object.class != pd.getPropertyType()) {
MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd);
// Do not allow eager init for type matching in case of a prioritized post-processor.
boolean eager = !PriorityOrdered.class.isAssignableFrom(bw.getWrappedClass());
DependencyDescriptor desc = new AutowireByTypeDependencyDescriptor(methodParam, eager);
Object autowiredArgument = resolveDependency(desc, beanName, autowiredBeanNames, converter);
if (autowiredArgument != null) {
pvs.add(propertyName, autowiredArgument);
}
for (String autowiredBeanName : autowiredBeanNames) {
registerDependentBean(autowiredBeanName, beanName);
if (logger.isDebugEnabled()) {
logger.debug("Autowiring by type from bean name '" + beanName + "' via property '" +
propertyName + "' to bean named '" + autowiredBeanName + "'");
}
}
autowiredBeanNames.clear();
}
}
catch (BeansException ex) {
throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, propertyName, ex);
}
}
}
示例6: findMyBatisBeanDefinitions
import org.springframework.beans.BeanWrapper; //導入方法依賴的package包/類
private Map<String, BeanDefinition> findMyBatisBeanDefinitions() {
String[] enumsLocations = attributes.getStringArray(Constant.ENUMS_LOCATIONS_ATTRIBUTE_NAME);
String[] basePackages = attributes.getStringArray(Constant.BASE_PACKAGES_ATTRIBUTE_NAME);
AnnotationAttributes[] plugins = attributes.getAnnotationArray(Constant.PLUGINS_ATTRIBUTE_NAME);
if (ArrayUtils.isEmpty(enumsLocations)) {
enumsLocations = findDefaultPackage(metadata);
}
if (ArrayUtils.isEmpty(basePackages)) {
basePackages = findDefaultPackage(metadata);
}
Map<String, BeanDefinition> beanDefinitions = new HashMap<>(16);
BeanDefinitionBuilder sqlSessionFactoryBean = BeanDefinitionBuilder.genericBeanDefinition(SqlSessionFactoryBean.class);
if (useFlyway) {
sqlSessionFactoryBean.addDependsOn(Constant.FLYWAY_BEAN_NAME);
}
sqlSessionFactoryBean.addPropertyReference("dataSource", Constant.DATA_SOURCE_BEAN_NAME);
sqlSessionFactoryBean.addPropertyValue("mapperLocations", "classpath*:/META-INF/mybatis/**/*Mapper.xml");
sqlSessionFactoryBean.addPropertyValue("configLocation", "classpath:/META-INF/mybatis/mybatis.xml");
TypeHandlerScanner scanner = new TypeHandlerScanner();
sqlSessionFactoryBean.addPropertyValue("typeHandlers", scanner.find(StringUtils.join(enumsLocations, ",")));
List<Interceptor> pluginsList = new ArrayList<>(plugins.length);
List<Class<? extends Interceptor>> clazz = new ArrayList<>(plugins.length);
for (AnnotationAttributes plugin : plugins) {
Class<? extends Interceptor> pluginClass = plugin.getClass("value");
AnnotationAttributes[] params = plugin.getAnnotationArray("params");
clazz.add(pluginClass);
Interceptor interceptor = BeanUtils.instantiate(pluginClass);
BeanWrapper beanWrapper = new BeanWrapperImpl(interceptor);
for (AnnotationAttributes param : params) {
String key = param.getString("key");
String value = param.getString("value");
PropertyDescriptor descriptor = beanWrapper.getPropertyDescriptor(key);
Method writeMethod = descriptor.getWriteMethod();
Method readMethod = descriptor.getReadMethod();
writeMethod.setAccessible(true);
try {
Class<?> returnType = readMethod.getReturnType();
Object valueObject = value;
if (Integer.class.equals(returnType) || int.class.equals(returnType)) {
valueObject = Integer.valueOf(value);
} else if (Long.class.equals(returnType) || long.class.equals(returnType)) {
valueObject = Long.valueOf(value);
} else if (Boolean.class.equals(returnType) || boolean.class.equals(returnType)) {
valueObject = Boolean.valueOf(value);
} else if (Double.class.equals(returnType) || double.class.equals(returnType)) {
valueObject = Double.valueOf(value);
}
writeMethod.invoke(interceptor, valueObject);
} catch (Exception e) {
e.printStackTrace();
}
}
pluginsList.add(interceptor);
}
if (!clazz.contains(PaginationInterceptor.class)) {
pluginsList.add(BeanUtils.instantiate(PaginationInterceptor.class));
}
sqlSessionFactoryBean.addPropertyValue("plugins", pluginsList);
BeanDefinitionBuilder scannerConfigurerBean = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
scannerConfigurerBean.addPropertyValue("basePackage", StringUtils.join(basePackages, ","));
scannerConfigurerBean.addPropertyValue("annotationClass", Repository.class);
scannerConfigurerBean.addPropertyValue("sqlSessionFactoryBeanName", "sqlSessionFactory");
beanDefinitions.put("sqlSessionFactory", sqlSessionFactoryBean.getBeanDefinition());
beanDefinitions.put("mapperScannerConfigurer", scannerConfigurerBean.getBeanDefinition());
return beanDefinitions;
}