You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Otho <ta...@googlemail.com> on 2009/04/07 19:58:31 UTC

T 5.1 How to inject Spring beans by name?

Since Spring beans are not exposed anymore as services I get exceptions like


Error obtaining injected value for field org.example.user.Login.dao: Spring
context contains 2 beans assignable to type org.example.dao.Dao: dao,
daoImpl.

I couldn't find anything in the docs about injecting by name. And the
proposes workaround for non singletons with injecting ApplicationContext and
then get the bean by name/id  works, but is a bit awkward in cases like
this.

Is there some other possibility? If not, is support for injection by name
planned in the future without the 5.0 compatibility mode?

Re: T 5.1 How to inject Spring beans by name?

Posted by Otho <ta...@googlemail.com>.
As far as I know there is no other direct solution. An indirekt one is to
have a separate service layer for the bean. In the dao/daoImpl example:

<bean id="daoService" class="DaoServiceImpl (impl of DaoService interface)">
<property name="dao" ref="daoImpl"/>
</bean>

and then you can @Inject DaoService daoService;

But using the ApplicationContext service isn't that bad. Regardless of the
true lore of decoupling... it is VERY unlikely that you change the DI
container or the bean id's in any non-trivial application.


2009/5/24 moonlee <mo...@hotmail.com>

>
> Hi, Otho.
> Do you have some solutions for then problem.
> I get the exceptions now, and I don't want to use the ApplicationContext
> Service
> to get bean by name.
>
>
> Otho wrote:
> >
> > Since Spring beans are not exposed anymore as services I get exceptions
> > like
> >
> >
> > Error obtaining injected value for field org.example.user.Login.dao:
> > Spring
> > context contains 2 beans assignable to type org.example.dao.Dao: dao,
> > daoImpl.
> >
> > I couldn't find anything in the docs about injecting by name. And the
> > proposes workaround for non singletons with injecting ApplicationContext
> > and
> > then get the bean by name/id  works, but is a bit awkward in cases like
> > this.
> >
> > Is there some other possibility? If not, is support for injection by name
> > planned in the future without the 5.0 compatibility mode?
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/T-5.1-How-to-inject-Spring-beans-by-name--tp22934788p23690308.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: T 5.1 How to inject Spring beans by name?

Posted by moonlee <mo...@hotmail.com>.
Hi, Otho.
Do you have some solutions for then problem.
I get the exceptions now, and I don't want to use the ApplicationContext
Service
to get bean by name.


Otho wrote:
> 
> Since Spring beans are not exposed anymore as services I get exceptions
> like
> 
> 
> Error obtaining injected value for field org.example.user.Login.dao:
> Spring
> context contains 2 beans assignable to type org.example.dao.Dao: dao,
> daoImpl.
> 
> I couldn't find anything in the docs about injecting by name. And the
> proposes workaround for non singletons with injecting ApplicationContext
> and
> then get the bean by name/id  works, but is a bit awkward in cases like
> this.
> 
> Is there some other possibility? If not, is support for injection by name
> planned in the future without the 5.0 compatibility mode?
> 
> 

-- 
View this message in context: http://www.nabble.com/T-5.1-How-to-inject-Spring-beans-by-name--tp22934788p23690308.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T 5.1 How to inject Spring beans by name?

Posted by Otho <ta...@googlemail.com>.
Thx for the hint. I was lazy and wanted to scrap the added indirection of a
separare service layer for a small exploration app.

But the problem occurs in more places than only the transaction proxy
hiding.

In my latest project I use a couple of spring configured activemq/jms queues
which of course all resolve to Queue.class. I use the ApplicationContext
service for now, but it really seems odd to me, why there is no inbuilt
facility by which I can reference a Spring bean by name.

Spring itself utilizes the JSR compatible javax.annotation.Resource for
this.
Like in

 @Autowired
 @Resource(name = "mailQueue")
 private Queue mailQueue = null;

 @Autowired
 @Resource(name = "feedQueue")
 private Queue feedQueue = null;

