Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #46 from adammelliott/inject-fields-during-before-…
Browse files Browse the repository at this point in the history
…initialization

Fix metric injection into AOP-advised beans
  • Loading branch information
ryantenney committed Nov 14, 2013
2 parents 8a2f1e5 + 6b54102 commit 285e1c0
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ public InjectMetricAnnotationBeanPostProcessor(final MetricRegistry metrics) {
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean;
}

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) {
public Object postProcessBeforeInitialization(final Object bean, String beanName) {
final Class<?> targetClass = AopUtils.getTargetClass(bean);

ReflectionUtils.doWithFields(targetClass, new FieldCallback() {
Expand Down Expand Up @@ -88,6 +83,11 @@ else if (Histogram.class == type) {
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;
}

@Override
public int getOrder() {
return LOWEST_PRECEDENCE - 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (C) 2012 Ryan W Tenney ([email protected])
*
* Licensed 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 com.ryantenney.metrics.spring;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import com.ryantenney.metrics.annotation.InjectMetric;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:aop-field-injection-interaction.xml")
public class AopFieldInjectionInteractionTest {

@Autowired private MetricRegistry metricRegistry;
@Autowired private TestAspectTarget target;

@Test
public void testFieldInjectionShouldNotCauseErrorWhenTargetIsAopProxy() throws Exception {
// verify that AOP interception is working
assertThat(target.targetMethod(), is(5));

assertThat(metricRegistry.getCounters(), hasKey("targetCounter"));
assertThat(metricRegistry.getCounters().get("targetCounter").getCount(), is(1L));
}

}

@Aspect
class TestAspectWrapper {

@Around("execution(* com.ryantenney.metrics.spring.TestAspectTarget.targetMethod())")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
joinPoint.proceed();
return 5;
}

}

interface TestAspectTarget {

public int targetMethod();

}

class TestAspectTargetImpl implements TestAspectTarget {

@InjectMetric(name = "targetCounter", absolute = true) private Counter counter;

@Override
public int targetMethod() {
this.counter.inc();
return 3;
}

}
1 change: 1 addition & 0 deletions src/test/java/com/ryantenney/metrics/spring/TestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
@RunWith(Suite.class)
// @formatter:off
@SuiteClasses({
AopFieldInjectionInteractionTest.class,
CovariantReturnTypeTest.class,
EnableMetricsTest.class,
HealthCheckTest.class,
Expand Down
37 changes: 37 additions & 0 deletions src/test/resources/aop-field-injection-interaction.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2012 Ryan W Tenney ([email protected])
Licensed 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:metrics="http://www.ryantenney.com/schema/metrics"

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.ryantenney.com/schema/metrics http://www.ryantenney.com/schema/metrics/metrics-3.0.xsd">

<aop:aspectj-autoproxy />

<metrics:annotation-driven />

<bean class="com.ryantenney.metrics.spring.TestAspectWrapper" />
<bean class="com.ryantenney.metrics.spring.TestAspectTargetImpl" />

</beans>

0 comments on commit 285e1c0

Please sign in to comment.