0

I'm trying to implement Togglz in my Spring Boot application.

I followed the Quick Start steps but when I run my application I get this exception:

Exception in thread "main" java.util.ServiceConfigurationError: org.togglz.core.spi.BeanFinder: org.togglz.cdi.spi.CDIBeanFinder Unable to get public no-arg constructor at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:586) at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:679) at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1240) at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1273) at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1309) at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1393) at org.togglz.core.context.BeanFinderFeatureManagerProvider.getFeatureManager(BeanFinderFeatureManagerProvider.java:36) at org.togglz.core.context.FeatureContext.findFeatureManagerInClassLoader(FeatureContext.java:107) at org.togglz.core.context.FeatureContext.getFeatureManagerOrNull(FeatureContext.java:73) at org.togglz.core.context.FeatureContext.getFeatureManager(FeatureContext.java:46) at com.paulcarron.testproject.MyFeatures.isActive(MyFeatures.java:18) at com.paulcarron.testproject.TogglzTest.getTogglzStatus(TogglzTest.java:31) at com.paulcarron.testproject.TestprojectApplication.main(TestprojectApplication.java:15) Caused by: java.lang.NoClassDefFoundError: javax/enterprise/context/spi/Contextual at java.base/java.lang.Class.getDeclaredConstructors0(Native Method) at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3405) at java.base/java.lang.Class.getConstructor0(Class.java:3610) at java.base/java.lang.Class.getConstructor(Class.java:2303) at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:666) at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:663) at java.base/java.security.AccessController.doPrivileged(AccessController.java:569) at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:674) ... 11 more Caused by: java.lang.ClassNotFoundException: javax.enterprise.context.spi.Contextual at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ... 19 more

This is my code in TogglzTest:

if(MyFeatures.FEATURE_ONE.isActive() ) {
    System.out.println("togglzOff is " + togOnTrue);
    System.out.println("togglzOff is " + togOnFalse);
}

Here is my isActive method in MyFeatures:

public boolean isActive() {
    return FeatureContext.getFeatureManager().isActive(this);
}

I did some Googling and all I can find was various dependencies that I should add but doing so doesn't resolve my issue. Here's my pom:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.me</groupId>
    <artifactId>testproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>testproject</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Togglz for Servlet environments (mandatory) -->
        <dependency>
            <groupId>org.togglz</groupId>
            <artifactId>togglz-servlet</artifactId>
            <version>3.1.2</version>
        </dependency>

        <!-- CDI integration (optional) -->
        <dependency>
            <groupId>org.togglz</groupId>
            <artifactId>togglz-cdi</artifactId>
            <version>3.1.2</version>
        </dependency>

        <!-- Spring integration (optional) -->
        <dependency>
            <groupId>org.togglz</groupId>
            <artifactId>togglz-spring-web</artifactId>
            <version>3.1.2</version>
        </dependency><!-- https://mvnrepository.com/artifact/jakarta.enterprise/jakarta.enterprise.cdi-api -->
        <dependency>
            <groupId>jakarta.enterprise</groupId>
            <artifactId>jakarta.enterprise.cdi-api</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.togglz</groupId>
            <artifactId>togglz-spring-core</artifactId>
            <version>3.3.3</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

How do I resolve this?

1
  • 1
    Looking at the stacktrace you have togglz configured for CDI not for Spring which is totally different then Spring is. So ditch that dependency. In addition as you are also using SPring Boot why aren't you simply using the Spring Boot Starter from Togglz which includes the proper dependencies. However the Togglz version you are using isn't compatible with Spring Boot 3 (as it is using JavaEE and not JakartaEE) so either don't use togglz in this version or downgrade Spring Boot to 2.7.x.
    – M. Deinum
    Commented Jan 23, 2023 at 9:06

1 Answer 1

0

The current milestone 4.0.0-M3 is compatible with Spring Boot 3 and I will release a final togglz 4.0.0 soon. Right now everything is working as expected so IMO you could also use the milestone release already.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.