5

I'm learning Spring from this tutorial:

http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024

In this tutorial, instructor downloads spring dependencies (spring-beans, spring context, spring-core) in version 3.2.3.RELEASE.

and then writes this code:

package com.caveofprogramming.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

   public static void main(String[] args) {

      ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");

      Person person = (Person)context.getBean("person");
      person.speak();
}
}

When I use: spring-context, spring-beans and spring-core in last version 4.3.3.RELEASE then ApplicationContext import doesn't work. It works when I change it to the old version. "Doesn't work" means that eclips doesn't know what I should import when I write "ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");" and when I write "import org.springframework.context.ApplicationContext" by myself it's underline.

What should I do to import ApplicationContext with newest version dependencies?

Edit: This is the error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
ApplicationContext cannot be resolved to a type
FileSystemXmlApplicationContext cannot be resolved to a type

at com.caveofprogramming.spring.test.App.main(App.java:10)

and this is my pom file:

<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.caveofprogramming.spring.test</groupId>
<artifactId>spring-tutorial-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>

Edit 2: I also saw that program accepts 4.1.1.RELEASE version. Mabye the newest version of dependencies isn't necessary? I'm just starting with Spring and everyone says that I should work on the newest version.

Edit 3;

The only solution which I found is using spring-context 4.1.1.RELEASE

abarisone
  • 3,707
  • 11
  • 35
  • 54
Michal Ryzio
  • 51
  • 1
  • 1
  • 4
  • 1
    Include the exact pom.xml you're using. Also describe the problem in detail, "import doesn't work" is not specific enough. – kryger Sep 26 '16 at 12:40

4 Answers4

2

Either add these jars in your class path org.springframework.context.support-3.1.0.RELEASE.jar and org.springframework.context-3.1.0.RELEASE.jar

Or add this dependency if you are using maven and update the project.

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>  
 <version>4.x.x.RELEASE</version>    
</dependency>
Trinimon
  • 13,839
  • 9
  • 44
  • 60
Vivek Kumar
  • 360
  • 1
  • 7
  • 16
0

You have to add dependency

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.x.x</version>
</dependency>
kryger
  • 12,906
  • 8
  • 44
  • 65
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
0

This might be a problem with eclipse, especially if you are using an older version. Eclipse used to have issues with Maven projects, when you added new dependencies.

You can force Eclipse to update it's Maven dependencies by right clicking your project and selecting Maven->Update project

enter image description here

After that your project should compile just fine: enter image description here

PS: Check the current documentation for up-to-date setup of XML-based application context setup

Tobi Nonymous
  • 581
  • 7
  • 14
  • My Eclipse version: 4.6.0 (Neon). "Update project..." doesn't work. I also deleted and then imported existing maven project without result. Program works when I use 4.1.1.RELEASE spring dependencies version. It will be huge mistake ? – Michal Ryzio Sep 26 '16 at 21:35
0

I added this in my pom.xml file inside the dependencies tag.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.3.7</version>
    <type>pom</type>
</dependency>

What it does is that it downloads the spring-context-5.3.7.jar file from which ApplicationContext class can be imported. Hope it works for u too.

Here is my pom.xml file for better reference.

<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.Sharma</groupId>
  <artifactId>NachoVarga</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>NachoVarga</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>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.7</version>
    <type>pom</type>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.3.7</version>
    <type>pom</type>
</dependency>

  </dependencies>
</project>
ZINE Mahmoud
  • 1,272
  • 1
  • 17
  • 32