Announcement Announcement Module
Collapse
No announcement yet.
Bean profiles not working - I always got NoSuchBeanDefinitionException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean profiles not working - I always got NoSuchBeanDefinitionException

    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 :
    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 this is my bean configuration file :
    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>
    And finally I instanciate the bean like this :
    Code:
    ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/com/di/testspring/test-bean.xml");
    TestBean t = (TestBean) ctx.getBean("testBean"); 
    System.out.println(t.toString());
    The error I got :
    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)
    Could you please help me.

    Thanks in advance,
    Ilalaina.

  • #2
    context-param doesn't do anything for servlet, add init-param to servlet definition.

    Comment


    • #3
      Thanks for your answer.
      Unfortunately it didn't solve my problem. Here's my new web.xml but I always got the same error :
      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>
      		<init-param>
      			<param-name>spring.profiles.active</param-name>
      			<param-value>DEV</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>
      
      </web-app>

      Comment

      Working...
      X