下面列出了org.springframework.beans.BeanWrapper#isReadableProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Provide parameter values in an {@link MapSqlParameterSource} based on values from
* the provided item.
* Supports accessing nested maps or arrays.
* e.g. :map1.level1.level2
* or :map1.array1[10].level2
* However it assume keys for the maps are all String.
* @param item the item to use for parameter values
*/
@Override
public SqlParameterSource createSqlParameterSource(T item) {
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(item);
if (beanWrapper.isReadableProperty(mapPropertyName)) {
Map<String, Object> map = (Map<String, Object>) beanWrapper.getPropertyValue(mapPropertyName);
if (shouldFlattenMap) {
Map<String, Object> flattenMap = new HashMap<String, Object>();
flatten(map, flattenMap, "");
return new MapSqlParameterSource(flattenMap);
} else {
return new MapSqlParameterSource(map);
}
}
return new MapSqlParameterSource();
}
public static Map<String, Object> toMap(Object bean, String... ignoreProperties) {
Map<String, Object> map = new LinkedHashMap<>();
List<String> ignoreList = new ArrayList<>(Arrays.asList(ignoreProperties));
ignoreList.add("class");
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean);
for (PropertyDescriptor pd : beanWrapper.getPropertyDescriptors()) {
if (!ignoreList.contains(pd.getName()) && beanWrapper.isReadableProperty(pd.getName())) {
Object propertyValue = beanWrapper.getPropertyValue(pd.getName());
map.put(pd.getName(), propertyValue);
}
}
return map;
}
private void processPojo(String counterName, Object payload) {
BeanWrapper beanWrapper = new BeanWrapperImpl(payload);
if (beanWrapper.isReadableProperty(fvcSinkProperties.getFieldName())) {
Object value = beanWrapper.getPropertyValue(fvcSinkProperties.getFieldName());
processValue(counterName, value);
}
}
private boolean isSrcHasProperty(BeanWrapper srcBean, String targetPropertyName){
if(srcBean.getWrappedInstance() instanceof Map){
Map<?, ?> map = (Map<?, ?>)srcBean.getWrappedInstance();
return map.containsKey(targetPropertyName);
}else{
return srcBean.isReadableProperty(targetPropertyName);
}
}
public AdvancedParameterSource addBean( Object bean ){
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess( bean );
for( PropertyDescriptor pd : beanWrapper.getPropertyDescriptors() ){
if( beanWrapper.isReadableProperty( pd.getName() ) ){
String paramName = pd.getName();
Object value = beanWrapper.getPropertyValue( paramName );
addValue( paramName, value );
}
}
return this;
}
public static void setCorrespondingProperties(BeanWrapper target, BeanWrapper source) {
for (PropertyDescriptor pd : source.getPropertyDescriptors()) {
String property = pd.getName();
if (!"class".equals(property) && source.isReadableProperty(property) &&
source.getPropertyValue(property) != null) {
if (target.isWritableProperty(property)) {
target.setPropertyValue(property, source.getPropertyValue(property));
}
}
}
}
@Test
void testAllPropertiesSet() throws Exception {
TomcatJdbcDataSourceFactory tomcatJdbcDataSourceFactory = new TomcatJdbcDataSourceFactory();
tomcatJdbcDataSourceFactory.setDbProperties(new Properties());
tomcatJdbcDataSourceFactory.setDefaultAutoCommit(true);
tomcatJdbcDataSourceFactory.setDefaultReadOnly(false);
tomcatJdbcDataSourceFactory.setDefaultTransactionIsolation(
TransactionDefinition.ISOLATION_READ_COMMITTED);
tomcatJdbcDataSourceFactory.setDefaultCatalog("myCatalog");
tomcatJdbcDataSourceFactory.setConnectionProperties("foo=bar");
tomcatJdbcDataSourceFactory.setInitialSize(11);
tomcatJdbcDataSourceFactory.setMaxActive(100);
tomcatJdbcDataSourceFactory.setMaxIdle(110);
tomcatJdbcDataSourceFactory.setMinIdle(10);
tomcatJdbcDataSourceFactory.setMaxWait(23);
tomcatJdbcDataSourceFactory.setValidationQuery("SELECT 1");
tomcatJdbcDataSourceFactory.setTestOnBorrow(true);
tomcatJdbcDataSourceFactory.setTestOnReturn(true);
tomcatJdbcDataSourceFactory.setTestWhileIdle(true);
tomcatJdbcDataSourceFactory.setTimeBetweenEvictionRunsMillis(100);
tomcatJdbcDataSourceFactory.setNumTestsPerEvictionRun(100);
tomcatJdbcDataSourceFactory.setMinEvictableIdleTimeMillis(1000);
tomcatJdbcDataSourceFactory.setAccessToUnderlyingConnectionAllowed(false);
tomcatJdbcDataSourceFactory.setRemoveAbandoned(true);
tomcatJdbcDataSourceFactory.setLogAbandoned(true);
tomcatJdbcDataSourceFactory.setValidationInterval(10000);
tomcatJdbcDataSourceFactory.setJmxEnabled(true);
tomcatJdbcDataSourceFactory.setInitSQL("SET SCHEMA");
tomcatJdbcDataSourceFactory.setTestOnConnect(true);
tomcatJdbcDataSourceFactory.setJdbcInterceptors("foo");
tomcatJdbcDataSourceFactory.setFairQueue(false);
tomcatJdbcDataSourceFactory.setUseEquals(false);
tomcatJdbcDataSourceFactory.setAbandonWhenPercentageFull(80);
tomcatJdbcDataSourceFactory.setMaxAge(100);
tomcatJdbcDataSourceFactory.setUseLock(true);
tomcatJdbcDataSourceFactory.setSuspectTimeout(200);
tomcatJdbcDataSourceFactory.setDataSourceJNDI("foo");
tomcatJdbcDataSourceFactory.setAlternateUsernameAllowed(true);
tomcatJdbcDataSourceFactory.setCommitOnReturn(true);
tomcatJdbcDataSourceFactory.setRollbackOnReturn(true);
tomcatJdbcDataSourceFactory.setUseDisposableConnectionFacade(false);
tomcatJdbcDataSourceFactory.setLogValidationErrors(true);
tomcatJdbcDataSourceFactory.setPropagateInterruptState(true);
DataSourceInformation dataSourceInformation = new DataSourceInformation(
DatabaseType.MYSQL, "localhost", 3306, "test", "user", "password");
DataSource dataSource = tomcatJdbcDataSourceFactory
.createDataSource(dataSourceInformation);
BeanWrapper source = PropertyAccessorFactory
.forBeanPropertyAccess(tomcatJdbcDataSourceFactory);
BeanWrapper target = PropertyAccessorFactory
.forBeanPropertyAccess(dataSource.getPoolProperties());
List<String> ignoredProperties = Arrays.asList("driverClassName", "url",
"username", "password");
for (PropertyDescriptor propertyDescriptor : source.getPropertyDescriptors()) {
if (propertyDescriptor.getWriteMethod() != null
&& target.isReadableProperty(propertyDescriptor.getName())
&& !ignoredProperties.contains(propertyDescriptor.getName())) {
assertThat(target.getPropertyValue(propertyDescriptor.getName()))
.isEqualTo(source.getPropertyValue(propertyDescriptor.getName()));
}
}
}
/**
* Returns the {@link Object value} for the {@link PropertyDescriptor property} identified by
* the given {@link String field name} on the underlying, target {@link Object}.
*
* @param fieldName {@link String} containing the name of the field to get the {@link Object value} for.
* @return the {@link Object value} for the {@link PropertyDescriptor property} identified by
* the given {@link String field name} on the underlying, target {@link Object}.
* @see org.springframework.beans.BeanWrapper#getPropertyValue(String)
* @see #getBeanWrapper()
*/
@Override
public Object getField(String fieldName) {
BeanWrapper beanWrapper = getBeanWrapper();
return beanWrapper.isReadableProperty(fieldName)
? beanWrapper.getPropertyValue(fieldName)
: null;
}