6
votes

I am facing the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer : org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:414) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:394) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:385) at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:261) at org.springframework.boot.SpringApplication.(SpringApplication.java:237) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)

My main method is

package proj1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebApplication.class, args);
    }
}

My pom.xml is

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.webapp</groupId>
  <artifactId>proj1</artifactId>
  <packaging>war</packaging>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>
  <version>0.0.1-SNAPSHOT</version>
  <name>proj1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>1.4.0.RELEASE</version>
</dependency> 
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
  </dependencies>
  <build>
    <finalName>proj1</finalName>
  </build>
</project>
4
I don't believe this is full stack-trace of error.luboskrnac
For starters stop mixing framework versions (1.3.5 and 1.4.0). Mixing versions of a framework (regardless the framework) is trouble bound to happen. Next to that you are using Spring 4.0.0 dependency whereas 4.2 would be expected (remove the spring-core dependency as that is already included).M. Deinum
I tried this @M.Deinum if I change the version to any one i.e. 1.3.5 or 1.4.0 there are The import org.springframework.boot.SpringApplication cannot be resolved errors that is the reason I keep the verisions different.Please see the post linkArchit Aggarwal
Again NEVER mix versions of a framework as that is trouble waiting to happen, not to mention the mix of spring framework versions you will get with this. Sort out the real issue instead of mixing things you shouldn't be mixing in the first place.M. Deinum

4 Answers

2
votes

I recently came across this issue. I was running an executable jar through a bat file. My executable jar was already having all the spring boot jars required for the version mentioned in the pom.xml. But by mistake I defined a classpath of spring-boot libraries(of other version) in the bat file as well. When I removed that classpath entry, this particular issue was gone as then it was using only the spring boot jars that were mentioned in the pom file. I think this issue is coming because of the version conflict of the spring jars. Please correct me if I'm wrong. Thanks.

0
votes

Your <dependencies> should look something like this

<?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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.webapp</groupId>
    <artifactId>proj1</artifactId>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>
    <version>0.0.1-SNAPSHOT</version>
    <name>proj1 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Test Dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>proj1</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

And not the mixture of Spring Boot (1.3 and 1.4) versions and Spring Versions (4.0, 4.2 and 4.3) as you had. You also included dependencies that are already included (or break things).

Another thing is you should have the spring-boot-maven plugin which is also lacking in your pom.

0
votes

It worked for me to change version of spring-boot-starter-parent from 2.0.0 to 2.1.3.

-2
votes
public class MyPolicy extends Policy {
    @Override
    public PermissionCollection getPermissions(CodeSource codesource) {
        Permissions p = new Permissions();
        /**
          * The AllPermission is a permission that implies all other permissions.
          * <p>
          * <b>Note:</b> Granting AllPermission should be done with extreme care,
          * as it implies all other permissions. Thus, it grants code the ability
          * to run with security
          * disabled.  Extreme caution should be taken before granting such
          * a permission to code.  This permission should be used only during testing,
          * or in extremely rare cases where an application or applet is
          * completely trusted and adding the necessary permissions to the policy
          * is prohibitively cumbersome.
          *
          * @see java.security.Permission
          * @see java.security.AccessController
          * @see java.security.Permissions
          * @see java.security.PermissionCollection
          * @see java.lang.SecurityManager
          *
          *
          * @author Roland Schemers
          * @since 1.2
          *
          * @serial exclude


         public final class AllPermission*/
        //p.add(new AllPermission());
        //p.add(new CryptoPermission());
        p.add(new AudioPermission());
        p.add(new AuthPermission());
        p.add(new AWTPermission());
        p.add(new BasicPermission());
        p.add(new DelegationPermission());
        p.add(new FilePermission());
        p.add(new JAXBPermission());
        p.add(new LinkPermission());
        p.add(new LoggingPermission());
        p.add(new ManagementPermission());
        p.add(new MBeanPermission());
        p.add(new MBeanServerPermission());
        p.add(new MBeanTrustPermission());
        p.add(new NetPermission());
        p.add(new Permission());
        p.add(new PrivateCredentialPermission());
        p.add(new PropertyPermission());
        p.add(new ReflectPermission());
        p.add(new RuntimePermission());
        p.add(new SecurityPermission());
        p.add(new SerializablePermission());
        p.add(new ServicePermission());
        p.add(new SocketPermission());
        p.add(new SQLPermission());
        p.add(new SSLPermission());
        p.add(new SubjectDelegationPermission());
        p.add(new UnresolvedPermission());
        p.add(new URLPermission());
        p.add(new WebServicePermission());
        return p;
    }
}

public  void main(String[] args) {
    Policy.setPolicy(new MyPolicy());

    SpringApplication.run(Application.class, args);
}