Browse Source

Polishing

(cherry picked from commit 8e571decc1)
pull/1946/head
Juergen Hoeller 7 years ago
parent
commit
42dbc39032
  1. 35
      spring-context/src/main/java/org/springframework/cache/annotation/CacheAnnotationParser.java
  2. 22
      spring-context/src/main/java/org/springframework/cache/interceptor/AbstractFallbackCacheOperationSource.java
  3. 12
      spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java
  4. 8
      spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java
  5. 37
      spring-tx/src/main/java/org/springframework/transaction/interceptor/AbstractFallbackTransactionAttributeSource.java
  6. 18
      spring-tx/src/test/java/org/springframework/transaction/interceptor/MapTransactionAttributeSource.java

35
spring-context/src/main/java/org/springframework/cache/annotation/CacheAnnotationParser.java vendored

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,41 +24,40 @@ import org.springframework.lang.Nullable;
/** /**
* Strategy interface for parsing known caching annotation types. * Strategy interface for parsing known caching annotation types.
* {@link AnnotationCacheOperationSource} delegates to such * {@link AnnotationCacheOperationSource} delegates to such parsers
* parsers for supporting specific annotation types such as Spring's own * for supporting specific annotation types such as Spring's own
* {@link Cacheable}, {@link CachePut} or {@link CacheEvict}. * {@link Cacheable}, {@link CachePut} and{@link CacheEvict}.
* *
* @author Costin Leau * @author Costin Leau
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 3.1 * @since 3.1
* @see AnnotationCacheOperationSource
* @see SpringCacheAnnotationParser
*/ */
public interface CacheAnnotationParser { public interface CacheAnnotationParser {
/** /**
* Parses the cache definition for the given class, * Parse the cache definition for the given class,
* based on a known annotation type. * based on an annotation type understood by this parser.
* <p>This essentially parses a known cache annotation into Spring's * <p>This essentially parses a known cache annotation into Spring's metadata
* metadata attribute class. Returns {@code null} if the class * attribute class. Returns {@code null} if the class is not cacheable.
* is not cacheable.
* @param type the annotated class * @param type the annotated class
* @return CacheOperation the configured caching operation, * @return the configured caching operation, or {@code null} if none found
* or {@code null} if none was found
* @see AnnotationCacheOperationSource#findCacheOperations(Class) * @see AnnotationCacheOperationSource#findCacheOperations(Class)
*/ */
@Nullable @Nullable
Collection<CacheOperation> parseCacheAnnotations(Class<?> type); Collection<CacheOperation> parseCacheAnnotations(Class<?> type);
/** /**
* Parses the cache definition for the given method, * Parse the cache definition for the given method,
* based on a known annotation type. * based on an annotation type understood by this parser.
* <p>This essentially parses a known cache annotation into Spring's * <p>This essentially parses a known cache annotation into Spring's metadata
* metadata attribute class. Returns {@code null} if the method * attribute class. Returns {@code null} if the method is not cacheable.
* is not cacheable.
* @param method the annotated method * @param method the annotated method
* @return CacheOperation the configured caching operation, * @return the configured caching operation, or {@code null} if none found
* or {@code null} if none was found
* @see AnnotationCacheOperationSource#findCacheOperations(Method) * @see AnnotationCacheOperationSource#findCacheOperations(Method)
*/ */
@Nullable @Nullable
Collection<CacheOperation> parseCacheAnnotations(Method method); Collection<CacheOperation> parseCacheAnnotations(Method method);
} }

22
spring-context/src/main/java/org/springframework/cache/interceptor/AbstractFallbackCacheOperationSource.java vendored

