Learner
Learner

Reputation: 493

Spring Application - Application Context IOC

I am trying my hand on spring framework. This is my first basic spring application. I am following javatpoint tutorials. But I am getting an error. Can someone help me resolve it please.My code is as below

Student.java

package com.javatpoint;

public class Student {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public void displayInfo(){
    System.out.println("Hello: "+name);
}
}

Test.java

package com.javatpoint;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {
public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    Student student=(Student)context.getBean("studentbean");
    student.displayInfo();
}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="studentbean" class="com.javatpoint.Student">
<property name="name" value="Archana Ravindran"></property>
</bean>

</beans>

Error

Feb 10, 2016 9:56:39 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3aa9f827: startup date [Wed Feb 10 09:56:39 IST 2016]; root of context hierarchy Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/asm/ClassVisitor at org.springframework.context.support.AbstractRefreshableApplicationContext.customizeBeanFactory(AbstractRefreshableApplicationContext.java:218) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at com.javatpoint.Test.main(Test.java:15) Caused by: java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 7 more

My .classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="con" path="melibrary.com.genuitec.eclipse.lib.spring3.MYECLIPSE_SPRING30_CORE"/>
    <classpathentry kind="lib" path="D:/Spring/spcorejars/org.springframework.beans-3.0.1.RELEASE-A.jar"/>
    <classpathentry kind="lib" path="D:/Spring/spcorejars/org.springframework.core-3.0.1.RELEASE-A.jar"/>
    <classpathentry kind="lib" path="D:/Spring/spcorejars/org.springframework.asm-3.0.1.RELEASE-A.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

Error that I am getting External Jar addition

Upvotes: 0

Views: 781

Answers (2)

Praveen Kumar K S
Praveen Kumar K S

Reputation: 3074

To answer your comment question in short

  1. Springframework requires dependencies (jar files)to execute your program successfully.

In an elaborate manner

  1. Usually basic java programs doesn't require any third party libraries as a dependencies. You need to import your jdk in IDE which will take care everything.
  2. When you use to other frameworks like spring, struts, JSF you are exposed to the open source and outside world which requires multiple dependencies for these framework to run your java programs. Build tools like maven, gradle, m2e plugins(in your IDE)does a brilliant job in handling these dependencies.
  3. If you want to find the dependencies you can execute commands offered by these build tools.
  4. Every frameworks is designed/architected in such a way to address a specific solution. But remember one thing, all these frameworks have been developed in 100% java. JDK Versions may take different dimensional advancements to make your life simpler in the form of functional programming. But your OOPS concept and Java Fundamentals will never change.

Upvotes: 0

Praveen Kumar K S
Praveen Kumar K S

Reputation: 3074

Solution to your Question

  1. While you are building for SpringFramework versions 3.X and above, it is recommended to use the Bill of Materials and download all the JAR Files in your workspace classpath
  2. In Your case you are missing asm.jar in your classpath, Hence you are getting this exception. Please download it from here
  3. Also, permanent solution for your ClassNotFoundException, In your eclipse you have click CTRL+SHIFT+T, It will open the classfiles which is included in Jars. In your case you search for ClassVisitor as shown in the image.open type. If you are using Java 7 and Java 8 version no need to add this jar file. It is already built in as shown in above figure. JDK 7
  4. If you are using Alternate JAVA versions, you have to download asm-all-5.0.4.jar from maven central repository and place in your classpath.
  5. Please refer the figure given below for placing in your classpath.
  6. Please add apache commons logging jar file in your classpath

Upvotes: 1

Related Questions