SSM+shiro 在realm中出现注解注入service失败

来源:互联网 发布:阿里云备案幕布剪裁 编辑:程序博客网 时间:2024/06/08 21:29

错误重现


Error creating bean with name 'shiroFilter' defined in class path resource [applicationContext.xml]:BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor#0' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'securityManager' while setting bean property 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'securityManager' defined in class path resource [applicationContext.xml]: Cannot resolve reference to
bean 'jdbcRealm' while setting bean property 'realms' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'jdbcRealm': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field:
com.test.shiro.service.UserService com.test.shiro.realms.ShiroRealm.userService;
nested exception is java.lang.IllegalArgumentException: Can not set com.test.shiro.service.UserService 
field com.test.shiro.realms.ShiroRealm.userService to com.sun.proxy.$Proxy18



反正就是一大堆、重点就是realm注入不了service这样就不能从数据库中读取到用户的信息


大概的原因 应该就是SPring在创建代理类的时候不能生成

因为我的service直接用实现的方式而不是用接口实现这样会出现代理不能扫描注入。

接下来就是改变

写一个接口

public interface UserServiceInterface {List<User> getall();User findByUsername(String username);User selectByPrimaryKey(int id);}


实现他

@Servicepublic class UserService implements UserServiceInterface{@AutowiredUserMapper userMapper;/** * 查询所有员工 *  * @return */public List<User> getall() {return userMapper.selectByExample(null);}


后面省略。

然后在realm中在使用


@Repositorypublic class ShiroRealm extends AuthorizingRealm {@AutowiredUserServiceInterface userService;User user;


搞定了!!!

原创粉丝点击