diff --git a/docs/src/main/docbook/spring.xml b/docs/src/main/docbook/spring.xml index 68ce413241..0de6b6df8e 100644 --- a/docs/src/main/docbook/spring.xml +++ b/docs/src/main/docbook/spring.xml @@ -63,6 +63,29 @@ holder. Spring singleton and request scopes are supported. + + To enable JAX-RS resources to work Spring functionality that requires proxying, such as Spring transaction management + (with @Transactional), Spring Security and aspect oriented programming (such as @Aspect), the resources + must themselves be managed by Spring, by annotating with @Component, @Service, + @Controller or @Repository: + + import javax.ws.rs.GET; +import javax.ws.rs.Path; +import org.springframework.stereotype.Component; + +@Component +@Path("/") +public class SomeResource { + + @Transactional + @GET + public void updateResource() { + // ... + } +} + + + Limitations: diff --git a/ext/spring3/pom.xml b/ext/spring3/pom.xml index 65162f87e3..1acd72e5c2 100644 --- a/ext/spring3/pom.xml +++ b/ext/spring3/pom.xml @@ -146,6 +146,28 @@ test + + + org.springframework + spring-aop + ${spring3.version} + test + + + + org.aspectj + aspectjrt + 1.6.11 + test + + + + org.aspectj + aspectjweaver + 1.6.11 + test + + diff --git a/ext/spring3/src/main/resources/jersey-spring-applicationContext.xml b/ext/spring3/src/main/resources/jersey-spring-applicationContext.xml index 09c08dffed..34b645eb89 100644 --- a/ext/spring3/src/main/resources/jersey-spring-applicationContext.xml +++ b/ext/spring3/src/main/resources/jersey-spring-applicationContext.xml @@ -42,12 +42,8 @@ --> + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> diff --git a/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/Aspect4JTest.java b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/Aspect4JTest.java new file mode 100644 index 0000000000..94af5c0cfc --- /dev/null +++ b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/Aspect4JTest.java @@ -0,0 +1,81 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.server.spring.aspect4j; + +import javax.ws.rs.core.Application; + +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.junit.Assert.assertEquals; + +public class Aspect4JTest extends JerseyTest { + + private ClassPathXmlApplicationContext applicationContext; + + private TestAspect testAspect; + + @Before + public void before() { + testAspect.reset(); + } + + @Override + protected Application configure() { + applicationContext = new ClassPathXmlApplicationContext("jersey-spring-aspect4j-applicationContext.xml"); + testAspect = applicationContext.getBean(TestAspect.class); + return new Aspect4jJerseyConfig() + .property("contextConfig", applicationContext); + } + + @Test + public void methodCallShouldNotBeIntercepted() { + target("test1").request().get(String.class); + assertEquals(0, testAspect.getInterceptions()); + } + + @Test + public void methodCallShouldBeIntercepted() { + target("test2").request().get(String.class); + assertEquals(1, applicationContext.getBean(TestAspect.class).getInterceptions()); + } +} diff --git a/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/Aspect4jJerseyConfig.java b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/Aspect4jJerseyConfig.java new file mode 100644 index 0000000000..d5c31459a0 --- /dev/null +++ b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/Aspect4jJerseyConfig.java @@ -0,0 +1,52 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.server.spring.aspect4j; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.server.spring.scope.RequestContextFilter; + +public class Aspect4jJerseyConfig extends ResourceConfig { + + public Aspect4jJerseyConfig() { + register(RequestContextFilter.class); + register(NoComponentResource.class); + register(ComponentResource.class); + } +} diff --git a/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/ComponentResource.java b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/ComponentResource.java new file mode 100644 index 0000000000..9df0a37c79 --- /dev/null +++ b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/ComponentResource.java @@ -0,0 +1,62 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.server.spring.aspect4j; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +import org.glassfish.jersey.server.spring.TestComponent1; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Path("/") +@Component +public class ComponentResource { + + @Autowired + private TestComponent1 testComponent1; + + @Path("test2") + @GET + public String test2() { + return testComponent1.result(); + } + +} diff --git a/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/NoComponentResource.java b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/NoComponentResource.java new file mode 100644 index 0000000000..b8e27f9a10 --- /dev/null +++ b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/NoComponentResource.java @@ -0,0 +1,61 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.server.spring.aspect4j; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +import org.glassfish.jersey.server.spring.TestComponent1; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Path("/") +public class NoComponentResource { + + @Autowired + private TestComponent1 testComponent1; + + @Path("test1") + @GET + public String test1() { + return testComponent1.result(); + } + +} diff --git a/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/TestAspect.java b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/TestAspect.java new file mode 100644 index 0000000000..af9451055c --- /dev/null +++ b/ext/spring3/src/test/java/org/glassfish/jersey/server/spring/aspect4j/TestAspect.java @@ -0,0 +1,27 @@ +package org.glassfish.jersey.server.spring.aspect4j; + +import javax.inject.Singleton; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; + +@Aspect +@Singleton +public class TestAspect { + + private int interceptions = 0; + + @Before("execution(* org.glassfish.jersey.server.spring.aspect4j.*.*())") + public void intercept(JoinPoint joinPoint) { + interceptions++; + } + + public int getInterceptions() { + return interceptions; + } + + public void reset() { + interceptions = 0; + } +} \ No newline at end of file diff --git a/ext/spring3/src/test/resources/jersey-spring-aspect4j-applicationContext.xml b/ext/spring3/src/test/resources/jersey-spring-aspect4j-applicationContext.xml new file mode 100644 index 0000000000..0a3825e01a --- /dev/null +++ b/ext/spring3/src/test/resources/jersey-spring-aspect4j-applicationContext.xml @@ -0,0 +1,56 @@ + + + + + + + + + +