Browse Source
Provide context during repository method invocation to potential consumers. See: #3090 Original Pull Request: #3093pull/3136/head
8 changed files with 335 additions and 36 deletions
@ -0,0 +1,79 @@
@@ -0,0 +1,79 @@
|
||||
/* |
||||
* Copyright 2024 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 |
||||
* |
||||
* https://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.data.repository.core.support; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
import org.springframework.data.repository.core.RepositoryMetadata; |
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.transaction.support.TransactionSynchronizationManager; |
||||
|
||||
/** |
||||
* @author Christoph Strobl |
||||
*/ |
||||
class DefaultRepositoryMethodMetadata implements RepositoryMethodMetadata { |
||||
|
||||
private final RepositoryMetadata repositoryMetadata; |
||||
private final MethodMetadata methodMetadata; |
||||
|
||||
DefaultRepositoryMethodMetadata(RepositoryMetadata repositoryMetadata, MethodMetadata methodMetadata) { |
||||
|
||||
this.repositoryMetadata = repositoryMetadata; |
||||
this.methodMetadata = methodMetadata; |
||||
} |
||||
|
||||
static DefaultRepositoryMethodMetadata repositoryMethodMetadata(RepositoryMetadata repositoryMetadata, |
||||
Method declaredMethod) { |
||||
|
||||
return repositoryMethodMetadata(repositoryMetadata, declaredMethod, null); |
||||
} |
||||
|
||||
static DefaultRepositoryMethodMetadata repositoryMethodMetadata(RepositoryMetadata repositoryMetadata, |
||||
Method declaredMethod, @Nullable Method targetMethod) { |
||||
|
||||
return new DefaultRepositoryMethodMetadata(repositoryMetadata, |
||||
new DefaultMethodMetadata(declaredMethod, targetMethod)); |
||||
} |
||||
|
||||
static void bind(RepositoryMethodMetadata metadata) { |
||||
TransactionSynchronizationManager.bindResource(RepositoryMethodMetadata.class, metadata); |
||||
} |
||||
|
||||
static void unbind() { |
||||
TransactionSynchronizationManager.unbindResourceIfPossible(RepositoryMethodMetadata.class); |
||||
} |
||||
|
||||
@Override |
||||
public RepositoryMetadata repository() { |
||||
return repositoryMetadata; |
||||
} |
||||
|
||||
@Override |
||||
public MethodMetadata method() { |
||||
return methodMetadata; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "DefaultRepositoryMethodMetadata{" + "repository=" + repositoryMetadata.getRepositoryInterface() |
||||
+ ", domainType=" + repositoryMetadata.getDomainType() + ", invokedMethod=" + methodMetadata.declaredMethod() |
||||
+ ", targetMethod=" + methodMetadata.targetMethod() + '}'; |
||||
} |
||||
|
||||
record DefaultMethodMetadata(Method declaredMethod, @Nullable Method targetMethod) implements MethodMetadata { |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
/* |
||||
* Copyright 2024 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 |
||||
* |
||||
* https://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.data.repository.core.support; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
import org.springframework.data.repository.core.RepositoryMetadata; |
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.transaction.support.TransactionSynchronizationManager; |
||||
|
||||
/** |
||||
* @author Christoph Strobl |
||||
*/ |
||||
public interface RepositoryMethodMetadata { |
||||
|
||||
@Nullable |
||||
static RepositoryMethodMetadata get() { |
||||
return (RepositoryMethodMetadata) TransactionSynchronizationManager.getResource(RepositoryMethodMetadata.class); |
||||
} |
||||
|
||||
MethodMetadata method(); |
||||
|
||||
RepositoryMetadata repository(); |
||||
|
||||
interface MethodMetadata { |
||||
|
||||
Method declaredMethod(); |
||||
@Nullable Method targetMethod(); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue