Announcement Announcement Module
Collapse
No announcement yet.
ApplicationContext : getBean() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ApplicationContext : getBean()

    Hi,

    I need to instantiate a collection of beans which needs to be added to a Global list.
    So, I did the following:

    HTML Code:
    // AppContext class has an instance of the ApplicationContext 
    ApplicationContext ctx = AppContext.getApplicationContext();  
    for () {
    ...
    IbRequestRealTimeBarsController baseRetrieve = (IbRequestRealTimeBarsController)     ctx.getBean("IbRequestRealTimeBarsController");
    ...
    Global.currencyPairThreadList.add(baseRetrieve);
    }

    HTML Code:
    @Service
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public class IbRequestRealTimeBarsController {
    ...
    }
    The following exeption occurs:

    PHP Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionExceptionNo bean named 'IbRequestRealTimeBarsController' is defined
        org
    .springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:553)
        
    org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
        
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
        
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        
    org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1098)
        
    com.stockdomain.controller.CurrencyPairThreadObserverController.setupViewTables(CurrencyPairThreadObserverController.java:178)
        
    com.stockdomain.controller.CurrencyPairThreadObserverController.setupForm(CurrencyPairThreadObserverController.java:45)
        
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        
    java.lang.reflect.Method.invoke(Method.java:597)
        
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
        
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
        
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
        
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
        
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
        
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
        
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
        
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803

    what did I do wrong or forget ?

    Thank in advance,

    Frank

  • #2
    Hi Frank,

    Can you please post your configurations?

    Comment


    • #3
      When a component is autodetected as part of the scanning process, its bean name is generated by the BeanNameGenerator strategy known to that scanner. By default, any Spring stereotype annotation (@Component, @Repository, @Service, and @Controller) that contains a name value will thereby provide that name to the corresponding bean definition.

      If such an annotation contains no name value or for any other detected component (such as those discovered by custom filters), the default bean name generator returns the uncapitalized non-qualified class name. For example, if the following two components were detected, the names would be myMovieLister and movieFinderImpl:

      Please read following section on Spring documentation http://static.springsource.org/sprin...name-generator

      Comment


      • #4
        Hi Amiladominge,

        thanks for reply. Here's the configuration

        PHP 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:tx="http://www.springframework.org/schema/tx"
               xmlns:context="http://www.springframework.org/schema/context"
               xmlns:task="http://www.springframework.org/schema/task"
               xsi:schemaLocation="
                   [url]http://www.springframework.org/schema/beans[/url] 
                   [url]http://www.springframework.org/schema/beans/spring-beans-3.0.xsd[/url]
                   [url]http://www.springframework.org/schema/tx[/url] 
                   [url]http://www.springframework.org/schema/tx/spring-tx-3.0.xsd[/url]
                   [url]http://www.springframework.org/schema/context[/url] 
                   [url]http://www.springframework.org/schema/context/spring-context-3.0.xsd[/url]
                   [url]http://www.springframework.org/schema/task[/url] 
                   http://www.springframework.org/schema/task/spring-task-3.0.xsd">           
                   
            <context:component-scan base-package="com.stockdomain">
                <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            </context:component-scan>    
            
            <bean id="contextApplicationContextProvider" class="com.stockdomain.util.ApplicationContextProvider"></bean>         

            <!-- dataSource -->
            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName"><value>org.postgresql.Driver</value></property>
              <property name="url"><value>jdbc:postgresql://localhost/db_finance</value></property>
              <property name="username"><value>xxxxx</value></property>
              <property name="password"><value>xxxxx</value></property>
            </bean> 
            
            <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
                <property name="entityManagerFactory" ref="entityManagerFactory" />
            </bean>  

            <bean id="entityManagerFactory"
                  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="persistenceUnitName" value="mypersistenceunit" />
                <property name="dataSource" ref="dataSource" />
                <property name="jpaVendorAdapter">
                    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                        <property name="showSql" value="true" />
                        <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
                    </bean>
                </property>
                <property name="jpaPropertyMap">
                    <map>
                        <entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
                    </map>
                </property>
            </bean>
                
            <tx:annotation-driven transaction-manager="transactionManager"/> 
            
        </beans>
        Last edited by fv967; Oct 9th, 2012, 01:20 AM.

        Comment


        • #5
          Have you read the link he posted? The bean name is wrong it starts with a lowercase letter (classname with first letter lowercased).

          Comment


          • #6
            Many thanks for reply, will put on lowercase and test later today when back in office.

            Comment


            • #7
              I changed the class name to lowercase, and now it works : ctx.getBean("ibRequestRealTimeBarsController")

              Many thanks,

              Frank

              Comment

              Working...
              X