I was reading through a Spring tutorial and came across the following example. It mentioned that Spring supports the Java EE annotation #Resource. I was trying the example with the source below, but it gave an InvocationTargetException. I suppose it was probably due to the SpellChecker object could not be injected properly.
Relevant stacktrace:
Feb 27, 2018 8:56:23 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#5d76b067: startup date [Tue Feb 27 20:56:23 CST 2018];root of context hierarchy
Feb 27, 2018 8:56:32 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [Beans.xml]
Inside TextEditor constructor.Inside SpellChecker constructor.
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.NullPointerException at com.tutorialspoint.TextEditor.spellCheck(TextEditor.java:22) at com.tutorialspoint.MainApp.main(MainApp.java:10)
... 8 more
I attempted with the #Autowired annotation instead of the #Resource annotation, and it gave the expected result:
Inside TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.
Would like to see if you can kindly advise / point out mistakes if any, thanks a lot.
(Reference:
https://www.tutorialspoint.com/spring/spring_jsr250_annotations.htm)
[TextEditor.java]
import javax.annotation.Resource;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor() {
System.out.println("Inside TextEditor constructor.");
}
#Resource(name = "spellChecker")
public void setSpellChecker(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling(); //Gave InvocationTargetException here
}
}
[SpellChecker.java]
public class SpellChecker {
public SpellChecker() {
System.out.println("Inside SpellChecker constructor.");
}
public void checkSpelling() {
System.out.println("Inside checkSpelling.");
}
}
[MainApp.java]
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
[Beans.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:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for textEditor bean without constructor-arg -->
<bean id = "textEditor" name = "textEditor" class = "com.tutorialspoint.TextEditor"></bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" name = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>
Related
I am trying to follow the example under #Configuration & #Bean Annotations section from link https://www.tutorialspoint.com/spring/spring_java_based_configuration.htm, and I got the following exception:
Dec 31, 2018 2:59:33 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#2dda6444: startup date [Mon Dec 31 14:59:33 EST 2018]; root of context hierarchy
Exception in thread "main" java.lang.IllegalArgumentException
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:139)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:282)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
at com.tutorialspoint.MainApp.main(MainApp.java:9)
I am using java 1.8, spring 3.2.1. Thanks for your help.
package com.tutorialspoint;
import org.springframework.context.annotation.*;
#Configuration
public class HelloWorldConfig {
#Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
}
It failed on new AnnotationConfigApplicationContext(HelloWorldConfig.class) of the MainApp.java
If you can, use Spring 4.1.6, as specified in the tutorial.
I reproduced your error using spring-framework-3.2.1.RELEASE-dist.zip but the Hello World app ran fine for me using spring-framework-4.1.6.RELEASE-dist.zip.
I'm new to spring and I was trying constructor injection. I get an IllegalArgumentException in the first line of the main function. It does not have problems locating the xml file when I use a property tag inside the xml(setter injection) but here it is raising an exception.
Triangle.java
package myPackage;
public class Triangle
{
private String type;
public Triangle(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void draw()
{
System.out.println(getType()+" Triangle Drawn");
}
}
DrawingApp.java
package myPackage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle t = (Triangle) context.getBean("triangle");
t.draw();
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="triangle" class="myPackage.Triangle" >
<constructor-arg value="equilateral" />
</bean>
</beans>
These are the errors that I get.
Feb 05, 2018 1:41:14 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#2d8e6db6: startup date [Mon Feb 05 13:41:14 IST 2018]; root of context hierarchy
Feb 05, 2018 1:41:14 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Feb 05, 2018 1:41:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#17550481: defining beans [triangle]; root of factory hierarchy
Exception in thread "main" java.lang.IllegalArgumentException
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.core.LocalVariableTableParameterNameDiscoverer.inspectClass(LocalVariableTableParameterNameDiscoverer.java:112)
at org.springframework.core.LocalVariableTableParameterNameDiscoverer.getParameterNames(LocalVariableTableParameterNameDiscoverer.java:86)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:193)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1049)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at myPackage.DrawingApp.main(DrawingApp.java:12)
Trying to run simple spring code, getting NoSuchBeanDefinitionException.
Defined a class in beans.xml with id - "sayHelloImpl".
Created applicationContext of ClassPathXmlApplicationContext class and provided a path to the beans.xml file. When invoking .getBean("sayHelloImpl") getting NoSuchBeanDefinitionException.
beans.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<bean id="sayHelloImpl" class = "com.Impl.SayHelloImpl"></bean>
</beans>
SayHelloImpl.java file
package com.Impl;
import org.springframework.stereotype.Service;
import com.Interface.SayHello;
#Service("sayHelloImpl")
public class SayHelloImpl implements SayHello {
#Override
public String helloSpring() {
System.out.println("inside helloSpring()");
return "Hello Spring 4.1.7";
}
}
Main.java file
package com.Main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.Interface.SayHello;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath*:beans.xml");
SayHello hello = (SayHelloImpl) applicationContext
.getBean("sayHelloImpl");
System.out.println(hello.helloSpring());
}
}
After executing this main file I am getting below exception on console,
Jan 14, 2018 4:51:15 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#5d099f62: startup date [Sun Jan 14 16:51:15 IST 2018]; root of context hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sayHelloImpl' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1168)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
at com.Main.Main.main(Main.java:14)
Snapshot of the project structure
Just 2 days into java & spring.
Trying to pull data from mySQL DB using spring in a maven project.
Following a tutorial.
But when the code is run, getting the following error(Only a portion of the error is attached).
All the codes are attached below.
error:
Jul 25, 2015 11:00:15 PM
org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext#da2dbb: startup date [Sat Jul 25 23:00:15 IST 2015]; root of context hierarchy
Jul 25, 2015 11:00:15 PM
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/caveofprogramming/spring/test/beans/Beans.xml]
Jul 25, 2015 11:00:16 PM
org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from class path resource [com/caveofprogramming/spring/props/jdbc.properties]
Jul 25, 2015 11:00:16 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#f438e: defining beans [offersDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource]; root of factory hierarchy
Jul 25, 2015 11:00:16 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#f438e: defining beans [offersDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'offersDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.caveofprogramming.spring.test.OffersDAO.setDataSource(javax.sql.DataSource); nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at
App.java
package com.caveofprogramming.spring.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/caveofprogramming/spring/test/beans/Beans.xml");
OffersDAO offersDao = (OffersDAO)context.getBean("offersDao");
List<Offer> offers = offersDao.getOffers();
for(Offer offer:offers){
System.out.println(offer);
}
((ClassPathXmlApplicationContext)context).close();
}
}
Offer.java
package com.caveofprogramming.spring.test;
public class Offer {
private String author;
private String title;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#Override
public String toString() {
return "Offer [author=" + author + ", title=" + title + "]";
}
}
OffersDAO.java
package com.caveofprogramming.spring.test;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
#Component("offersDao")
public class OffersDAO {
private JdbcTemplate jdbc;
#Autowired
public void setDataSource(DataSource jdbc) {
System.out.println("TESTINGGGGGGGGG in setDataSource");
this.jdbc = new JdbcTemplate(jdbc);
}
public List<Offer> getOffers(){
return jdbc.query("select * from library.book", new RowMapper<Offer>(){
public Offer mapRow(ResultSet rs, int rowNum) throws SQLException {
Offer offer = new Offer();
offer.setTitle(rs.getString("title"));
offer.setAuthor(rs.getString("author"));
return offer;
}});
}
}
jdbc.properties
jdbc.username = root
jdbc.password = 123456
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/library
Beans.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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.caveofprogramming.spring.test"> </context:component-scan>
<context:property-placeholder
location="com/caveofprogramming/spring/props/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="username" value="${jdbc.username}"></property>
</bean>
</beans>
Do ask, if more clarity is needed.
Thanks in advance
Your exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool which basically means the required java class is not on the class path.
The problem is due to commons-pool not being in your class path.
Ideally this should be availed when your add the apache tomcat server as a server in your project and doing will get the commons-pool from the server
If this is not the case then kindly add it in the lib folder by downloading it
If it's a maven project then it has it in the pom.xml
commons-pool
commons-pool
1.5.6
I am using simple setter injection for setting values to a varaible in a class
But when i try to run the application, the object returns me null values
What could be the reason?
Below are the files.
Spring.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans2.0.dtd">
<beans>
<bean id = "triangle" class="com.test.Triangle">
<property name = "type" value = "Equilateral" />
</bean>
</beans>
Triangle.java
public class Triangle {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void draw() {
System.out.println(getType() + " Triangle drawn");
}
}
DrawingApplication.java
package com.test;
public class DrawingApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
Triangle t = (Triangle) context.getBean("triangle");
t.draw();
}
}
I tried to replicate the problem statement. This is my package structure:
Spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id = "triangle" class="com.test.Triangle">
<property name = "type" value = "Equilateral" />
</bean>
</beans>
DrawingApplication.java:
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
Triangle t = (Triangle) context.getBean("triangle");
t.draw();
}
}
Triangle.java:
package com.test;
public class Triangle {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void draw() {
System.out.println(getType() + " Triangle drawn");
}
}
Pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Example</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
I ran the above code and got following output:
Jun 17, 2014 12:17:21 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#693a317a: startup date [Tue Jun 17 12:17:21 GMT+05:30 2014]; root of context hierarchy
Jun 17, 2014 12:17:21 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Spring.xml]
Jun 17, 2014 12:17:21 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#5954864a: defining beans [triangle]; root of factory hierarchy
Equilateral Triangle drawn
I got the expected output, so looks like there is some issue with the classpath and the location of spring.xml file.