Using STS, how to add application context - java

I need some help with setting up a spring project.
I am busy going through the book “Spring in action” and I need to try some of the examples out. I have looked at plenty of pages and nowhere can is see where I am going wrong. It must be something silly that I missed or overlooked.
I installed Spring source tool suite
Created a new java/maven project
Added a new applicationContext.xml bean definition file
The outline of the project looks as follows
I created my beans (vwCar & nissanCar that implements car interface) and where it comes to use them I have a main method in the app class. I need to create an application context.
ApplicationContext context = new ClassPathApplicationContext("src/main/resources/applicationContext.xml");
But I have difficulty creating the ApplicationContext. It gives me an error and code assist don’t work
Using code assist the only thing that it suggests is (Pressing Ctrl+Space after typing app):
If I just type it out I get an error
ApplicationContext cannot be resolved to a type in class App.java
Is there something that I should import myself?
I can see an "S" on the project folder - doesn't this indicate that the project is already spring enabled?
---------ADDED AFTER ALEX COMMENTED TO SUGGEST THAT I SHOULD ADD A MAVEN DEPENDENCY-------------
I added the missing dependency like Alex suggested but I don't know what the correct version is. If I look a the STS directory I see several files named ... 2.9.2
org.springframework.ide.eclipse.feature_2.9.2.201205070117-RELEASE
but if I add the dependency with 2.9.2 i get the following error on my POM
Missing artifact org.springframework:spring-context:jar:2.9.2
My POM looks like below
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.chrispie</groupId>
<artifactId>MyMavenSpringProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyMavenSpringProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
</project>

Since you're using Maven you should add the following dependency to your pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
Where ${org.springframework-version} should be replaced with the version you are using.
This will ensure that the Spring jars needed to get started are available to your application.
It must be something silly that I missed or overlooked.
I think you'v just overlooked the dependency management functionality of Maven. Creating a Java/Maven project doesn't pull in the required Spring jars. If you used a template project from the STS landing page this would all have been setup for.

Related

package spark does not exist

