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

错误重现


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;


搞定了!!!

猜你喜欢

转载自blog.csdn.net/u013354696/article/details/73656702
今日推荐