forked from spring-projects/spring-framework
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure package-private init/destroy methods are always invoked
Prior to this commit, if an init/destroy method was package-private and declared in a superclass in a package different from the package in which the registered bean resided, a local init/destroy method with the same name would effectively "shadow" the method from the different package, resulting in only the local init/destroy method being invoked. This commit addresses this issue by tracking package-private init methods from different packages using their fully-qualified method names, analogous to the existing support for private init/destroy methods. Closes spring-projectsgh-30718
- Loading branch information
Showing
5 changed files
with
138 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...rc/test/java/org/springframework/context/annotation/lifecyclemethods/InitDestroyBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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 org.springframework.context.annotation.lifecyclemethods; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class InitDestroyBean { | ||
|
||
public final List<String> initMethods = new ArrayList<>(); | ||
public final List<String> destroyMethods = new ArrayList<>(); | ||
|
||
|
||
public void initMethod() { | ||
this.initMethods.add("initMethod"); | ||
} | ||
|
||
public void destroyMethod() { | ||
this.destroyMethods.add("destroyMethod"); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...rg/springframework/context/annotation/lifecyclemethods/PackagePrivateInitDestroyBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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 org.springframework.context.annotation.lifecyclemethods; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.annotation.PreDestroy; | ||
|
||
public class PackagePrivateInitDestroyBean extends InitDestroyBean { | ||
|
||
@PostConstruct | ||
void postConstruct() { | ||
this.initMethods.add("PackagePrivateInitDestroyBean.postConstruct"); | ||
} | ||
|
||
@PreDestroy | ||
void preDestroy() { | ||
this.destroyMethods.add("PackagePrivateInitDestroyBean.preDestroy"); | ||
} | ||
|
||
} |