Chciałem zrobić sobie prosty program aby przetestowac spring-a. (IDE
jdeveloper).
Mam Klase Class1
package project2;
public class Class1 {
private int i;
private int j;
public Class1() {
}
public void setI(int i) {
this.i = i;
}
public int getI() {
return i;
}
public void setJ(int j) {
this.j = j;
}
public int getJ() {
return j;
}
/**
* @return
*/
public int suma(){
return (this.i+this.j);
}
}
wypisy w 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="sumowanie" class="project2.Class1">
<property name="i" value="1"></property>
<property name="j" value="2"/>
</bean>
</beans>
i kod rozruchowy main
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[]
{ "classpath*:beans.xml" });
Class1 c = (Class1) context.getBean("sumowanie");
// of course, an ApplicationContext is just a BeanFactory
//BeanFactory factory = (BeanFactory)context;
System.out.print(c.suma());
efekt koncowy
/usr/lib/jvm/java-6-sun-1.6.0.00/bin/java -server -classpath /home/vds/
jdevhome/mywork/Application2/Project2/classes:/home/vds/Desktop/
jdevstudiobase10133.zip_FILES/jakarta-taglibs/commons-logging-1.0.3/
commons-logging-api.jar:/home/vds/Desktop/
jdevstudiobase10133.zip_FILES/jakarta-taglibs/commons-logging-1.0.3/
commons-logging.jar:/home/vds/Desktop/spring-framework-2.0.6/dist/
spring-aspects.jar:/home/vds/Desktop/spring-framework-2.0.6/dist/
spring-mock.jar:/home/vds/Desktop/spring-framework-2.0.6/dist/spring-
src.zip:/home/vds/Desktop/spring-framework-2.0.6/dist/spring.jar:/home/
vds/Desktop/spring-framework-2.0.6/dist/modules/spring-aop.jar:/home/
vds/Desktop/spring-framework-2.0.6/dist/modules/spring-beans.jar:/home/
vds/Desktop/spring-framework-2.0.6/dist/modules/spring-context.jar:/
home/vds/Desktop/spring-framework-2.0.6/dist/modules/spring-core.jar:/
home/vds/Desktop/spring-framework-2.0.6/dist/modules/spring-dao.jar:/
home/vds/Desktop/spring-framework-2.0.6/dist/modules/spring-
hibernate2.jar:/home/vds/Desktop/spring-framework-2.0.6/dist/modules/
spring-hibernate3.jar:/home/vds/Desktop/spring-framework-2.0.6/dist/
modules/spring-ibatis.jar:/home/vds/Desktop/spring-framework-2.0.6/
dist/modules/spring-jca.jar:/home/vds/Desktop/spring-framework-2.0.6/
dist/modules/spring-jdbc.jar:/home/vds/Desktop/spring-framework-2.0.6/
dist/modules/spring-jdo.jar:/home/vds/Desktop/spring-framework-2.0.6/
dist/modules/spring-jms.jar:/home/vds/Desktop/spring-framework-2.0.6/
dist/modules/spring-jmx.jar:/home/vds/Desktop/spring-framework-2.0.6/
dist/modules/spring-jpa.jar:/home/vds/Desktop/spring-framework-2.0.6/
dist/modules/spring-portlet.jar:/home/vds/Desktop/spring-
framework-2.0.6/dist/modules/spring-remoting.jar:/home/vds/Desktop/
spring-framework-2.0.6/dist/modules/spring-struts.jar:/home/vds/
Desktop/spring-framework-2.0.6/dist/modules/spring-support.jar:/home/
vds/Desktop/spring-framework-2.0.6/dist/modules/spring-toplink.jar:/
home/vds/Desktop/spring-framework-2.0.6/dist/modules/spring-web.jar:/
home/vds/Desktop/spring-framework-2.0.6/dist/modules/spring-webmvc.jar
project2.Application1
2007-09-02 13:35:33
org.springframework.context.support.AbstractApplicationContext
prepareRefresh
INFO: Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@281d4b:
display name
[org.springframework.context.support.ClassPathXmlApplicationContext@281d4b];
startup date [Sun Sep 02 13:35:33 CEST 2007]; root of context
hierarchy
2007-09-02 13:35:34
org.springframework.context.support.AbstractApplicationContext
obtainFreshBeanFactory
INFO: Bean factory for application context
[org.springframework.context.support.ClassPathXmlApplicationContext@281d4b]:
org.springframework.beans.factory.support.DefaultListableBeanFactory@1d6f122
2007-09-02 13:35:34
org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@1d6f122:
defining beans []; root of factory hierarchy
Exception in thread "main"
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'sumowanie' is defined
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:
356)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:
916)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:
243)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:
160)
at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:
733)
at project2.Application1.main(Application1.java:39)
Process exited with exit code 1.
Co robie źle. clasa jest zdefiniowania. bens zdefiniowany.
A czy nie powinno sie wywalic w miescu tej dekalracji
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[]
{ "classpath*:beans.xml" }); podajc ze nie moze otworzyc, znalesc
pliku xml
Witam,
po tym wyjątku:
Exception in thread "main"
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'sumowanie' is defined
dokładnie widać, że nie udało się pobrać w ApplicationContext beanu o
nazwie 'sumowanie' więc trzeba zmienić odpowiednio ścieżkę w
ClassPathXmlApplicationContext, żeby odczytał beans.xml
Pozdrowienia
Łukasz Lipka