Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 598
Michal Ryzio Avatar asked Oct 30 '22 19:10

Michal Ryzio


1 Answers

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>
like image 148
Vivek Kumar Avatar answered Nov 11 '22 13:11

Vivek Kumar