@ -163,24 +163,22 @@ public abstract class AbstractFallbackCacheOperationSource implements CacheOpera
/** /**
* Subclasses need to implement this to return the caching attribute * Subclasses need to implement this to return the caching attribute for the
* for the given method, if any. * given class, if any.
* @param method the method to retrieve the attribute for * @param clazz the class to retrieve the attribute for
* @return all caching attribute associated with this method * @return all caching attribute associated with this class, or {@code null} if none
* (or {@code null} if none)
*/ */
@Nullable @Nullable
protected abstract Collection<CacheOperation> findCacheOperations(Method method); protected abstract Collection<CacheOperation> findCacheOperations(Class<?> clazz);
/** /**
* Subclasses need to implement this to return the caching attribute * Subclasses need to implement this to return the caching attribute for the
* for the given class, if any. * given method, if any.
* @param clazz the class to retrieve the attribute for * @param method the method to retrieve the attribute for
* @return all caching attribute associated with this class * @return all caching attribute associated with this method, or {@code null} if none
* (or {@code null} if none)
*/ */
@Nullable @Nullable
protected abstract Collection<CacheOperation> findCacheOperations(Class<?> clazz); protected abstract Collection<CacheOperation> findCacheOperations(Method method);
/** /**
* Should only public methods be allowed to have caching semantics? * Should only public methods be allowed to have caching semantics?

12
spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -96,7 +96,6 @@ public class EnableAsyncTests {
fail("Should have thrown UnsatisfiedDependencyException"); fail("Should have thrown UnsatisfiedDependencyException");
} }
catch (UnsatisfiedDependencyException ex) { catch (UnsatisfiedDependencyException ex) {
ex.printStackTrace();
assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException); assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException);
} }
} }
@ -111,7 +110,6 @@ public class EnableAsyncTests {
fail("Should have thrown UnsatisfiedDependencyException"); fail("Should have thrown UnsatisfiedDependencyException");
} }
catch (UnsatisfiedDependencyException ex) { catch (UnsatisfiedDependencyException ex) {
ex.printStackTrace();
assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException); assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException);
} }
} }
@ -218,8 +216,8 @@ public class EnableAsyncTests {
ctx.close(); ctx.close();
} }
@Test @Test // SPR-14949
public void spr14949FindsOnInterfaceWithInterfaceProxy() throws InterruptedException { public void findOnInterfaceWithInterfaceProxy() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigA.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigA.class);
AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class); AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class);
@ -230,8 +228,8 @@ public class EnableAsyncTests {
ctx.close(); ctx.close();
} }
@Test @Test // SPR-14949
public void spr14949FindsOnInterfaceWithCglibProxy() throws InterruptedException { public void findOnInterfaceWithCglibProxy() throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigB.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigB.class);
AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class); AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class);

8
spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java

@ -135,14 +135,14 @@ public class AnnotationTransactionAttributeSource extends AbstractFallbackTransa
@Override @Override
@Nullable @Nullable
protected TransactionAttribute findTransactionAttribute(Method method) { protected TransactionAttribute findTransactionAttribute(Class<?> clazz) {
return determineTransactionAttribute(method); return determineTransactionAttribute(clazz);
} }
@Override @Override
@Nullable @Nullable
protected TransactionAttribute findTransactionAttribute(Class<?> clazz) { protected TransactionAttribute findTransactionAttribute(Method method) {
return determineTransactionAttribute(clazz); return determineTransactionAttribute(method);
} }
/** /**

37
spring-tx/src/main/java/org/springframework/transaction/interceptor/AbstractFallbackTransactionAttributeSource.java

@ -55,7 +55,13 @@ public abstract class AbstractFallbackTransactionAttributeSource implements Tran
* Canonical value held in cache to indicate no transaction attribute was * Canonical value held in cache to indicate no transaction attribute was
* found for this method, and we don't need to look again. * found for this method, and we don't need to look again.
*/ */
private static final TransactionAttribute NULL_TRANSACTION_ATTRIBUTE = new DefaultTransactionAttribute(); @SuppressWarnings("serial")
private static final TransactionAttribute NULL_TRANSACTION_ATTRIBUTE = new DefaultTransactionAttribute() {
@Override
public String toString() {
return "null";
}
};
/** /**
@ -78,7 +84,7 @@ public abstract class AbstractFallbackTransactionAttributeSource implements Tran
* <p>Defaults to the class's transaction attribute if no method attribute is found. * <p>Defaults to the class's transaction attribute if no method attribute is found.
* @param method the method for the current invocation (never {@code null}) * @param method the method for the current invocation (never {@code null})
* @param targetClass the target class for this invocation (may be {@code null}) * @param targetClass the target class for this invocation (may be {@code null})
* @return TransactionAttribute for this method, or {@code null} if the method * @return a TransactionAttribute for this method, or {@code null} if the method
* is not transactional * is not transactional
*/ */
@Override @Override
@ -90,7 +96,7 @@ public abstract class AbstractFallbackTransactionAttributeSource implements Tran
// First, see if we have a cached value. // First, see if we have a cached value.
Object cacheKey = getCacheKey(method, targetClass); Object cacheKey = getCacheKey(method, targetClass);
Object cached = this.attributeCache.get(cacheKey); TransactionAttribute cached = this.attributeCache.get(cacheKey);
if (cached != null) { if (cached != null) {
// Value will either be canonical value indicating there is no transaction attribute, // Value will either be canonical value indicating there is no transaction attribute,
// or an actual transaction attribute. // or an actual transaction attribute.
@ -98,7 +104,7 @@ public abstract class AbstractFallbackTransactionAttributeSource implements Tran
return null; return null;
} }
else { else {
return (TransactionAttribute) cached; return cached;
} }
} }
else { else {
@ -182,25 +188,22 @@ public abstract class AbstractFallbackTransactionAttributeSource implements Tran
/** /**
* Subclasses need to implement this to return the transaction attribute * Subclasses need to implement this to return the transaction attribute for the
* for the given method, if any. * given class, if any.
* @param method the method to retrieve the attribute for * @param clazz the class to retrieve the attribute for
* @return all transaction attribute associated with this method * @return all transaction attribute associated with this class, or {@code null} if none
* (or {@code null} if none)
*/ */
@Nullable @Nullable
protected abstract TransactionAttribute findTransactionAttribute(Method method); protected abstract TransactionAttribute findTransactionAttribute(Class<?> clazz);
/** /**
* Subclasses need to implement this to return the transaction attribute * Subclasses need to implement this to return the transaction attribute for the
* for the given class, if any. * given method, if any.
* @param clazz the class to retrieve the attribute for * @param method the method to retrieve the attribute for
* @return all transaction attribute associated with this class * @return all transaction attribute associated with this method, or {@code null} if none
* (or {@code null} if none)
*/ */
@Nullable @Nullable
protected abstract TransactionAttribute findTransactionAttribute(Class<?> clazz); protected abstract TransactionAttribute findTransactionAttribute(Method method);
/** /**
* Should only public methods be allowed to have transactional semantics? * Should only public methods be allowed to have transactional semantics?

18
spring-tx/src/test/java/org/springframework/transaction/interceptor/MapTransactionAttributeSource.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,23 +31,23 @@ public class MapTransactionAttributeSource extends AbstractFallbackTransactionAt
private final Map<Object, TransactionAttribute> attributeMap = new HashMap<>(); private final Map<Object, TransactionAttribute> attributeMap = new HashMap<>();
public void register(Method method, TransactionAttribute txAttr) {
this.attributeMap.put(method, txAttr);
}
public void register(Class<?> clazz, TransactionAttribute txAttr) { public void register(Class<?> clazz, TransactionAttribute txAttr) {
this.attributeMap.put(clazz, txAttr); this.attributeMap.put(clazz, txAttr);
} }
public void register(Method method, TransactionAttribute txAttr) {
@Override this.attributeMap.put(method, txAttr);
protected TransactionAttribute findTransactionAttribute(Method method) {
return this.attributeMap.get(method);
} }
@Override @Override
protected TransactionAttribute findTransactionAttribute(Class<?> clazz) { protected TransactionAttribute findTransactionAttribute(Class<?> clazz) {
return this.attributeMap.get(clazz); return this.attributeMap.get(clazz);
} }
@Override
protected TransactionAttribute findTransactionAttribute(Method method) {
return this.attributeMap.get(method);
}
} }

Loading…
Cancel
Save