Reinstate support for @javax.annotation.ManagedBean & @javax.inject.Named
This commit reinstates support for the legacy JSR-250 @javax.annotation.ManagedBean and JSR-330 @javax.inject.Named annotations with regard to component name lookups and component scanning. Closes gh-31090
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2002-2023 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 example.indexed;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@javax.annotation.ManagedBean
|
||||
public class IndexedJavaxManagedBeanComponent {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2002-2023 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 example.indexed;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@javax.inject.Named("myIndexedJavaxNamedComponent")
|
||||
public class IndexedJavaxNamedComponent {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2002-2023 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 example.scannable;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@javax.annotation.ManagedBean("myJavaxManagedBeanComponent")
|
||||
public class JavaxManagedBeanComponent {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2002-2023 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 example.scannable;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@javax.inject.Named("myJavaxNamedComponent")
|
||||
public class JavaxNamedComponent {
|
||||
}
|
||||
@@ -24,6 +24,8 @@ import java.lang.annotation.Target;
|
||||
import example.scannable.DefaultNamedComponent;
|
||||
import example.scannable.JakartaManagedBeanComponent;
|
||||
import example.scannable.JakartaNamedComponent;
|
||||
import example.scannable.JavaxManagedBeanComponent;
|
||||
import example.scannable.JavaxNamedComponent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
@@ -90,11 +92,21 @@ class AnnotationBeanNameGeneratorTests {
|
||||
assertGeneratedName(JakartaNamedComponent.class, "myJakartaNamedComponent");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateBeanNameWithJavaxNamedComponent() {
|
||||
assertGeneratedName(JavaxNamedComponent.class, "myJavaxNamedComponent");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateBeanNameWithJakartaManagedBeanComponent() {
|
||||
assertGeneratedName(JakartaManagedBeanComponent.class, "myJakartaManagedBeanComponent");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateBeanNameWithJavaxManagedBeanComponent() {
|
||||
assertGeneratedName(JavaxManagedBeanComponent.class, "myJavaxManagedBeanComponent");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateBeanNameWithCustomStereotypeComponent() {
|
||||
assertGeneratedName(DefaultNamedComponent.class, "thoreau");
|
||||
|
||||
@@ -29,6 +29,8 @@ import java.util.stream.Stream;
|
||||
import example.gh24375.AnnotatedComponent;
|
||||
import example.indexed.IndexedJakartaManagedBeanComponent;
|
||||
import example.indexed.IndexedJakartaNamedComponent;
|
||||
import example.indexed.IndexedJavaxManagedBeanComponent;
|
||||
import example.indexed.IndexedJavaxNamedComponent;
|
||||
import example.profilescan.DevComponent;
|
||||
import example.profilescan.ProfileAnnotatedComponent;
|
||||
import example.profilescan.ProfileMetaAnnotatedComponent;
|
||||
@@ -40,6 +42,8 @@ import example.scannable.FooService;
|
||||
import example.scannable.FooServiceImpl;
|
||||
import example.scannable.JakartaManagedBeanComponent;
|
||||
import example.scannable.JakartaNamedComponent;
|
||||
import example.scannable.JavaxManagedBeanComponent;
|
||||
import example.scannable.JavaxNamedComponent;
|
||||
import example.scannable.MessageBean;
|
||||
import example.scannable.NamedComponent;
|
||||
import example.scannable.NamedStubDao;
|
||||
@@ -100,9 +104,16 @@ class ClassPathScanningCandidateComponentProviderTests {
|
||||
JakartaManagedBeanComponent.class
|
||||
);
|
||||
|
||||
private static final Set<Class<?>> indexedJakartaComponents = Set.of(
|
||||
private static final Set<Class<?>> scannedJavaxComponents = Set.of(
|
||||
JavaxNamedComponent.class,
|
||||
JavaxManagedBeanComponent.class
|
||||
);
|
||||
|
||||
private static final Set<Class<?>> indexedComponents = Set.of(
|
||||
IndexedJakartaNamedComponent.class,
|
||||
IndexedJakartaManagedBeanComponent.class
|
||||
IndexedJakartaManagedBeanComponent.class,
|
||||
IndexedJavaxNamedComponent.class,
|
||||
IndexedJavaxManagedBeanComponent.class
|
||||
);
|
||||
|
||||
|
||||
@@ -111,25 +122,28 @@ class ClassPathScanningCandidateComponentProviderTests {
|
||||
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
|
||||
provider.setResourceLoader(new DefaultResourceLoader(
|
||||
CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())));
|
||||
testDefault(provider, TEST_BASE_PACKAGE, true, false);
|
||||
testDefault(provider, TEST_BASE_PACKAGE, true, true, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultsWithIndex() {
|
||||
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
|
||||
provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
|
||||
testDefault(provider, "example", true, true);
|
||||
testDefault(provider, "example", true, true, true);
|
||||
}
|
||||
|
||||
private void testDefault(ClassPathScanningCandidateComponentProvider provider, String basePackage,
|
||||
boolean includeScannedJakartaComponents, boolean includeIndexedJakartaComponents) {
|
||||
boolean includeScannedJakartaComponents, boolean includeScannedJavaxComponents, boolean includeIndexedComponents) {
|
||||
|
||||
Set<Class<?>> expectedTypes = new HashSet<>(springComponents);
|
||||
if (includeScannedJakartaComponents) {
|
||||
expectedTypes.addAll(scannedJakartaComponents);
|
||||
}
|
||||
if (includeIndexedJakartaComponents) {
|
||||
expectedTypes.addAll(indexedJakartaComponents);
|
||||
if (includeScannedJavaxComponents) {
|
||||
expectedTypes.addAll(scannedJavaxComponents);
|
||||
}
|
||||
if (includeIndexedComponents) {
|
||||
expectedTypes.addAll(indexedComponents);
|
||||
}
|
||||
|
||||
Set<BeanDefinition> candidates = provider.findCandidateComponents(basePackage);
|
||||
@@ -202,7 +216,7 @@ class ClassPathScanningCandidateComponentProviderTests {
|
||||
|
||||
private void testCustomAnnotationTypeIncludeFilter(ClassPathScanningCandidateComponentProvider provider) {
|
||||
provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
|
||||
testDefault(provider, TEST_BASE_PACKAGE, false, false);
|
||||
testDefault(provider, TEST_BASE_PACKAGE, false, false, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -295,7 +309,7 @@ class ClassPathScanningCandidateComponentProviderTests {
|
||||
Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
|
||||
assertScannedBeanDefinitions(candidates);
|
||||
assertBeanTypes(candidates, FooServiceImpl.class, StubFooDao.class, ServiceInvocationCounter.class,
|
||||
BarComponent.class, JakartaManagedBeanComponent.class);
|
||||
BarComponent.class, JakartaManagedBeanComponent.class, JavaxManagedBeanComponent.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user