kopever
kopever

Reputation: 13

Cannot get spring bean?

Can't get bean from spring container in listener or filter classes.

Upvotes: 1

Views: 5588

Answers (1)

user3145373 ツ
user3145373 ツ

Reputation: 8146

Have you used something like this :

@Autowired
private ApplicationContext context;

ICustomer customer = (ICustomer) context.getBean("name"); // here name is the name of the bean you want to get context

look I have used this in my project like this ans works well :

String processorName = Introspector.decapitalize(command.getClass().getSimpleName() + "Processor");
ICommandProcessor processor = (ICommandProcessor) context.getBean(processorName);

here if I ll pass command Add then it ll pass processorName = AddProcessor and get it's context.

what is your requirement ? can you post code ?

Look this xml file then check yours from it :

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">            
            <property name="packagesToScan" value="com.domain"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.generate_statistics">true</prop>
                </props>
            </property>
        </bean>

<!-- Transaction Manager -->
    <bean id="txManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />

here : <property name="packagesToScan" value="com.domain"/> is important when you want to use @Autowired in my project it's com.domain, so refer this and make changes as you need. then again post.

Upvotes: 2

Related Questions