Skip to content

Commit e51f660

Browse files
committed
Polishing
1 parent 9f71c98 commit e51f660

File tree

6 files changed

+51
-79
lines changed

6 files changed

+51
-79
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,9 +21,7 @@
2121
import java.io.Serializable;
2222
import java.util.ArrayList;
2323
import java.util.Arrays;
24-
import java.util.HashMap;
2524
import java.util.List;
26-
import java.util.Map;
2725

2826
import org.aopalliance.aop.Advice;
2927
import org.aopalliance.intercept.Interceptor;
@@ -342,11 +340,8 @@ private synchronized Object newPrototypeInstance() {
342340
// an independent instance of the configuration.
343341
// In this case, no proxy will have an instance of this object's configuration,
344342
// but will have an independent copy.
345-
if (logger.isTraceEnabled()) {
346-
logger.trace("Creating copy of prototype ProxyFactoryBean config: " + this);
347-
}
348-
349343
ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory());
344+
350345
// The copy needs a fresh advisor chain, and a fresh TargetSource.
351346
TargetSource targetSource = freshTargetSource();
352347
copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain());
@@ -359,9 +354,6 @@ private synchronized Object newPrototypeInstance() {
359354
}
360355
copy.setFrozen(this.freezeProxy);
361356

362-
if (logger.isTraceEnabled()) {
363-
logger.trace("Using ProxyCreatorSupport copy: " + copy);
364-
}
365357
return getProxy(copy.createAopProxy());
366358
}
367359

@@ -395,9 +387,7 @@ private void checkInterceptorNames() {
395387
logger.debug("Bean with name '" + finalName + "' concluding interceptor chain " +
396388
"is not an advisor class: treating it as a target or TargetSource");
397389
}
398-
String[] newNames = new String[this.interceptorNames.length - 1];
399-
System.arraycopy(this.interceptorNames, 0, newNames, 0, newNames.length);
400-
this.interceptorNames = newNames;
390+
this.interceptorNames = Arrays.copyOf(this.interceptorNames, this.interceptorNames.length - 1);
401391
}
402392
}
403393
}
@@ -449,16 +439,12 @@ private synchronized void initializeAdvisorChain() throws AopConfigException, Be
449439

