Problem
A Spring application fails during startup or runtime with errors indicating that an @Autowired field is null. This often manifests as a NullPointerException or an AssertionError if checks are in place, for example:
java.lang.AssertionError: The autowired field appctx should not be null!
at at.cps.oflow.utils.AssertUtils.assertNotNull(AssertUtils.java:17)
at at.cps.oflow.licensing.consumer.OflowLicenseParamConsumerImpl.getLicenseStoreFile(OflowLicenseParamConsumerImpl.java:39)
...
This issue might appear intermittently or after changes to the project setup, like switching version control systems. The problem is particularly noted in projects using older versions of JDK (e.g., JDK 1.6) and Maven (e.g., Maven 3.2.5).
During the Maven build process, errors related to SSL/TLS might be observed when downloading dependencies, such as:
javax.net.ssl.SSLException: Received fatal alert: protocol_version
Cause
Modern Maven repositories, including Maven Central, increasingly require secure connections using newer TLS protocol versions (TLS 1.2 or higher). Older JDK versions, such as JDK 1.6, may not support these newer TLS versions by default.
Consequently, Maven (especially older versions like 3.2.5 running on an older JDK) fails to download dependencies over HTTPS from these repositories. This leads to an incomplete or corrupted set of project dependencies, causing the Spring application context to fail during initialization, resulting in @Autowired dependencies not being injected (remaining null).
Committing compiled .class files into version control can also mask or exacerbate build issues, leading to inconsistent states.
Resolution
To resolve this issue, configure Maven to download dependencies over HTTP instead of HTTPS, or ensure your environment supports the required TLS version.
Configure Maven to use an HTTP Mirror:
-
Locate your Maven
settings.xmlfile. This can be in your user home directory (USER_HOME/.m2/settings.xml) or in the Maven installation directory (M2_HOME/conf/settings.xml). -
Edit the
settings.xmlfile and add a mirror configuration within the<mirrors>section. This configuration redirects requests from the default HTTPS repositories to an HTTP alternative.-
Option 1: Using a public insecure HTTP mirror for Maven Central: (Use with caution, as it bypasses HTTPS security for central repository downloads).
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd"> <mirrors> <mirror> <id>insecure-repo1</id> <!-- Use 'central' to only mirror Maven Central, or '*' to mirror all repositories --> <mirrorOf>central</mirrorOf> <name>Maven Central Insecure HTTP Mirror</name> <url>http://insecure.repo1.maven.org/maven2/</url> </mirror> </mirrors> </settings> -
Option 2: Using an internal HTTP repository manager (like Nexus or Artifactory): If your organization uses an internal repository manager accessible via HTTP, configure it as a mirror for all repositories (
*). This is often the preferred solution in corporate environments.<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd"> <mirrors> <mirror> <id>internal-nexus</id> <!-- Mirror all repositories through the internal Nexus --> <mirrorOf>*</mirrorOf> <name>Internal Nexus Repository</name> <url>http://your-internal-nexus-or-artifactory-url/repository/maven-public/</url> </mirror> </mirrors> </settings>(Replace
http://your-internal-nexus-or-artifactory-url/repository/maven-public/with the actual URL of your internal HTTP repository)
-
-
After modifying
settings.xml, ensure IntelliJ IDEA is configured to use the correct Maven installation and settings file (Settings/Preferences | Build, Execution, Deployment | Build Tools | Maven). -
Reload Maven projects in IntelliJ IDEA (Maven Tool Window | Reload All Maven Projects).
-
Perform a clean build of your project (e.g., run
mvn clean installor use Build | Rebuild Project in IntelliJ IDEA).
Additional Recommendations:
- Avoid committing compiled files (like
.classfiles) to your version control system (Git, SVN, etc.). Add them to your.gitignore(or SVN ignore list) file. - If possible, consider upgrading the JDK and Maven versions used by the project to versions with modern TLS support, although this might require code changes.
Comments
0 comments
Please sign in to leave a comment.