Skip to content

Commit 0e9924f

Browse files
committed
Add autowiring+AOP support to injected components
1 parent cea8f3b commit 0e9924f

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

utils/src/com/cloud/utils/component/ComponentContext.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.aop.Advisor;
2121
import org.springframework.aop.framework.ProxyFactory;
2222
import org.springframework.aop.support.DefaultPointcutAdvisor;
23+
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
2324
import org.springframework.context.ApplicationContext;
2425
import org.springframework.context.ApplicationContextAware;
2526
import org.springframework.stereotype.Component;
@@ -53,15 +54,24 @@ public static <T> T getCompanent(Class<T> beanType) {
5354
assert(s_appContext != null);
5455
return (T)s_appContext.getBean(beanType);
5556
}
57+
58+
public static<T> T inject(Class<T> clz) {
59+
T instance = s_appContext.getAutowireCapableBeanFactory().createBean(clz);
60+
return inject(instance);
61+
}
5662

5763
public static<T> T inject(Object instance) {
64+
// autowire dynamically loaded object
65+
AutowireCapableBeanFactory beanFactory = s_appContext.getAutowireCapableBeanFactory();
66+
beanFactory.autowireBean(instance);
67+
5868
Advisor advisor = new DefaultPointcutAdvisor(new MatchAnyMethodPointcut(),
5969
new TransactionContextBuilder());
70+
6071
ProxyFactory pf = new ProxyFactory();
61-
6272
pf.setTarget(instance);
6373
pf.addAdvisor(advisor);
6474

65-
return (T)pf.getProxy();
75+
return (T)pf.getProxy();
6676
}
6777
}

utils/test/com/cloud/utils/db/DbAnnotatedBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
import javax.annotation.PostConstruct;
21+
import javax.inject.Inject;
2122

2223
import junit.framework.Assert;
2324

@@ -29,12 +30,15 @@
2930
public class DbAnnotatedBase {
3031
private static final Logger s_logger = Logger.getLogger(DbAnnotatedBase.class);
3132

33+
@Inject DummyComponent _dummy;
34+
3235
@PostConstruct
3336
public void initTest() {
3437
Assert.assertTrue(true);
3538
}
3639

3740
public void MethodWithClassDbAnnotated() {
3841
s_logger.info("called");
42+
_dummy.sayHello();
3943
}
4044
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.utils.db;
18+
19+
import org.springframework.stereotype.Component;
20+
21+
@Component
22+
public class DummyComponent {
23+
24+
public void sayHello() {
25+
System.out.println("Hello, world");
26+
}
27+
}

0 commit comments

Comments
 (0)