450440
// Materialize interceptor chain from bean names.
451441
for (String name : this.interceptorNames) {
452-
if (logger.isTraceEnabled()) {
453-
logger.trace("Configuring advisor or advice '" + name + "'");
454-
}
455-
456442
if (name.endsWith(GLOBAL_SUFFIX)) {
457443
if (!(this.beanFactory instanceof ListableBeanFactory)) {
458444
throw new AopConfigException(
459445
"Can only use global advisors or interceptors with a ListableBeanFactory");
460446
}
461-
addGlobalAdvisor((ListableBeanFactory) this.beanFactory,
447+
addGlobalAdvisors((ListableBeanFactory) this.beanFactory,
462448
name.substring(0, name.length() - GLOBAL_SUFFIX.length()));
463449
}
464450

@@ -475,7 +461,7 @@ private synchronized void initializeAdvisorChain() throws AopConfigException, Be
475461
// Avoid unnecessary creation of prototype bean just for advisor chain initialization.
476462
advice = new PrototypePlaceholderAdvisor(name);
477463
}
478-
addAdvisorOnChainCreation(advice, name);
464+
addAdvisorOnChainCreation(advice);
479465
}
480466
}
481467
}
@@ -498,11 +484,10 @@ private List<Advisor> freshAdvisorChain() {
498484
if (logger.isDebugEnabled()) {
499485
logger.debug("Refreshing bean named '" + pa.getBeanName() + "'");
500486
}
501-
// Replace the placeholder with a fresh prototype instance resulting
502-
// from a getBean() lookup
487+
// Replace the placeholder with a fresh prototype instance resulting from a getBean lookup
503488
if (this.beanFactory == null) {
504-
throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " +
505-
"- cannot resolve prototype advisor '" + pa.getBeanName() + "'");
489+
throw new IllegalStateException("No BeanFactory available anymore (probably due to " +
490+
"serialization) - cannot resolve prototype advisor '" + pa.getBeanName() + "'");
506491
}
507492
Object bean = this.beanFactory.getBean(pa.getBeanName());
508493
Advisor refreshedAdvisor = namedBeanToAdvisor(bean);
@@ -519,28 +504,26 @@ private List<Advisor> freshAdvisorChain() {
519504
/**
520505
* Add all global interceptors and pointcuts.
521506
*/
522-
private void addGlobalAdvisor(ListableBeanFactory beanFactory, String prefix) {
507+
private void addGlobalAdvisors(ListableBeanFactory beanFactory, String prefix) {
523508
String[] globalAdvisorNames =
524509
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, Advisor.class);
525510
String[] globalInterceptorNames =
526511
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, Interceptor.class);
527-
List<Object> beans = new ArrayList<>(globalAdvisorNames.length + globalInterceptorNames.length);
528-
Map<Object, String> names = new HashMap<>(beans.size());
529-
for (String name : globalAdvisorNames) {
530-
Object bean = beanFactory.getBean(name);
531-
beans.add(bean);
532-
names.put(bean, name);
533-
}
534-
for (String name : globalInterceptorNames) {
535-
Object bean = beanFactory.getBean(name);
536-
beans.add(bean);
537-
names.put(bean, name);
538-
}
539-
AnnotationAwareOrderComparator.sort(beans);
540-
for (Object bean : beans) {
541-
String name = names.get(bean);
542-
if (name.startsWith(prefix)) {
543-
addAdvisorOnChainCreation(bean, name);
512+
if (globalAdvisorNames.length > 0 || globalInterceptorNames.length > 0) {
513+
List<Object> beans = new ArrayList<>(globalAdvisorNames.length + globalInterceptorNames.length);
514+
for (String name : globalAdvisorNames) {
515+
if (name.startsWith(prefix)) {
516+
beans.add(beanFactory.getBean(name));
517+
}
518+
}
519+
for (String name : globalInterceptorNames) {
520+
if (name.startsWith(prefix)) {
521+
beans.add(beanFactory.getBean(name));
522+
}
523+
}
524+
AnnotationAwareOrderComparator.sort(beans);
525+
for (Object bean : beans) {
526+
addAdvisorOnChainCreation(bean);
544527
}
545528
}
546529
}
@@ -551,17 +534,11 @@ private void addGlobalAdvisor(ListableBeanFactory beanFactory, String prefix) {
551534
* Because of these three possibilities, we can't type the signature
552535
* more strongly.
553536
* @param next advice, advisor or target object
554-
* @param name bean name from which we obtained this object in our owning
555-
* bean factory
556537
*/
557-
private void addAdvisorOnChainCreation(Object next, String name) {
538+
private void addAdvisorOnChainCreation(Object next) {
558539
// We need to convert to an Advisor if necessary so that our source reference
559540
// matches what we find from superclass interceptors.
560-
Advisor advisor = namedBeanToAdvisor(next);
561-
if (logger.isTraceEnabled()) {
562-
logger.trace("Adding advisor with name '" + name + "'");
563-
}
564-
addAdvisor(advisor);
541+
addAdvisor(namedBeanToAdvisor(next));
565542
}
566543

567544
/**
@@ -572,9 +549,7 @@ private void addAdvisorOnChainCreation(Object next, String name) {
572549
*/
573550
private TargetSource freshTargetSource() {
574551
if (this.targetName == null) {
575-
if (logger.isTraceEnabled()) {
576-
logger.trace("Not refreshing target: Bean name not specified in 'interceptorNames'.");
577-
}
552+
// Not refreshing target: bean name not specified in 'interceptorNames'
578553
return this.targetSource;
579554
}
580555
else {
@@ -602,8 +577,8 @@ private Advisor namedBeanToAdvisor(Object next) {
602577
// We expected this to be an Advisor or Advice,
603578
// but it wasn't. This is a configuration error.
604579
throw new AopConfigException("Unknown advisor type " + next.getClass() +
605-
"; Can only include Advisor or Advice type beans in interceptorNames chain except for last entry," +
606-
"which may also be target or TargetSource", ex);
580+
"; can only include Advisor or Advice type beans in interceptorNames chain " +
581+
"except for last entry which may also be target instance or TargetSource", ex);
607582
}
608583
}
609584

@@ -614,7 +589,7 @@ private Advisor namedBeanToAdvisor(Object next) {
614589
protected void adviceChanged() {
615590
super.adviceChanged();
616591
if (this.singleton) {
617-
logger.debug("Advice has changed; recaching singleton instance");
592+
logger.debug("Advice has changed; re-caching singleton instance");
618593
synchronized (this) {
619594
this.singletonInstance = null;
620595
}

spring-context/src/main/java/org/springframework/context/annotation/Configuration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -344,9 +344,9 @@
344344
*
345345
* <p>By default, {@code @Bean} methods will be <em>eagerly instantiated</em> at container
346346
* bootstrap time. To avoid this, {@code @Configuration} may be used in conjunction with
347-
* the {@link Lazy @Lazy} annotation to indicate that all {@code @Bean} methods declared within
348-
* the class are by default lazily initialized. Note that {@code @Lazy} may be used on
349-
* individual {@code @Bean} methods as well.
347+
* the {@link Lazy @Lazy} annotation to indicate that all {@code @Bean} methods declared
348+
* within the class are by default lazily initialized. Note that {@code @Lazy} may be used
349+
* on individual {@code @Bean} methods as well.
350350
*
351351
* <h2>Testing support for {@code @Configuration} classes</h2>
352352
*

spring-context/src/main/java/org/springframework/context/annotation/PropertySource.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -170,8 +170,11 @@
170170
public @interface PropertySource {
171171

172172
/**
173-
* Indicate the name of this property source. If omitted, a name will
174-
* be generated based on the description of the underlying resource.
173+
* Indicate the name of this property source. If omitted, the {@link #factory()}
174+
* will generate a name based on the underlying resource (in the case of
175+
* {@link org.springframework.core.io.support.DefaultPropertySourceFactory}:
176+
* derived from the resource description through a corresponding name-less
177+
* {@link org.springframework.core.io.support.ResourcePropertySource} constructor).
175178
* @see org.springframework.core.env.PropertySource#getName()
176179
* @see org.springframework.core.io.Resource#getDescription()
177180
*/

spring-core/src/main/java/org/springframework/core/io/support/PropertySourceFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,8 @@ public interface PropertySourceFactory {
3333
/**
3434
* Create a {@link PropertySource} that wraps the given resource.
3535
* @param name the name of the property source
36+
* (can be {@code null} in which case the factory implementation
37+
* will have to generate a name based on the given resource)
3638
* @param resource the resource (potentially encoded) to wrap
3739
* @return the new {@link PropertySource} (never {@code null})
3840
* @throws IOException if resource resolution failed

spring-core/src/main/java/org/springframework/util/ClassUtils.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -532,7 +532,7 @@ public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) {
532532
* @param lhsType the target type
533533
* @param rhsType the value type that should be assigned to the target type
534534
* @return if the target type is assignable from the value type
535-
* @see TypeUtils#isAssignable
535+
* @see TypeUtils#isAssignable(java.lang.reflect.Type, java.lang.reflect.Type)
536536
*/
537537
public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
538538
Assert.notNull(lhsType, "Left-hand side type must not be null");
@@ -542,17 +542,12 @@ public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
542542
}
543543
if (lhsType.isPrimitive()) {
544544
Class<?> resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
545-
if (lhsType == resolvedPrimitive) {
546-
return true;
547-
}
545+
return (lhsType == resolvedPrimitive);
548546
}
549547
else {
550548
Class<?> resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
551-
if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) {
552-
return true;
553-
}
549+
return (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper));
554550
}
555-
return false;
556551
}
557552

558553
/**
@@ -1064,7 +1059,7 @@ public static String getQualifiedMethodName(Method method, @Nullable Class<?> cl
10641059
* @param clazz the clazz to analyze
10651060
* @param paramTypes the parameter types of the method
10661061
* @return whether the class has a corresponding constructor
1067-
* @see Class#getMethod
1062+
* @see Class#getConstructor
10681063
*/
10691064
public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) {
10701065
return (getConstructorIfAvailable(clazz, paramTypes) != null);

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -138,6 +138,7 @@ public DataSourceTransactionManager(DataSource dataSource) {
138138
afterPropertiesSet();
139139
}
140140

141+
141142
/**
142143
* Set the JDBC DataSource that this instance should manage transactions for.
143144
* <p>This will typically be a locally defined DataSource, for example an
@@ -409,13 +410,9 @@ protected void prepareTransactionalConnection(Connection con, TransactionDefinit
409410
throws SQLException {
410411

411412
if (isEnforceReadOnly() && definition.isReadOnly()) {
412-
Statement stmt = con.createStatement();
413-
try {
413+
try (Statement stmt = con.createStatement()) {
414414
stmt.executeUpdate("SET TRANSACTION READ ONLY");
415415
}
416-
finally {
417-
stmt.close();
418-
}
419416
}
420417
}
421418

0 commit comments

Comments
 (0)