|
1 | 1 | /*
|
2 |
| - * Copyright 2016-2018 the original author or authors. |
| 2 | + * Copyright 2016-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import org.apache.commons.logging.LogFactory;
|
21 | 21 |
|
22 | 22 | import org.springframework.beans.factory.DisposableBean;
|
23 |
| -import org.springframework.beans.factory.FactoryBean; |
24 | 23 | import org.springframework.beans.factory.InitializingBean;
|
| 24 | +import org.springframework.beans.factory.config.AbstractFactoryBean; |
| 25 | +import org.springframework.context.Lifecycle; |
| 26 | +import org.springframework.context.SmartLifecycle; |
25 | 27 | import org.springframework.expression.spel.standard.SpelExpressionParser;
|
26 | 28 |
|
27 | 29 | /**
|
|
35 | 37 | * @since 5.0
|
36 | 38 | */
|
37 | 39 | public abstract class IntegrationComponentSpec<S extends IntegrationComponentSpec<S, T>, T>
|
38 |
| - implements FactoryBean<T>, InitializingBean, DisposableBean { |
| 40 | + extends AbstractFactoryBean<T> |
| 41 | + implements SmartLifecycle { |
39 | 42 |
|
40 | 43 | protected final static SpelExpressionParser PARSER = new SpelExpressionParser();
|
41 | 44 |
|
@@ -71,26 +74,73 @@ public T get() {
|
71 | 74 | }
|
72 | 75 |
|
73 | 76 | @Override
|
74 |
| - public T getObject() { |
75 |
| - return get(); |
| 77 | + public Class<?> getObjectType() { |
| 78 | + return get().getClass(); |
76 | 79 | }
|
77 | 80 |
|
78 | 81 | @Override
|
79 |
| - public Class<?> getObjectType() { |
80 |
| - return get().getClass(); |
| 82 | + protected T createInstance() throws Exception { |
| 83 | + T instance = get(); |
| 84 | + if (instance instanceof InitializingBean) { |
| 85 | + ((InitializingBean) instance).afterPropertiesSet(); |
| 86 | + } |
| 87 | + return instance; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected void destroyInstance(T instance) throws Exception { |
| 92 | + if (instance instanceof DisposableBean) { |
| 93 | + ((DisposableBean) instance).destroy(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void start() { |
| 99 | + T instance = get(); |
| 100 | + if (instance instanceof Lifecycle) { |
| 101 | + ((Lifecycle) instance).start(); |
| 102 | + } |
81 | 103 | }
|
82 | 104 |
|
83 | 105 | @Override
|
84 |
| - public void afterPropertiesSet() throws Exception { |
85 |
| - if (this.target instanceof InitializingBean) { |
86 |
| - ((InitializingBean) this.target).afterPropertiesSet(); |
| 106 | + public void stop() { |
| 107 | + T instance = get(); |
| 108 | + if (instance instanceof Lifecycle) { |
| 109 | + ((Lifecycle) instance).stop(); |
87 | 110 | }
|
88 | 111 | }
|
89 | 112 |
|
90 | 113 | @Override
|
91 |
| - public void destroy() throws Exception { |
92 |
| - if (this.target instanceof DisposableBean) { |
93 |
| - ((DisposableBean) this.target).destroy(); |
| 114 | + public boolean isRunning() { |
| 115 | + T instance = get(); |
| 116 | + return !(instance instanceof Lifecycle) || ((Lifecycle) instance).isRunning(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean isAutoStartup() { |
| 121 | + T instance = get(); |
| 122 | + return instance instanceof SmartLifecycle && ((SmartLifecycle) instance).isAutoStartup(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void stop(Runnable callback) { |
| 127 | + T instance = get(); |
| 128 | + if (instance instanceof SmartLifecycle) { |
| 129 | + ((SmartLifecycle) instance).stop(callback); |
| 130 | + } |
| 131 | + else { |
| 132 | + callback.run(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public int getPhase() { |
| 138 | + T instance = get(); |
| 139 | + if (instance instanceof SmartLifecycle) { |
| 140 | + return ((SmartLifecycle) instance).getPhase(); |
| 141 | + } |
| 142 | + else { |
| 143 | + return 0; |
94 | 144 | }
|
95 | 145 | }
|
96 | 146 |
|
|
0 commit comments