File tree Expand file tree Collapse file tree
src/com/cloud/utils/component Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020import org .springframework .aop .Advisor ;
2121import org .springframework .aop .framework .ProxyFactory ;
2222import org .springframework .aop .support .DefaultPointcutAdvisor ;
23+ import org .springframework .beans .factory .config .AutowireCapableBeanFactory ;
2324import org .springframework .context .ApplicationContext ;
2425import org .springframework .context .ApplicationContextAware ;
2526import 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}
Original file line number Diff line number Diff line change 1818
1919
2020import javax .annotation .PostConstruct ;
21+ import javax .inject .Inject ;
2122
2223import junit .framework .Assert ;
2324
2930public 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments