google / guice

Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 11 and above, brought to you by Google.
https://github.com/google/guice
Apache License 2.0
12.47k stars 1.67k forks source link

jdk11 / guice 4.2.2 #1256

Closed prasanthgithub closed 1 year ago

prasanthgithub commented 5 years ago

Hi, As part of migration to jdk11 we are asked to upgrade guice version which supports jdk11.

So in pom.xml I tried to upgrade guice version from 4.0 => 4.2.2 and left other dependency versions as it is but when i started our REST application its unable to get a successful C3p0 connection. However if i revert back to 4.0 version it works fine.

<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    **<version>4.2.2</version>**
</dependency>
<dependency>
    <groupId>com.google.inject.extensions</groupId>
    <artifactId>guice-jndi</artifactId>
    <version>4.0</version>
</dependency>
<dependency>
    <groupId>com.google.inject.extensions</groupId>
    <artifactId>guice-servlet</artifactId>
    <version>4.0</version>
</dependency>
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>
<!-- END: C3P0 database connection and google guice for inject -->

Java Code:

import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.inject.AbstractModule; import com.google.inject.Provider; import com.google.inject.jndi.JndiIntegration;

public class DemoDataSource extends AbstractModule { private static final String JNDI_ENV = "java:comp/env/jdbc/c3p0DataSource";

@Override
protected void configure() {

bind(javax.naming.Context.class).to(javax.naming.InitialContext.class).asEagerSingleton(); Provider dataSourceProvider = JndiIntegration.fromJndi(DataSource.class, JNDI_ENV); bind(DataSource.class).toProvider(dataSourceProvider).asEagerSingleton(); bind(DBConnectionPool.class).to(C3P0ConnectionProvider.class).asEagerSingleton(); }
} }

Any help is much appreciated.

GedMarc commented 5 years ago

There is currently no guice version that supports JDK 11 (JPMS) - I've made a few pull requests but it looks like the project is only running under maintenance mode.

But I went ahead and did it all anyway - The version is 4.2.3-SNAPSHOT and is always kept up to date, and all of these are in a large production corporate environment. No changes from original except for one in JCache API to ignore synthetic methods (which either break or duplicate caching)

Before forward tho - don't use any addon logging libraries - remove sl4j, learn the new jdk9 logging system which caters for everything Rest services - use Rest v4, this is JPMS compatible. then use the JPMS jackson (https://mvnrepository.com/artifact/com.jwebmp.jpms.jackson) https://mvnrepository.com/artifact/com.jwebmp.jpms.guice

These are all standalone and part of the guiced-ee framework, which also contains a C3P0 module that is JLink friendly ()

javax.inject (jdk1.5 compiled jars will not run on JDK9^)

    <dependency>
                <groupId>com.jwebmp.thirdparty</groupId>
                <artifactId>javax.inject</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

JCache - the current ri and api do not allow for synthetic methods, and is not strictly named

               <dependency>
                <groupId>com.jwebmp.thirdparty.jcache</groupId>
                <artifactId>cache-api</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.thirdparty.jcache</groupId>
                <artifactId>cache-annotations-ri-common</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.thirdparty.jcache</groupId>
                <artifactId>cache-annotations-ri-guice</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

2) activation - jakarta.activation package clashes with javax.activation so made a jpms compatible pass through allowing both packages. jakarta.activation is used for jaxb 2.3^

<dependency>
                <groupId>com.jwebmp.thirdparty</groupId>
                <artifactId>javax.activation</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.thirdparty</groupId>
                <artifactId>jakarta.activation</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

javax.transaction - The transaction API must be strictly named for use with guice-persist

     <dependency>
                <groupId>com.jwebmp.thirdparty</groupId>
                <artifactId>javax.transaction</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

BTM - Transaction manager

 <dependency>
                <groupId>com.jwebmp.guicedee.persistence</groupId>
                <artifactId>btm</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
                <exclusions>
                    <exclusion>
                        <groupId>javax.transaction</groupId>
                        <artifactId>jta</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

Guice JPMS Libraries

   <!-- Guice until there is a named module-info for jlink-->
            <dependency>
                <groupId>com.jwebmp.jpms.guice</groupId>
                <artifactId>guice-parent</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.inject</groupId>
                <artifactId>guice-parent</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.inject</groupId>
                <artifactId>guice-bom</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.inject</groupId>
                <artifactId>guice</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <!-- Guice Extensions -->
            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>extensions-parent</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-assistedinject</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-dagger-adapter</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-grapher</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-jmx</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-jndi</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-persist</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-servlet</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-spring</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-testlib</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>com.jwebmp.inject.extensions</groupId>
                <artifactId>guice-throwingproviders</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
            </dependency>

Guiced-EE with a mild tutorial is here (https://jwebmp.com/)

prasanthgithub commented 5 years ago

Many thanks GedMarc for the reply.

So Google Guice still does not support Jdk11 which is bit strange since the documentation for 4.2.2 suggests it supports jdk11! https://github.com/google/guice/wiki/Guice422 . (Scroll down and it says it supports jdk11)

Please leave REST related i should not have mentioned it but the problem is occuring during initialisation after we deploy our WAR in tomcat since C3P0 Database connection setup code is triggered.

Any ideas why the above code works fine when using guice-inject 4.0 and does not work when using guice 4.2.2 (both compiled using jdk11 compiler)

GedMarc commented 5 years ago

Automatic module name was added in as a placeholder in a later version, without it the library gets thrown into the unnamed module section, with it, it's get identified but is not JPMS compatible

NB to note that an automatic-module-name in MANIFEST.MF is a PLACEHOLDER, not an identifier, it doesn't do anything in JPMS land except reserve the name, so no, it is not JDK 11 (JPMS) friendly at all. You will not be able to build JLink artifacts, you will not be able to run in strict module-info module (as in not an open module) and you will not be utilizing the vast performance increase that comes with using modules.

You cannot use WAR/EAR/EJB in JDK 11 JPMS, the very concept of managed deploys is a direct contradiction against modular development and DDD. They do not exist anymore, hence why EE was cancelled and thrown out by Oracle - as in it was not handed over to eclipse, it was thrown away, and eclipse decided to pick it up to support the systems that choose to stay with JDK 8.

For JPMS look at systems designed for it - such as undertow (not wildfly....), or jetty (not tomcat).

This is a common misconception with most libraries claiming jpms support where they simply say "but i changed my manifest file"

prasanthgithub commented 5 years ago

Thanks for confirmation that guice 4.2.2 indeed does not actually support jdk11

One last question.

Apologies, if i sound dumb or annoying but i want to repeat the later part of the question which needs clarification please.

So the question again is

Combination which works : guice-inject 4.0 + (compiled using jdk11 compiler) +( deployed in tomcat 11 which uses jdk11 )

but below combination does not work.

guice-inject 4.2.2 + (compiled using jdk11 compiler) +( deployed in tomcat 11 which uses jdk11 )

Curious if you have any ideas pls.

GedMarc commented 5 years ago

automatic module name added in manifest.mf

GedMarc commented 5 years ago

https://github.com/google/guice/commit/fe67d047ff6b9c1b866585d0f913e83c20685295

When you get the performance hit you know why :)

prasanthgithub commented 5 years ago

Many thanks GedMarc for the quick replies and patience.

Much Appreciated PJ

sameerinbox commented 5 years ago

I have similar setup however I am not looking for any JPMS support. Application is old and written for Java 8. Intention of migrating to Java11 is only to go from Oracle8 to OpenJDK11 with no refactoring in code.

Would guice-inject 4.0 + (compiled using jdk11 compiler) +( deployed in tomcat 9 which uses jdk11 ) be a issue?

GedMarc commented 5 years ago

Guice inject 4 compiled using jdk 11 compiler (how are you going to jar/jar cglib & asm into the compiled project? Perhaps Shade?) @sameerinbox Because you are not "using" jdk 11, but more "abusing" backwards compatibility, you will get hit with warnings, and a large performance knock, and thats about it.

Remember you are never to run a JDK in production, but otherwise, no concerns.

nsoft commented 4 years ago

It would be really helpful if one of the project maintainers would comment on this issue. It's persistence, along with #1277 #1274 and the fact it's been almost a year since release with zero bugs closed with code in the last 3 months (at a quick look I might have missed something) makes this project look quite dead. I was hoping to use it again on a new project, but if it's dying out I don't want to build on it....

GedMarc commented 4 years ago

@nsoft I will be releasing a repackage of the guice libraries 4.2.2 with full strict encapsulation support, including jlink and jpackage.

It has been forever, I'm also pretty much tired of waiting :) The changes in 4.2.3-SNAPSHOT also don't look incredibly.. moving...

mcculls commented 4 years ago

@nsoft speaking personally, I'm continuing to work on improvements to AOP that help with JDK11+. (I have various applications built on Guice, so I also want to make sure it works as well as possible with the latest JDK.)

mreiche commented 4 years ago

I switched to the version without AOP com.google.inject:guice:4.2.2:no_aop which works for me, because these features are not required in my project for now (https://github.com/google/guice/wiki/AOP)

GedMarc commented 4 years ago

I've released JDK 11 Guice 4.2.2 Modular artifacts that can be used,

https://www.guicedee.com