cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kelv...@apache.org
Subject git commit: Add autowiring+AOP support to injected components
Date Fri, 02 Nov 2012 21:49:52 GMT
Updated Branches:
  refs/heads/asf_javelin_spring 3afbb0fcc -> 677e70d88


Add autowiring+AOP support to injected components


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/677e70d8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/677e70d8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/677e70d8

Branch: refs/heads/asf_javelin_spring
Commit: 677e70d88ba19f4079b7e28f34956a6ad9f565d1
Parents: 3afbb0f
Author: Kelven Yang <kelveny@gmail.com>
Authored: Fri Nov 2 14:49:32 2012 -0700
Committer: Kelven Yang <kelveny@gmail.com>
Committed: Fri Nov 2 14:49:32 2012 -0700

----------------------------------------------------------------------
 .../cloud/utils/component/ComponentContext.java    |   14 ++++++-
 utils/test/com/cloud/utils/db/DbAnnotatedBase.java |    4 ++
 utils/test/com/cloud/utils/db/DummyComponent.java  |   27 +++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/677e70d8/utils/src/com/cloud/utils/component/ComponentContext.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/component/ComponentContext.java b/utils/src/com/cloud/utils/component/ComponentContext.java
index 91990d4..6383509 100644
--- a/utils/src/com/cloud/utils/component/ComponentContext.java
+++ b/utils/src/com/cloud/utils/component/ComponentContext.java
@@ -20,6 +20,7 @@ package com.cloud.utils.component;
 import org.springframework.aop.Advisor;
 import org.springframework.aop.framework.ProxyFactory;
 import org.springframework.aop.support.DefaultPointcutAdvisor;
+import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.stereotype.Component;
@@ -53,15 +54,24 @@ public class ComponentContext implements ApplicationContextAware {
     	assert(s_appContext != null);
     	return (T)s_appContext.getBean(beanType);
     }
+
+    public static<T> T inject(Class<T> clz) {
+    	T instance = s_appContext.getAutowireCapableBeanFactory().createBean(clz);
+    	return inject(instance);
+    }
     
     public static<T> T inject(Object instance) {
+    	// autowire dynamically loaded object
+    	AutowireCapableBeanFactory  beanFactory = s_appContext.getAutowireCapableBeanFactory();
+    	beanFactory.autowireBean(instance);
+
     	Advisor advisor = new DefaultPointcutAdvisor(new MatchAnyMethodPointcut(),
     			new TransactionContextBuilder());
+    
     	ProxyFactory pf = new ProxyFactory();
-    	
         pf.setTarget(instance);
         pf.addAdvisor(advisor);
         
-        return (T)pf.getProxy();
+        return (T)pf.getProxy();        
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/677e70d8/utils/test/com/cloud/utils/db/DbAnnotatedBase.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/db/DbAnnotatedBase.java b/utils/test/com/cloud/utils/db/DbAnnotatedBase.java
index 2160a35..f87b20f 100644
--- a/utils/test/com/cloud/utils/db/DbAnnotatedBase.java
+++ b/utils/test/com/cloud/utils/db/DbAnnotatedBase.java
@@ -18,6 +18,7 @@ package com.cloud.utils.db;
 
 
 import javax.annotation.PostConstruct;
+import javax.inject.Inject;
 
 import junit.framework.Assert;
 
@@ -29,6 +30,8 @@ import org.springframework.stereotype.Component;
 public class DbAnnotatedBase {
     private static final Logger s_logger = Logger.getLogger(DbAnnotatedBase.class);
 
+    @Inject DummyComponent _dummy;
+    
     @PostConstruct
     public void initTest() {
     	Assert.assertTrue(true);
@@ -36,5 +39,6 @@ public class DbAnnotatedBase {
     
     public void MethodWithClassDbAnnotated() {
     	s_logger.info("called");
+    	_dummy.sayHello();
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/677e70d8/utils/test/com/cloud/utils/db/DummyComponent.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/db/DummyComponent.java b/utils/test/com/cloud/utils/db/DummyComponent.java
new file mode 100644
index 0000000..2922630
--- /dev/null
+++ b/utils/test/com/cloud/utils/db/DummyComponent.java
@@ -0,0 +1,27 @@
+// 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
+// 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 com.cloud.utils.db;
+
+import org.springframework.stereotype.Component;
+
+@Component
+public class DummyComponent {
+
+	public void sayHello() {
+		System.out.println("Hello, world");
+	}
+}


Mime
View raw message