-
Notifications
You must be signed in to change notification settings - Fork 803
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SCB-2237] allow InjectProperties inject to spring bean instance (#2314)
- Loading branch information
Showing
23 changed files
with
597 additions
and
319 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
48 changes: 48 additions & 0 deletions
48
...on-config/src/main/java/org/apache/servicecomb/config/inject/InjectBeanPostProcessor.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,48 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.servicecomb.config.inject; | ||
|
||
import java.util.Collections; | ||
|
||
import org.apache.servicecomb.config.priority.PriorityPropertyManager; | ||
import org.apache.servicecomb.foundation.common.utils.BeanUtils; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class InjectBeanPostProcessor implements BeanPostProcessor { | ||
private final PriorityPropertyManager priorityPropertyManager; | ||
|
||
public InjectBeanPostProcessor(PriorityPropertyManager priorityPropertyManager) { | ||
this.priorityPropertyManager = priorityPropertyManager; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | ||
Class<?> beanCls = BeanUtils.getImplClassFromBean(bean); | ||
InjectProperties injectProperties = beanCls.getAnnotation(InjectProperties.class); | ||
if (injectProperties == null) { | ||
return bean; | ||
} | ||
|
||
priorityPropertyManager.createConfigObject(bean, Collections.emptyMap()); | ||
|
||
return bean; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../foundation-config/src/main/java/org/apache/servicecomb/config/priority/ConfigObject.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,39 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.servicecomb.config.priority; | ||
|
||
import java.util.List; | ||
|
||
public class ConfigObject<T> { | ||
private final T instance; | ||
|
||
private final List<ConfigObjectProperty> properties; | ||
|
||
public ConfigObject(T instance, List<ConfigObjectProperty> properties) { | ||
this.instance = instance; | ||
this.properties = properties; | ||
} | ||
|
||
public T getInstance() { | ||
return instance; | ||
} | ||
|
||
public List<ConfigObjectProperty> getProperties() { | ||
return properties; | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...ion-config/src/main/java/org/apache/servicecomb/config/priority/ConfigObjectProperty.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,38 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.servicecomb.config.priority; | ||
|
||
import org.apache.servicecomb.foundation.common.utils.bean.Setter; | ||
|
||
/** | ||
* do not reference config object instance, otherwise gc for weak hash map will failed | ||
*/ | ||
public class ConfigObjectProperty { | ||
private final Setter<Object, Object> setter; | ||
|
||
private final PriorityProperty<?> property; | ||
|
||
public ConfigObjectProperty(Setter<Object, Object> setter, PriorityProperty<?> property) { | ||
this.setter = setter; | ||
this.property = property; | ||
} | ||
|
||
public void updateValue(Object instance) { | ||
setter.set(instance, property.getValue()); | ||
} | ||
} |
Oops, something went wrong.