Michal Ryzio
Michal Ryzio

Reputation: 51

ApplicationContext import in spring

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

Upvotes: 5

Views: 27324

Answers (5)

Pooja Dasari
Pooja Dasari

Reputation: 1

It worked for me after adding spring context dependencies in pom

Upvotes: 0

harshrj2110
harshrj2110

Reputation: 1

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>

Upvotes: 0

Vivek Kumar
Vivek Kumar

Reputation: 407

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>

Upvotes: 2

Tobi Nonymous
Tobi Nonymous

Reputation: 611

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

Upvotes: 0

kuhajeyan
kuhajeyan

Reputation: 11017

You have to add dependency

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.x.x</version>
</dependency>

Upvotes: 0

Related Questions