Skip to content

Commit 7120637

Browse files
committed
Support for Spring 5
Fixes: gh-3736 Signed-off-by: Ankur Pathak <[email protected]>
1 parent 61e6399 commit 7120637

File tree

47 files changed

+2281
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2281
-0
lines changed

ext/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<module>rx</module>
5454
<module>servlet-portability</module>
5555
<module>spring4</module>
56+
<module>spring5</module>
5657
<module>wadl-doclet</module>
5758
</modules>
5859

ext/spring5/pom.xml

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
5+
6+
This program and the accompanying materials are made available under the
7+
terms of the Eclipse Public License v. 2.0, which is available at
8+
http://www.eclipse.org/legal/epl-2.0.
9+
10+
This Source Code may also be made available under the following Secondary
11+
Licenses when the conditions for such availability set forth in the
12+
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
13+
version 2 with the GNU Classpath Exception, which is available at
14+
https://www.gnu.org/software/classpath/license.html.
15+
16+
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
17+
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>org.glassfish.jersey.ext</groupId>
26+
<artifactId>project</artifactId>
27+
<version>2.29-SNAPSHOT</version>
28+
</parent>
29+
30+
<artifactId>jersey-spring5</artifactId>
31+
<name>jersey-spring5</name>
32+
33+
<packaging>jar</packaging>
34+
35+
<description>
36+
Jersey extension module providing support for Spring 5 integration.
37+
</description>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.glassfish.jersey.core</groupId>
42+
<artifactId>jersey-server</artifactId>
43+
<version>${project.version}</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.glassfish.jersey.inject</groupId>
48+
<artifactId>jersey-hk2</artifactId>
49+
<version>${project.version}</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.glassfish.jersey.containers</groupId>
54+
<artifactId>jersey-container-servlet-core</artifactId>
55+
<version>${project.version}</version>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
60+
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
61+
<version>${project.version}</version>
62+
<scope>test</scope>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>commons-logging</groupId>
67+
<artifactId>commons-logging</artifactId>
68+
<version>1.2</version>
69+
<scope>test</scope>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>org.glassfish.hk2</groupId>
74+
<artifactId>hk2</artifactId>
75+
<version>${hk2.version}</version>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>org.glassfish.hk2</groupId>
80+
<artifactId>spring-bridge</artifactId>
81+
<version>${hk2.version}</version>
82+
<exclusions>
83+
<exclusion> <!-- already pulled in by jersey-server -->
84+
<groupId>javax.inject</groupId>
85+
<artifactId>javax.inject</artifactId>
86+
</exclusion>
87+
<exclusion>
88+
<groupId>org.glassfish.hk2</groupId>
89+
<artifactId>hk2-api</artifactId>
90+
</exclusion>
91+
</exclusions>
92+
</dependency>
93+
94+
<dependency>
95+
<groupId>org.springframework</groupId>
96+
<artifactId>spring-beans</artifactId>
97+
<version>${spring5.version}</version>
98+
</dependency>
99+
100+
<dependency>
101+
<groupId>org.springframework</groupId>
102+
<artifactId>spring-core</artifactId>
103+
<version>${spring5.version}</version>
104+
<exclusions>
105+
<exclusion>
106+
<groupId>commons-logging</groupId>
107+
<artifactId>commons-logging</artifactId>
108+
</exclusion>
109+
</exclusions>
110+
</dependency>
111+
112+
<dependency>
113+
<groupId>org.springframework</groupId>
114+
<artifactId>spring-web</artifactId>
115+
<version>${spring5.version}</version>
116+
</dependency>
117+
118+
<dependency>
119+
<groupId>org.springframework</groupId>
120+
<artifactId>spring-aop</artifactId>
121+
<version>${spring5.version}</version>
122+
</dependency>
123+
124+
<dependency>
125+
<groupId>jakarta.servlet</groupId>
126+
<artifactId>jakarta.servlet-api</artifactId>
127+
<version>${servlet4.version}</version>
128+
<scope>provided</scope>
129+
</dependency>
130+
131+
<dependency>
132+
<groupId>org.glassfish.jersey.test-framework</groupId>
133+
<artifactId>jersey-test-framework-core</artifactId>
134+
<version>${project.version}</version>
135+
<scope>test</scope>
136+
</dependency>
137+
138+
<dependency>
139+
<groupId>org.aspectj</groupId>
140+
<artifactId>aspectjrt</artifactId>
141+
<version>1.6.11</version>
142+
<scope>test</scope>
143+
</dependency>
144+
<dependency>
145+
<groupId>org.aspectj</groupId>
146+
<artifactId>aspectjweaver</artifactId>
147+
<version>1.6.11</version>
148+
<scope>test</scope>
149+
</dependency>
150+
151+
</dependencies>
152+
153+
<properties>
154+
<spring5.version>5.1.5.RELEASE</spring5.version>
155+
</properties>
156+
157+
<build>
158+
<plugins>
159+
<plugin>
160+
<groupId>com.sun.istack</groupId>
161+
<artifactId>istack-commons-maven-plugin</artifactId>
162+
<inherited>true</inherited>
163+
</plugin>
164+
<plugin>
165+
<groupId>org.codehaus.mojo</groupId>
166+
<artifactId>build-helper-maven-plugin</artifactId>
167+
<inherited>true</inherited>
168+
</plugin>
169+
</plugins>
170+
</build>
171+
172+
<profiles>
173+
<profile>
174+
<id>delayed-strategy-skip-test</id>
175+
<activation>
176+
<property>
177+
<name>org.glassfish.jersey.injection.manager.strategy</name>
178+
<value>delayed</value>
179+
</property>
180+
</activation>
181+
<build>
182+
<plugins>
183+
<plugin>
184+
<groupId>org.apache.maven.plugins</groupId>
185+
<artifactId>maven-surefire-plugin</artifactId>
186+
<configuration>
187+
<skipTests>true</skipTests>
188+
</configuration>
189+
</plugin>
190+
</plugins>
191+
</build>
192+
</profile>
193+
</profiles>
194+
</project>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.server.spring;
18+
19+
import java.lang.reflect.AnnotatedElement;
20+
import java.lang.reflect.Constructor;
21+
import java.lang.reflect.Field;
22+
import java.lang.reflect.Method;
23+
import java.util.HashSet;
24+
import java.util.Set;
25+
import java.util.logging.Logger;
26+
27+
import javax.inject.Singleton;
28+
29+
import org.glassfish.jersey.internal.inject.Injectee;
30+
import org.glassfish.jersey.internal.inject.InjectionResolver;
31+
32+
import org.springframework.beans.BeansException;
33+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
34+
import org.springframework.beans.factory.annotation.Autowired;
35+
import org.springframework.beans.factory.annotation.Qualifier;
36+
import org.springframework.beans.factory.config.DependencyDescriptor;
37+
import org.springframework.context.ApplicationContext;
38+
import org.springframework.core.MethodParameter;
39+
40+
/**
41+
* HK2 injection resolver for Spring framework {@link Autowired} annotation injection.
42+
*
43+
* @author Marko Asplund (marko.asplund at yahoo.com)
44+
* @author Vetle Leinonen-Roeim (vetle at roeim.net)
45+
*/
46+
@Singleton
47+
public class AutowiredInjectResolver implements InjectionResolver<Autowired> {
48+
49+
private static final Logger LOGGER = Logger.getLogger(AutowiredInjectResolver.class.getName());
50+
51+
private volatile ApplicationContext ctx;
52+
53+
/**
54+
* Create a new instance.
55+
*
56+
* @param ctx Spring application context.
57+
*/
58+
public AutowiredInjectResolver(ApplicationContext ctx) {
59+
this.ctx = ctx;
60+
}
61+
62+
@Override
63+
public Object resolve(Injectee injectee) {
64+
AnnotatedElement parent = injectee.getParent();
65+
String beanName = null;
66+
if (parent != null) {
67+
Qualifier an = parent.getAnnotation(Qualifier.class);
68+
if (an != null) {
69+
beanName = an.value();
70+
}
71+
}
72+
boolean required = parent != null ? parent.getAnnotation(Autowired.class).required() : false;
73+
return getBeanFromSpringContext(beanName, injectee, required);
74+
}
75+
76+
private Object getBeanFromSpringContext(String beanName, Injectee injectee, final boolean required) {
77+
try {
78+
DependencyDescriptor dependencyDescriptor = createSpringDependencyDescriptor(injectee);
79+
Set<String> autowiredBeanNames = new HashSet<>(1);
80+
autowiredBeanNames.add(beanName);
81+
return ctx.getAutowireCapableBeanFactory().resolveDependency(dependencyDescriptor, null,
82+
autowiredBeanNames, null);
83+
} catch (BeansException e) {
84+
if (required) {
85+
LOGGER.warning(e.getMessage());
86+
throw e;
87+
}
88+
return null;
89+
}
90+
}
91+
92+
private DependencyDescriptor createSpringDependencyDescriptor(final Injectee injectee) {
93+
AnnotatedElement annotatedElement = injectee.getParent();
94+
95+
if (annotatedElement.getClass().isAssignableFrom(Field.class)) {
96+
return new DependencyDescriptor((Field) annotatedElement, !injectee.isOptional());
97+
} else if (annotatedElement.getClass().isAssignableFrom(Method.class)) {
98+
return new DependencyDescriptor(
99+
new MethodParameter((Method) annotatedElement, injectee.getPosition()), !injectee.isOptional());
100+
} else {
101+
return new DependencyDescriptor(
102+
new MethodParameter((Constructor) annotatedElement, injectee.getPosition()), !injectee.isOptional());
103+
}
104+
}
105+
106+
@Override
107+
public boolean isConstructorParameterIndicator() {
108+
return false;
109+
}
110+
111+
@Override
112+
public boolean isMethodParameterIndicator() {
113+
return false;
114+
}
115+
116+
@Override
117+
public Class<Autowired> getAnnotation() {
118+
return Autowired.class;
119+
}
120+
}

0 commit comments

Comments
 (0)