Skip to content

[SPR-9375] - Supporting reading annotations from the class of the object. #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,7 @@
/**
* Abstract implementation of {@link TransactionAttributeSource} that caches
* attributes for methods and implements a fallback policy: 1. specific target
* method; 2. target class; 3. declaring method; 4. declaring class/interface.
* method; 2. target class; 3 user class; 4. declaring method; 5. declaring class/interface.
*
* <p>Defaults to using the target class's transaction attribute if none is
* associated with the target method. Any transaction attribute associated with
Expand Down Expand Up @@ -154,6 +154,12 @@ private TransactionAttribute computeTransactionAttribute(Method method, Class<?>
return txAtt;
}

// Third try is the transaction attribute on the target class.
txAtt = findTransactionAttribute(userClass);
if (txAtt != null) {
return txAtt;
}

if (specificMethod != method) {
// Fallback is to look at the original method.
txAtt = findTransactionAttribute(method);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.interceptor;

/**
* Intermediate class of the hierarchy. Implements the interface but has no annotation.
*
* @author Jose Ignacio Gil Jaldo
*/
public class SimpleBaseClass implements SimpleTestInterface {

public void justAMethod() {
// nothing to do
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.interceptor;

import org.springframework.transaction.annotation.Transactional;

/**
* Class that annotates with @Transactional.
*
* @author Jose Ignacio Gil Jaldo
*/
@Transactional(value="theTransactionManager")
public class SimpleSubClass extends SimpleBaseClass {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.interceptor;

/**
* Base interface of the test case hierarchy.
*
* @author Jose Ignacio Gil Jaldo
*/
public interface SimpleTestInterface {
void justAMethod();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,13 +19,15 @@
import static org.junit.Assert.*;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;

/**
* Unit tests for the various {@link TransactionAttributeSource} implementations.
Expand Down Expand Up @@ -158,5 +160,23 @@ public void testNameMatchTransactionAttributeSourceWithEmptyMethodName() throws
Object.class.getMethod("hashCode", (Class[]) null), null);
assertNull(ta);
}

/**
* SPR-9375: we recover @Transactional information for the following case
* - A Interface
* - B Abstract class implementing interface
* - C Class extending B and annotated with @Transactional
*
* If we try to recover the information from a method defined in A and implemented
* in B that is not annotated we should get the information coming from the @Transactional
* annotation in C.
*/
public void testTransactionalAnnotationFromParentClassIsTaken() throws Exception {
AnnotationTransactionAttributeSource source = new AnnotationTransactionAttributeSource();
Method searchedMethod = SimpleTestInterface.class.getMethod("justAMethod");
TransactionAttribute attribute =
source.getTransactionAttribute(searchedMethod, SimpleSubClass.class);
assertEquals("theTransactionManager", attribute.getQualifier());
}

}