Merge pull request #327 from olivergierke/SPR-10594
# By Oliver Gierke * SPR-10594: Add support for Objenesis proxy creation.master
commit
c9ce7aac37
10 changed files with 289 additions and 62 deletions
@ -0,0 +1,70 @@ |
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* 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 org.springframework.aop.framework; |
||||
|
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.springframework.cglib.proxy.Callback; |
||||
import org.springframework.cglib.proxy.Enhancer; |
||||
import org.springframework.cglib.proxy.Factory; |
||||
import org.springframework.objenesis.ObjenesisException; |
||||
import org.springframework.objenesis.ObjenesisStd; |
||||
|
||||
/** |
||||
* Objenesis based extension of {@link CglibAopProxy} to create proxy instances without |
||||
* invoking the constructor of the class. |
||||
* |
||||
* @author Oliver Gierke |
||||
* @since 4.0 |
||||
*/ |
||||
class ObjenesisCglibAopProxy extends CglibAopProxy { |
||||
|
||||
private static final Log logger = LogFactory.getLog(ObjenesisCglibAopProxy.class); |
||||
|
||||
private final ObjenesisStd objenesis; |
||||
|
||||
|
||||
/** |
||||
* Creates a new {@link ObjenesisCglibAopProxy} using the given {@link AdvisedSupport}. |
||||
* @param config must not be {@literal null}. |
||||
*/ |
||||
public ObjenesisCglibAopProxy(AdvisedSupport config) { |
||||
super(config); |
||||
this.objenesis = new ObjenesisStd(true); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) { |
||||
try { |
||||
Factory factory = (Factory) objenesis.newInstance(enhancer.createClass()); |
||||
factory.setCallbacks(callbacks); |
||||
return factory; |
||||
} |
||||
catch (ObjenesisException ex) { |
||||
// Fallback to Cglib on unsupported JVMs
|
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug("Unable to instantiate proxy using Objenesis, falling back " |
||||
+ "to regular proxy construction", ex); |
||||
} |
||||
return super.createProxyClassAndInstance(enhancer, callbacks); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,28 @@ |
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* 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 org.springframework.aop.framework; |
||||
|
||||
public class ClassWithConstructor { |
||||
|
||||
public ClassWithConstructor(Object object) { |
||||
|
||||
} |
||||
|
||||
public void method() { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,44 @@ |
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* 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 org.springframework.aop.framework; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* @author Oliver Gierke |
||||
*/ |
||||
@Component |
||||
public class ClassWithComplexConstructor { |
||||
|
||||
private final Dependency dependency; |
||||
|
||||
@Autowired |
||||
public ClassWithComplexConstructor(Dependency dependency) { |
||||
Assert.notNull(dependency); |
||||
this.dependency = dependency; |
||||
} |
||||
|
||||
public Dependency getDependency() { |
||||
return dependency; |
||||
} |
||||
|
||||
public void method() { |
||||
this.dependency.method(); |
||||
} |
||||
} |
@ -0,0 +1,32 @@ |
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* 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 org.springframework.aop.framework; |
||||
|
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Component class Dependency { |
||||
|
||||
private int value = 0; |
||||
|
||||
public void method() { |
||||
value++; |
||||
} |
||||
|
||||
public int getValue() { |
||||
return value; |
||||
} |
||||
} |
@ -0,0 +1,21 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" |
||||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" |
||||
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd |
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd |
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> |
||||
|
||||
<context:component-scan base-package="org.springframework.aop.framework" /> |
||||
|
||||
<aop:config> |
||||
<aop:advisor advice-ref="debugInterceptor" pointcut="bean(classWithComplexConstructor)" /> |
||||
</aop:config> |
||||
|
||||
|
||||
<bean id="debugInterceptor" |
||||
class="org.springframework.aop.interceptor.DebugInterceptor" /> |
||||
|
||||
|
||||
</beans> |
@ -0,0 +1,47 @@ |
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* 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 org.springframework.aop.framework; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.aop.interceptor.DebugInterceptor; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.support.ClassPathXmlApplicationContext; |
||||
|
||||
import static org.hamcrest.CoreMatchers.*; |
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* Integration test for Objenesis proxy creation. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public class ObjenesisProxyTests { |
||||
|
||||
@Test |
||||
public void appliesAspectToClassWithComplexConstructor() { |
||||
|
||||
@SuppressWarnings("resource") |
||||
ApplicationContext context = new ClassPathXmlApplicationContext("ObjenesisProxyTests-context.xml", getClass()); |
||||
|
||||
ClassWithComplexConstructor bean = context.getBean(ClassWithComplexConstructor.class); |
||||
bean.method(); |
||||
|
||||
DebugInterceptor interceptor = context.getBean(DebugInterceptor.class); |
||||
assertThat(interceptor.getCount(), is(1L)); |
||||
assertThat(bean.getDependency().getValue(), is(1)); |
||||
} |
||||
} |
Loading…
Reference in new issue