Spring Application - Application Context IOC

Learner

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

Praveen Kumar K S

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Getting Spring Application Context

Spring application context encryption

Spring application context is null

Spring Application context not found

Root Context path in spring application

Adding Plugins to Application Context in Spring

Differents application Context in Spring 4

Servlet context attribute from Spring application context

How to integrate spring context of library to application context?

Spring root application context and servlet context confusion

Access application context from spring securty context

java spring: how to access Application Context of another Spring Application

Spring Boot: pruning application context for integration testing

Reload Spring application context after every test

What is Application context and bean factory in spring framework

Removing Spring Beans from Application Context

Start and shutdown Spring application context in a unit test

What is the purpose of having multiple spring "application context"

Spring Application context is null in a ApplicationContextAware class

Spring Application Context available inside the constructor

Spring import application context from another project

Spring application deployed but context fails to start

What is a default Spring Boot Application Context?

Run a method only at Spring Application Context startup?

Spring JUnit Test not loading full Application Context

Spring JUnit Test not loading full Application Context

Programmatically adding Beans to Spring application Context

load spring application context from string

No Bean Found in Spring Boot application context