hi to all this my first post in this forum, please excuse me for my English.
i created an application that uses spring and can not access the beans correctly this:
Code:context = new ClassPathXmlApplicationContext("application-model.xml"); Categoria cat = (Categoria ) context.getBean("categoria ");
but when I try to create the factory can not, please could you help me? shipping details below
application-model.xml
Code:<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="categoria" class="model.Categoria" /> <bean id="endereco" class="model.Endereco" /> <bean id="lance" class="model.Lance" /> <bean id="post" class="model.Post" /> <bean id="projeto" class="model.Projeto" /> <bean id="tipolance" class="model.TipoLance" /> <bean id="usuario" class="model.Usuario" /> </beans>
application-factorys.xml
Code:<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="modelfactory" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constructor-arg value="application-model.xml" /> </bean> </beans>
ServiceLocator.java
Code:package utils; import org.apache.commons.lang.NullArgumentException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.access.BeanFactoryLocator; import org.springframework.beans.factory.access.BeanFactoryReference; import org.springframework.context.access.ContextSingletonBeanFactoryLocator; public class ServiceLocator { private static final ServiceLocator INSTANCE = new ServiceLocator(); private final BeanFactoryReference factoryReference; private ServiceLocator() { BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator.getInstance(); factoryReference = factoryLocator.useBeanFactory("modelfactory"); } public static ServiceLocator getInstance() { return INSTANCE; } public Object getObject(String name) { try { if (name == null) { throw new NullArgumentException("name"); } BeanFactory factory = this.factoryReference.getFactory(); return factory.getBean(name); } catch (RuntimeException exception) { throw exception; } } }
web.xml
Code:<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Lance Digital</display-name> <!-- Listner Tiles --> <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener> <!-- Listner Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Inicialização de contexto Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/classes/application-factorys.xml WEB-INF/classes/applicationContext.xml WEB-INF/classes/application-aspectos.xml WEB-INF/classes/application-daos.xml WEB-INF/classes/application-services.xml WEB-INF/classes/application-actions.xml WEB-INF/classes/application-security.xml WEB-INF/classes/application-model.xml </param-value> </context-param> <!-- Inicialização de contexto Tiles 2 --> <context-param> <param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </param-name> <param-value> /WEB-INF/classes/tiles.xml </param-value> </context-param> <!-- Filtros --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <!-- Mapeamento dos filtros --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Pagina de boas vindas --> <welcome-file-list> <welcome-file>/principal.action</welcome-file> </welcome-file-list> <!-- Login Config --> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>
error log
Code:14:54:43,106 INFO [STDOUT] 2009-09-21 14:54:43,106 [http-localhost%2F127.0.0.1-8080-1] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15cda11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@15cda11]; startup date [Mon Sep 21 14:54:43 GMT-03:00 2009]; root of context hierarchy 14:54:43,112 INFO [STDOUT] 2009-09-21 14:54:43,112 [http-localhost%2F127.0.0.1-8080-1] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@15cda11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@f8f6d0 14:54:43,113 INFO [STDOUT] 2009-09-21 14:54:43,113 [http-localhost%2F127.0.0.1-8080-1] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f8f6d0: defining beans []; root of factory hierarchy 14:54:43,147 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'modelfactory' is defined


Reply With Quote