SSM+shiro 在realm中出現註解注入service失敗
阿新 • • 發佈:2019-02-07
錯誤重現
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);
}
實現他
@Service
public class UserService implements UserServiceInterface{
@Autowired
UserMapper userMapper;
/**
* 查詢所有員工
*
* @return
*/
public List<User> getall() {
return userMapper.selectByExample(null);
}
後面省略。
然後在realm中在使用
@Repository
public class ShiroRealm extends AuthorizingRealm {
@Autowired
UserServiceInterface userService;
User user;
搞定了!!!