Hi all,
I'm new to Spring. I tried some tutorials on setting up spring bean profiles but I always got NoSuchBeanDefinitionException error, unless I add an argument -Dspring.profiles.active=myEnv.
I'm using STS with Eclipse Juno, Spring 3.1, Java 1.6.
Below is my web.xml :
and this is my bean configuration file :Code:<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>spring.profiles.active</param-name> <param-value>DEV</param-value> </context-param> </web-app>
And finally I instanciate the bean like this :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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <beans profile="PROD"> <bean id="testBean" name="testBean" class="com.di.testspring.TestBean"> <property name="attr1" value="testPROD1" /> <property name="attr2" value="testPROD2" /> </bean> </beans> <beans profile="DEV"> <bean id="testBean" name="testBean" class="com.di.testspring.TestBean"> <property name="attr1" value="testDEV1" /> <property name="attr2" value="testDEV2" /> </bean> </beans> </beans>
The error I got :Code:ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/com/di/testspring/test-bean.xml"); TestBean t = (TestBean) ctx.getBean("testBean"); System.out.println(t.toString());
Could you please help me.Code:INFO : org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@89cf1e: startup date [Mon Jan 21 11:56:37 EAT 2013]; root of context hierarchy INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [com/di/testspring/test-bean.xml] INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@19bfb30: defining beans []; root of factory hierarchy Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testBean' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:549) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1096) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117) at com.di.testspring.Main.main(Main.java:13)
Thanks in advance,
Ilalaina.


Reply With Quote