Maybe that could be a way for Tapestry 5.2 also?

Regards,
Otho


2009/5/31 Jonathan Barker <jo...@gmail.com>

> Otho,
>
> Just in case you are still looking for a solution...
>
>
> Change how you declare your beans.  For example:
>
>    <bean id="txProxyTemplate" abstract="true"
>
>
> class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
>        <property name="transactionManager">
>            <ref local="transactionManager" />
>        </property>
>        <property name="transactionAttributes">
>            <props>
>                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
>                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
>                <prop key="make*">PROPAGATION_REQUIRED</prop>
>                <prop key="delete*">PROPAGATION_REQUIRED</prop>
>                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
>            </props>
>        </property>
>    </bean>
>    <bean id="authorityDAO" parent="txProxyTemplate">
>        <property name="target">
>           <!-- This is how to hide the second implementer of the interface
> from Tapestry -->
>            <bean
>
> class="ca.itstrategic.fmp.portal.persistence.hibernate.AuthorityDAOImpl">
>                <property name="sessionFactory">
>                    <ref local="sessionFactory" />
>                </property>
>            </bean>
>        </property>
>    </bean>
>
>
> The key piece is how you specify the target of the
> TransactionProxyFactoryBean.  By using a <bean> declaration rather than a
> reference to a bean, Tapestry only sees the outer proxy bean.
>
>
> Jonathan
>
>
> On Tue, Apr 7, 2009 at 1:58 PM, Otho <ta...@googlemail.com> wrote:
>
> > Since Spring beans are not exposed anymore as services I get exceptions
> > like
> >
> >
> > Error obtaining injected value for field org.example.user.Login.dao:
> Spring
> > context contains 2 beans assignable to type org.example.dao.Dao: dao,
> > daoImpl.
> >
> > I couldn't find anything in the docs about injecting by name. And the
> > proposes workaround for non singletons with injecting ApplicationContext
> > and
> > then get the bean by name/id  works, but is a bit awkward in cases like
> > this.
> >
> > Is there some other possibility? If not, is support for injection by name
> > planned in the future without the 5.0 compatibility mode?
> >
>
>
>
> --
> Jonathan Barker
> ITStrategic
>

Re: T 5.1 How to inject Spring beans by name?

Posted by Jonathan Barker <jo...@gmail.com>.
Otho,

Just in case you are still looking for a solution...


Change how you declare your beans.  For example:

    <bean id="txProxyTemplate" abstract="true"

class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
            <ref local="transactionManager" />
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="make*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>
    <bean id="authorityDAO" parent="txProxyTemplate">
        <property name="target">
           <!-- This is how to hide the second implementer of the interface
from Tapestry -->
            <bean

class="ca.itstrategic.fmp.portal.persistence.hibernate.AuthorityDAOImpl">
                <property name="sessionFactory">
                    <ref local="sessionFactory" />
                </property>
            </bean>
        </property>
    </bean>


The key piece is how you specify the target of the
TransactionProxyFactoryBean.  By using a <bean> declaration rather than a
reference to a bean, Tapestry only sees the outer proxy bean.


Jonathan


On Tue, Apr 7, 2009 at 1:58 PM, Otho <ta...@googlemail.com> wrote:

> Since Spring beans are not exposed anymore as services I get exceptions
> like
>
>
> Error obtaining injected value for field org.example.user.Login.dao: Spring
> context contains 2 beans assignable to type org.example.dao.Dao: dao,
> daoImpl.
>
> I couldn't find anything in the docs about injecting by name. And the
> proposes workaround for non singletons with injecting ApplicationContext
> and
> then get the bean by name/id  works, but is a bit awkward in cases like
> this.
>
> Is there some other possibility? If not, is support for injection by name
> planned in the future without the 5.0 compatibility mode?
>



-- 
Jonathan Barker
ITStrategic