Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Proste zadania ze spring-sa

0 views
Skip to first unread message

vds

unread,
Sep 2, 2007, 7:45:14 AM9/2/07
to
Witam.

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.

mproch

unread,
Sep 3, 2007, 4:04:53 AM9/3/07
to
moze to nie jest zbyt odkrywcza odpowiedz - ale jedyna rzecz jaka sie
nasuwa to to, ze beans.xml jest nie w tym katalogu gdzie byc powinien
(czyli w glownym katalogu targeta...) sprobuj sie upewnic gdzie ten
plik jest w zrodlach i gdzie laduje przy kompilacji

vds

unread,
Sep 3, 2007, 7:07:56 AM9/3/07
to

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

Przemek Wiśniewski

unread,
Sep 3, 2007, 10:50:22 AM9/3/07
to
Wlasnie - czego uzywacie do rozpoznawania takich bledow? Ja probowalem
kilka rzeczy. Pierwsze co robilem to logowalem sobie co sie dalo i
czytalem to co jest na konsoli. Czasami to jest skuteczne, czasami
nie, zaleznie od stopnia skomplikowania problemu. Poza tym zawsze
zostaje w programie smietnik po takim logowaniu. Po drugie probowalem
debugowac, ale AOP skutecznie mnie zniechecil. Trudno jest debugowac
kod, ktory zostal utworzony dynamicznie. Po trzecie probowalem
profilowac co tez sie nie udaje glownie przez mnogosc metod i
stacktrace-y wielkie na 5 ekranow.

Daniel K

unread,
Sep 3, 2007, 3:04:41 PM9/3/07
to
Podobnie jak mproh sadzę, że twój plik xml nie znajduje się w głównym
katalogu.
Jeżeli twój dokument XML z definicją beanów znajduje się w pakiecie, w
którym znajduje się klasa musisz podać ścieżkę.
ApplicationContext context = new
ClassPathXmlApplicationContext("project2/beans.xml");
Możesz również użyć wyrażeń regularnych w celu wyszukania w
strukturze, pliku o podanej nazwie.
W Twoim przypadku wyglądało by to tak:
ApplicationContext context = new ClassPathXmlApplicationContext("**/
beans.xml");

Łukasz Lipka

unread,
Sep 4, 2007, 4:01:30 AM9/4/07
to

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

0 new messages