java吧 关注:1,184,932贴子:12,586,963
  • 0回复贴,共1

spring加入shiro配置,@Autowired注解失效,求解!

只看楼主收藏回复

#Java Bug未解之谜#很头疼,Exception 和 代码如下:
Exception
javax.servlet.ServletException: Servlet.init() for servlet ssm threw exceptionorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Thread.java:745)
Root Cause
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sr.service.ShiroService com.sr.controller.ShiroController.shiroService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sr.service.ShiroService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Root Cause
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sr.service.ShiroService com.sr.controller.ShiroController.shiroService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sr.service.ShiroService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 1、配置 shiro 的shiroFilter -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<!-- Spring 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>ssm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssm</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-context.xml配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
//这里除了Controller不扫描其他注解都扫描
//在spring-mvc.xml中配置了context:component-scan 只扫描Controller
<beans>
<context:component-scan base-package="com.sr">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
/**
* 这里开始配置shiro
* 这里的代码可以正常使用,但是spring 的 @Autowired
*/
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager"/>
<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
<property name="authenticator" ref="authenticator"/>
<property name="realms">
<list>
<ref bean="jdbcRealm"/>
<ref bean="jdbcRealmSec"/>
</list>
</property>
</bean>
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean>
<!-- 多Realm情况下,将Realm添加到容器中 -->
<bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
<property name="authenticationStrategy">
<bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"/>
</property>
</bean>
<bean id="jdbcRealm" class="com.sr.shiro.realms.ShiroRealm">
<!-- 配置MD5加密属性 -->
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 指定加密算法 -->
<property name="hashAlgorithmName" value="MD5"></property>
<!-- 指定加密次数 -->
<property name="hashIterations" value="1024"></property>
</bean>
</property>
</bean>
<bean id="jdbcRealmSec" class="com.sr.shiro.realms.ShiroRealmSec">
<!-- 配置MD5加密属性 -->
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 指定加密算法 -->
<property name="hashAlgorithmName" value="SHA1"></property>
<!-- 指定加密次数 -->
<property name="hashIterations" value="1024"></property>
</bean>
</property>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
<property name="securityManager" ref="securityManager"/>
</bean>
/**
* 6.配置ShiroFilter.
* 6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的<filter-name> 一致
*/
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/jsp/shiro_login.jsp"/>
<property name="successUrl" value="/jsp/shiro_success.jsp"/>
<property name="unauthorizedUrl" value="/jsp/shiro_unauthorized.jsp"/>
<property name="filterChainDefinitions">
<value>
/jsp/shiro_login.jsp = anon
/shiro/login = anon
/shiro/shiro_login = anon
/shiro/logout = logout
/jsp/user.jsp = roles[user]
/jsp/admin.jsp = roles[admin]
# allow WebStart to pull the jars for the swing app:
/*.jar = anon
# everything else requires authentication:
/** = authc
</value>
</property>
</bean>
</beans>
shiroController.java
@Controller
@RequestMapping("shiro")
public class ShiroController
@Autowired
private ShiroService shiroService;
@RequestMapping("shiro_login")
public String print(){
shiroService.Date();
System.out.println("");
return "success";
}
}
shiroService.java
@Service
public class ShiroService {
@RequiresRoles("admin")
public void Date(){
System.out.println(new Date());
}
}
求大神来救个火,万分感谢!


IP属地:江苏1楼2018-04-21 21:31回复