-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(spring): allow bean injection by constructor
Allow to instanciate this type of bean https://github.com/gravitee-io/gravitee-federation-agent/blob/2.7.1/gravitee-federation-agent-standalone/gravitee-federation-agent-standalone-container/src/main/java/com/graviteesource/integration/agent/standalone/probes/StatusProbe.java - fully rely on Spring to instanciate beans (by constructors, fields or any else) - spring already inject ApplicationContext if need - should allow usage of @PostConstruct - use more simple function to get all beans for a type APIM-7022
- Loading branch information
1 parent
c5f409f
commit a7ac22b
Showing
3 changed files
with
124 additions
and
40 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
113 changes: 113 additions & 0 deletions
113
src/test/java/io/gravitee/common/spring/factory/SpringFactoriesLoaderTest.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,113 @@ | ||
/* | ||
* Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* 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 io.gravitee.common.spring.factory; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import javax.annotation.PostConstruct; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Configuration | ||
class SpringFactoriesLoaderTest { | ||
|
||
@Service | ||
static class MyServiceDeps {} | ||
|
||
interface AService { | ||
ApplicationContext getApplicationContext(); | ||
|
||
MyServiceDeps getMyServiceDeps(); | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@Setter | ||
@Getter | ||
@Order(12) | ||
@Service | ||
static class MyService implements ApplicationContextAware, AService { | ||
|
||
private final MyServiceDeps myServiceDeps; | ||
private ApplicationContext applicationContext; | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@Setter | ||
@Getter | ||
@Order(1) | ||
@Service | ||
static class MyAnotherService implements ApplicationContextAware, AService { | ||
|
||
private final MyServiceDeps myServiceDeps; | ||
private ApplicationContext applicationContext; | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
@Order(3) | ||
@Service | ||
static class MyYetAnotherService implements AService, InitializingBean { | ||
|
||
private MyServiceDeps myServiceDeps; | ||
private final ApplicationContext applicationContext; | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
myServiceDeps = new MyServiceDeps(); | ||
} | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
@Order(5) | ||
@Service | ||
static class MyYetYetAnotherService implements AService { | ||
|
||
private MyServiceDeps myServiceDeps; | ||
private final ApplicationContext applicationContext; | ||
|
||
@PostConstruct | ||
public void setup() { | ||
myServiceDeps = new MyServiceDeps(); | ||
} | ||
} | ||
|
||
@Test | ||
void createSpringFactoriesInstances() { | ||
// Given | ||
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringFactoriesLoaderTest.class); | ||
|
||
// When | ||
var svc = SpringFactoriesLoader.createSpringFactoriesInstances(ctx, AService.class); | ||
|
||
// Then | ||
assertThat(svc).hasSize(4); | ||
for (AService s : svc) { | ||
assertThat(s.getApplicationContext()).isNotNull(); | ||
assertThat(s.getMyServiceDeps()).isNotNull(); | ||
} | ||
assertThat(svc.stream().mapToInt(o -> o.getClass().getAnnotation(Order.class).value())).isSorted(); | ||
} | ||
} |