'My Spring boot mvc project runs on STS but not running after building jar/war

The pom.xml

4.0.0

<groupId>com.birol</groupId>
<artifactId>EMS_WEB</artifactId>
<version>1.0.1-SNAPSHOT</version>

<name>EMS_WEB</name>
<packaging>war</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.7</version>
</parent>

<dependencies>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>2.2.6.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <!-- 2fa -->
    <dependency>
        <groupId>org.jboss.aerogear</groupId>
        <artifactId>aerogear-otp-java</artifactId>
        <version>${aerogear.version}</version>
        <scope>compile</scope>
    </dependency>

    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <scope>runtime</scope>
    </dependency>
    
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>el-api</artifactId>
        <version>${javax.el.version}</version>
    </dependency>
    
    <!-- Password Validation -->
    <dependency>
        <groupId>org.passay</groupId>
        <artifactId>passay</artifactId>
        <version>${passay.version}</version>
    </dependency>

    <!-- Spring Data JPA dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- DB dependencies -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
    </dependency>

    <!-- User Agent parser -->
    <dependency>
        <groupId>com.github.ua-parser</groupId>
        <artifactId>uap-java</artifactId>
        <version>${uap-java.version}</version>
    </dependency>

    <!-- maxmind dependency -->

    <dependency>
        <groupId>com.maxmind.geoip2</groupId>
        <artifactId>geoip2</artifactId>
        <version>${geoip2.version}</version>
    </dependency>

    <!-- test -->

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>        
    
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <scope>test</scope>
    </dependency>       

    <dependency>
        <groupId>net.logstash.logback</groupId>
        <artifactId>logstash-logback-encoder</artifactId>
        <version>${logstash-logback-encoder.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    
    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.18.1-GA</version>
    </dependency>

</dependencies>

<build>
    <finalName>EMS_WEB</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <executions>
            <execution>
              <goals>
                <goal>repackage</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>**/*IntegrationTest.java</exclude>
                    <exclude>**/*LiveTest.java</exclude>
                </excludes>
                <systemPropertyVariables>
                    <!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
                </systemPropertyVariables>
            </configuration>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>integration</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                            <configuration>
                                <excludes>
                                    <exclude>**/*LiveTest.java</exclude>
                                </excludes>
                                <includes>
                                    <include>**/*IntegrationTest.java</include>
                                </includes>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <systemPropertyVariables>
                            <test.mime>json</test.mime>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

<properties>
    <java-version>1.8</java-version>
    <javax.servlet.jsp-api.version>2.3.2-b01</javax.servlet.jsp-api.version>
    <javax.el.version>2.2</javax.el.version>
    <jstl.version>1.2</jstl.version>
    <guava.version>20.0</guava.version>
    <passay.version>1.0</passay.version>
    <logstash-logback-encoder.version>4.8</logstash-logback-encoder.version>
    <aerogear.version>1.0.0</aerogear.version>
    <uap-java.version>1.4.0</uap-java.version>
    <geoip2.version>2.15.0</geoip2.version>
</properties>

The error i am getting. this does not happen when i run it on STS as a spring boot project. But after building war , the war wont run

web - 2022-04-30 00:59:20,572 [main] WARN  o.s.b.w.s.c.GbHi8bXd2f26Vj8NqmcnHFztp53C1pSdpVjsf9ANBCtpYHvWau - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registrationListener': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'secSecurityConfig': Unsatisfied dependency expressed through field 'myAuthenticationSuccessHandler'; 

Main class. should i add component scanner to this?

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
}

RegistrationListener.java . Am i missing any tags/annotations?

@Component
public class RegistrationListener implements 
ApplicationListener<OnRegistrationCompleteEvent> {
@Autowired
private IUserService service;

@Autowired
private MessageSource messages;

@Autowired
private JavaMailSender mailSender;

@Autowired
private Environment env;

// API

@Override
public void onApplicationEvent(final OnRegistrationCompleteEvent event) {
    this.confirmRegistration(event);
}

private void confirmRegistration(final OnRegistrationCompleteEvent event) {
    final User user = event.getUser();
    final String token = UUID.randomUUID().toString();
    service.createVerificationTokenForUser(user, token);

    final SimpleMailMessage email = constructEmailMessage(event, user, token);
    mailSender.send(email);
}

//

private SimpleMailMessage constructEmailMessage(final OnRegistrationCompleteEvent event, final User user, final String token) {
    final String recipientAddress = user.getEmail();
    final String subject = "Registration Confirmation";
    final String confirmationUrl = event.getAppUrl() + "/registrationConfirm?token=" + token;
    String message= "Your SECRET for Google Authenticator: "+user.getSecret()+"\n";
    message+= messages.getMessage("message.regSuccLink", null, "You registered successfully. To confirm your registration, please click on the below link.", event.getLocale());
    final SimpleMailMessage email = new SimpleMailMessage();       
    email.setTo(recipientAddress);
    email.setSubject(subject);
    email.setText(message + " \r\n" + confirmationUrl);
    email.setFrom(env.getProperty("support.email"));
    return email;
}

}

IUserService.java Tried both with and without the @Service annotation

 @Service
 public interface IUserService {

User registerNewUserAccount(UserDto accountDto);

User getUser(String verificationToken);

void saveRegisteredUser(User user);

void deleteUser(User user);

void createVerificationTokenForUser(User user, String token);

VerificationToken getVerificationToken(String VerificationToken);

VerificationToken generateNewVerificationToken(String token);

void createPasswordResetTokenForUser(User user, String token);

User findUserByEmail(String email);

PasswordResetToken getPasswordResetToken(String token);

Optional<User> getUserByPasswordResetToken(String token);

Optional<User> getUserByID(long id);

void changeUserPassword(User user, String password);

boolean checkIfValidOldPassword(User user, String password);

String validateVerificationToken(String token);

String generateQRUrl(User user) throws UnsupportedEncodingException;

User updateUser2FA(boolean use2FA);

List<String> getUsersFromSessionRegistry();

NewLocationToken isNewLoginLocation(String username, String ip);

String isValidNewLocationToken(String token);

void addUserLocation(User user, String ip);

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source