I'm just starting to learn Java.
I am using IntelliJ to create a SparkJava (see: sparkjava.com) app using openjdk 17. I followed the tutorial instructions outlined in "Setting up Spark with Maven" (https://sparkjava.com/tutorials/maven-setup). I believe the instructions are very outdated because they did not work. After some googling, I finally just arrived at the following code and POM.xml. When I build the project, I get an error: java: package spark does not exist
I don't know what to do.
I added the dependency to my POM.xml.
I added the apache.spark.core_2.13 library via Project Structure.
I googled "IntelliJ package does not exist" but couldn't find a helpful answer. Most everyone said to "add the dependency to the POM" but I've already done that.
I googled "add package to Java project IntelliJ" but after clicking a number of linkis, I couldn't find a helpful answer. I tried a few of the suggestions, but none resolved this problem.
I think I am missing something fundamental here, like somehow I'm not telling IntelliJ where to find the spark code.
src/main/java/Sparky.java
import static spark.Spark.*;
public class Sparky {
public static void main(String[] args){
get("/hello", (req, res) -> "Hello World");
}
}
pom.xml
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>sparky</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.13</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
I guess you didn't configure IntelliJ for Maven properly. Either your project is not a Maven project (as far as IntelliJ is concerned, even if the structure of the project is valid for Maven), or you're missing an IntelliJ plugin or something. Look at IntelliJ's doc on how to create and manage a Maven project.
The dependency package was completely wrong. It was pointing at Apache Spark, not Spark Java.
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.3</version>
</dependency>

dependencies from pom.xml not being resolved

I'm trying to create a maven project in intellij, to create a parser in antlr. Here is my pom.xml:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mua</groupId>
<artifactId>json-parser-java-antlr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Json parser Java ANTLR</name>
<packaging>jar</packaging>
<description>Trying to create a parser using ANTLR in Java, as facing problems with LLVM</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
</dependency>
</dependencies>
</project>
When I click on import changes, it loads for just 1-2 seconds then done. But if I try to import MapUtils from org.apache.commons.collections4.MapUtils it says it can't resolve common, although I added in <groupId>org.apache.commons</groupId> dependency.
I'm new in maven project creation and management.
So, what is the problem here and how can I resolve this problem ?
I studied some pom.xml and found a parent attribute. No idea how to configure that.
My Eclipse editor shows:
Project build error: 'dependencies.dependency.version' for org.antlr:antlr4-runtime:jar is missing.
Project build error: 'dependencies.dependency.version' for org.apache.commons:commons-collections4:jar is missing.
Project build error: 'dependencies.dependency.version' for org.apache.directory.studio:org.apache.commons.io:jar is missing.
None of the dependencies work, because it doesn't know which version of them you want.
Specifying dependencies without a version is something you do when you have a parent pom. You don't, so versions are mandatory.
You could try adding a specific version of antlr to your pom.xml. And if you are using the antlr plugin as well, make sure the version of antlr run-time you are using is the same as the built-in version of the plugin.

Unable to import keycloak even though defined in pom.xml

I am new to maven and keycloak.
I am trying to write a simple extension for keycloak and following the tutorial from the docs.
The problem is that even though I have defined keycloak in my dependencies in pom.xml, I am unable to import them in my code.
Here are the images of my snippet
pom.xml
image
/src/main/java/com/example/rest/HelloResourceProviderFactory.java
image
Just for testing, I tried to put spring in pom.xml, and I was able to import that.
Is there some different procedure for creating an SPI for keycloak? or is there a different way to import keycloak dependencies.
You are missing the version for your keycloak dependencies
Change pom.xml and add a property for your keycloak version (e.g. 6.0.1).
Use the version parameter in all your keylocak dependencies.
<project ...>
<artifactId>...</artifactId>
...
<properties>
<keycloak.version>6.0.1</keycloak.version>
</properties>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>${keycloak.version}</version>
<scope>provided</scope>
</dependency>
...

Spring Boot SLF4J Error

I have been trying for several days to get the spring boot 15 minute hello world working. I keep encountering the following error:
Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
at org.apache.commons.logging.impl.SLF4JLocationAwareLog.warn(SLF4JLocationAwareLog.java:179)
at org.springframework.boot.SpringApplicationRunListeners.callFinishedListener(SpringApplicationRunListeners.java:91)
at org.springframework.boot.SpringApplicationRunListeners.finished(SpringApplicationRunListeners.java:72)
at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:810)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
at com.symphony.Application.main(Application.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
I know this question appears elsewhere on the site, I have tried countless things and nothing works. I am using IntelliJ 2016.1.3, Java version 1.8.0._60.
My pom.xml is:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>com.spring</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I have tried maven clean from IntelliJ and no luck.
EDIT: Screenshot added to show everything being pulled into project.
Maven Dependencies
UPDATE: Completely uninstalled maven 3.3.9 and only have the built in maven from IntelliJ ultimate. Still not working, extremely frustrating. Over a week into this and can't get the 15 minute demo working.
You appear to have an incompatibility with versions of logging frameworks. See if replacing the spring-boot-starter and spring-web artifacts with spring-boot-starter-web helps, as per the current Getting Started guide.
Update
I was able to configure and build the Spring Getting Started guide down to the Add Unit Tests heading without error on Intellij Ultimate 2016.2 using its own embedded Maven 3, and on the command-line using Maven 3.1.1 and Java 8 (1.8.0_31-b13).
I found significant build errors and/or execution errors when I:
changed the Maven configuration in Intellij to use Maven 2
compiled with the spring-boot-starter and spring-web artifacts rather than spring-boot-starter-web.
I think your problem is a build configuration error. Whenever I get these, I go back to command-line to build and run just to be sure the problem isn't an artifact of some IDE bug.
I've run in the same problem, but got it solved by excluding logback-classic
So my pom now Looks like this:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
If you're in Eclipse (idk, if IntelliJ has this Feature too) and look at the dependency hierarchy tab, you'll see, spring-boot itself implements multiple slf4j-apis.
Visualization of the dependency hierarchy filtered by "slf4j"
I know it may be too late for you, but your post was one of the first result by Google, so maybe it'll help someone else.
your pom lists java 8, but you said you are using java 6 for the project. make sure your project is pointing to a Java 8 ddk.

two war modules integration

i have made two maven projects : the first is a generic authentification module with spring security with this structure :
ear
|...warModule
| |...ejbModule
|...ejbModule
the second is a CRM have this structure
ear
|...warModule
|...ejbModule
|...ejbModule
now i want to integrate both so i can manage the CRM security with my authentification project (control url access ,permissions ...)is there a way to do that ?
One way is that, you declare first project as dependency in your second project POM file.
For detail you can check: Maven Dependency Mechanism
Sample code to add Dependencies:
<project>
...
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact 1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact 2</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>`
Second way and I would suggest to use Overlay approach to integrate multiple modules and another benefit to use overlay is can share common resources across multiple applications.
You have just to add a dependency declaration in one pom.xml file which point to the other artifact. For example lets say the downside lines are your CRM pom file:
<project>
<groupId>crm.group.id</groupId>
<artifactId>crm-artifact-id</artifactId>
<version>1.0</version>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>authentication.group.id</groupId>
<artifactId>authentication-artifact-id</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
It is just a sample and should be updated following your projects/modules group/artifact IDs.

Categories

Resources