You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by NabbleReallySucks <bi...@yahoo.com> on 2013/03/14 05:13:29 UTC

Autowired just doesn't ever seem to work in my Shiro implementations

This had cost me a few days last week, but I had a custom Realm where I would
@Autowired in a Service class that I need to query data with. Yes a Service
that was calling down to a Repository. I even tried to @Autowired the
Repository. Now the Repository is a Spring Data JPA Repository. I inject
this Service and Repository in many other classes and @Autowired works fine.
It injects it as needed.

However, it doesn't in the custom Realm. I get the typical exception from
Spring saying it couldn't find a candidate for autowiring. However, there is
a BeanDefinition for the Repository and Service in the ApplicationContext,
so those beans to inject are being created. And the Realm is also defined as
a Spring <bean>

<bean id="blahRealm"
class="com.blah.account.security.shiro.realm.BlahRealm">
        <property name="credentialsMatcher" ref="hashMatcher"/>
    </bean>

But it didn't work. So I have to do a hack and @Autowired the
ApplicationContext into the Realm and then getBean() to get it into the
Realm like the following code.



public class BlahRealm extends AuthorizingRealm {

  @Autowired
  ApplicationContext context;

  private AccountService accountService;

  @PostConstruct
  public void why() {
    System.out.println("In Post");
  }

  private AccountService getAccountService() {
    if (this.accountService == null) {
      this.accountService = context.getBean(AccountService.class);
    }
    return this.accountService;
  }

So, my question is why doesn't @Autowired work. I assume it is a timing
thing.

Thanks

Mark



--
View this message in context: http://shiro-user.582556.n2.nabble.com/Autowired-just-doesn-t-ever-seem-to-work-in-my-Shiro-implementations-tp7578384.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: Autowired just doesn't ever seem to work in my Shiro implementations

Posted by NabbleSometimesSucks <bi...@yahoo.com>.
jase wrote
> Could be the same issue i had. And others. Boils down to processing order
> for a beanpostprocessor.   
> http://shiro-user.582556.n2.nabble.com/ShiroFilterFactoryBean-and-a-spring-data-mongodb-realm-td7578141.html

Yep, that looks exactly the same. That one was with Spring Data Mongo, mine
Spring Data JPA.

Mark



--
View this message in context: http://shiro-user.582556.n2.nabble.com/Autowired-just-doesn-t-ever-seem-to-work-in-my-Shiro-implementations-tp7578384p7578405.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: Autowired just doesn't ever seem to work in my Shiro implementations

Posted by NabbleSometimesSucks <bi...@yahoo.com>.
Yeah, I know it is really bizarre. And both Shiro beans and these beans are
all in the ContextLoaderListener parent "middle-tier" ApplicationContext,
not in the Web child ApplicationContext. A typical mistake people make is
the other way around.

I even created a BeanFactoryPostProcessor and BeanPostProcessor to "debug"
what was happening underneath the covers with all the beans and it showed
that the beans were there.

Mark



--
View this message in context: http://shiro-user.582556.n2.nabble.com/Autowired-just-doesn-t-ever-seem-to-work-in-my-Shiro-implementations-tp7578384p7578394.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: Autowired just doesn't ever seem to work in my Shiro implementations

Posted by Les Hazlewood <lh...@apache.org>.
Wow, I have no idea why this would occur - @Autowired clearly works
for your ApplicationContext injection, but not another bean?  That
doesn't make any sense to me at all.

I have an @Autowired Realm implementation in my Spring app and its
@Autowired dependencies are injected just fine.  I'm not sure why
you're seeing this.

Les

On Wed, Mar 13, 2013 at 9:13 PM, NabbleReallySucks
<bi...@yahoo.com> wrote:
> This had cost me a few days last week, but I had a custom Realm where I would
> @Autowired in a Service class that I need to query data with. Yes a Service
> that was calling down to a Repository. I even tried to @Autowired the
> Repository. Now the Repository is a Spring Data JPA Repository. I inject
> this Service and Repository in many other classes and @Autowired works fine.
> It injects it as needed.
>
> However, it doesn't in the custom Realm. I get the typical exception from
> Spring saying it couldn't find a candidate for autowiring. However, there is
> a BeanDefinition for the Repository and Service in the ApplicationContext,
> so those beans to inject are being created. And the Realm is also defined as
> a Spring <bean>
>
> <bean id="blahRealm"
> class="com.blah.account.security.shiro.realm.BlahRealm">
>         <property name="credentialsMatcher" ref="hashMatcher"/>
>     </bean>
>
> But it didn't work. So I have to do a hack and @Autowired the
> ApplicationContext into the Realm and then getBean() to get it into the
> Realm like the following code.
>
>
>
> public class BlahRealm extends AuthorizingRealm {
>
>   @Autowired
>   ApplicationContext context;
>
>   private AccountService accountService;
>
>   @PostConstruct
>   public void why() {
>     System.out.println("In Post");
>   }
>
>   private AccountService getAccountService() {
>     if (this.accountService == null) {
>       this.accountService = context.getBean(AccountService.class);
>     }
>     return this.accountService;
>   }
>
> So, my question is why doesn't @Autowired work. I assume it is a timing
> thing.
>
> Thanks
>
> Mark
>
>
>
> --
> View this message in context: http://shiro-user.582556.n2.nabble.com/Autowired-just-doesn-t-ever-seem-to-work-in-my-Shiro-implementations-tp7578384.html
> Sent from the Shiro User mailing list archive at Nabble.com.