我正在开发一个spring项目,我正在尝试应用sprin安全性。问题是当我在contextConfigurationLocation参数中添加spring-security.xml由于某种原因我无法弄清楚服务bean是不是自动连接在控制器,我得到
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.ibios.services.ArticleCategoryService] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:952)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:821)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4149)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4458)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:583)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
spring security xml文件是:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<debug/>
<http pattern="/resources" security="none" />
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/login" access="permitAll"/>
<intercept-url pattern="/logout" access="permitAll"/>
<intercept-url pattern="/denied" access="hasRole('ROLE_USER')"/>
<intercept-url pattern="/" access="hasRole('ROLE_ADMIN')"/>
<intercept-url pattern="/user" access="hasRole('ROLE_USER')"/>
<intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')"/>
<form-login login-page="/login"
authentication-failure-url="/login/failure"
default-target-url="/"/>
<access-denied-handler error-page="/denied"/>
<logout invalidate-session="true"
logout-success-url="/logout/success"
logout-url="/logout"/>
</http>
<authentication-manager>
<authentication-provider user-service-ref="customUserDetailsService">
</authentication-provider>
</authentication-manager>
<beans:bean id="customUserDetailsService" class="com.ibios.services.impl.CustomUserDetailService"/>
root-context xml是:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
<bean id="jdbcPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:project.properties" />
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}"/>
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath:/META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="entityManager" />
<property name="dataSource" ref="datasource" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="terminologyService" class="com.ibios.services.impl.TerminologyServiceImpl" />
<bean id="articleCategoryService" class="com.ibios.services.impl.ArticleCategoryServiceImpl" />
<bean id="exerciseCategoryService" class="com.ibios.services.impl.ExerciseCategoryServiceImpl"/>
<bean id="exerciseService" class="com.ibios.services.impl.ExerciseServiceImpl"/>
<bean id="articleService" class="com.ibios.services.impl.ArticleServiceImpl"/>
<bean id="customUserDetailsService" class="com.ibios.services.impl.CustomUserDetailService"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="com.ibios.repositories" />
<context:component-scan base-package="com.ibios.web" />
</beans>
articleCategory服务impl类是:
public class ArticleCategoryServiceImpl implements ArticleCategoryService{
@Resource
ArticleCategoryRepository articleCategoryRepository;
@Override
public void create(ArticleCategory articleCategory) {
articleCategoryRepository.saveAndFlush(articleCategory);
}
@Override
public void update(ArticleCategory articleCategory) {
articleCategoryRepository.saveAndFlush(articleCategory);
}
@Override
public void delete(ArticleCategory articleCategory) {
articleCategoryRepository.delete(articleCategory);
}
@Transactional
@Override
public List<ArticleCategory> findAll() {
List<ArticleCategory> articleCategories = articleCategoryRepository.findAll();
return articleCategories;
}
@Override
public Page<ArticleCategory> findAll(Pageable pageable) {
PageRequest page1 = new PageRequest(pageable.getPageNumber(),
pageable.getPageSize(), Direction.DESC, "id");
Page<ArticleCategory> categories = articleCategoryRepository.findAll(page1);
return categories;
}
@Override
public ArticleCategory getById(Integer id) {
ArticleCategory articleCategory = articleCategoryRepository.findOne(id);
return articleCategory;
}
@Override
public List<ArticleCategory> findLatest() {
List<ArticleCategory> latest=articleCategoryRepository.findLatest();
return latest;
}
}
,控制器类是
@Controller
public class AddArticleCategoryController {
@Autowired
private ArticleCategoryService articleCategoryService;
@RequestMapping(value = "/addArticleCategory", method = RequestMethod.POST)
public String addArticleCategory(@ModelAttribute("articleCategory") ArticleCategory articleCategory, BindingResult result) {
articleCategory.setCreated(Calendar.getInstance());
articleCategoryService.create(articleCategory);
return "addArticleCategory";
}
@RequestMapping(value = "/addArticleCategory", method = RequestMethod.GET)
public ModelAndView showrticleCategory(@ModelAttribute ArticleCategory articleCategory, BindingResult result) {
return new ModelAndView("addArticleCategory", "articleCategory", new ArticleCategory());
}
@RequestMapping(value = "/showArticleCategories", method = RequestMethod.GET)
public String showArticleCategories(Model model, @PageableDefaults(pageNumber = 0, value = 15) Pageable pageable) {
Page<ArticleCategory> page = articleCategoryService.findAll(pageable);
model.addAttribute("page", page);
return "showArticleCategories";
}
@RequestMapping(value = "/editArticleCategory", method = RequestMethod.GET)
public String editArticleCategory(@RequestParam("id") String id, Model model) {
ArticleCategory articleCategory = articleCategoryService.getById(Integer.parseInt(id));
model.addAttribute("articleCategory", articleCategory);
return "editArticleCategory";
}
@RequestMapping(value = "/editArticleCategory", method = RequestMethod.POST)
public String editArticleCategorires(@ModelAttribute("articleCategory") ArticleCategory articleCategory, BindingResult result) {
System.out.println("CREATED:" + (String) result.getFieldValue("created"));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try {
Date createdDate = formatter.parse((String) result.getFieldValue("created"));
Calendar created = Calendar.getInstance();
created.setTime(createdDate);
articleCategory.setCreated(created);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
articleCategoryService.update(articleCategory);
return "showArticleCategories";
}
public ArticleCategoryService getArticleCategoryService() {
return articleCategoryService;
}
public void setArticleCategoryService(ArticleCategoryService articleCategoryService) {
this.articleCategoryService = articleCategoryService;
}
}
spring security的web.xml部分是:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
<param-value>/WEB-INF/spring/spring-security.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
到目前为止,我尝试使用ArticleCategoryService接口而不是实现类,我在事务管理器中将proxy-target-class设置为“true”,但没有结果。
答案 0 :(得分:2)
尝试将@Service
注释添加到ArticleCategoryServiceImpl
从
更改您的web.xml<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
<param-value>/WEB-INF/spring/spring-security.xml</param-value>
</context-param>
要:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/spring-security.xml</param-value>
</context-param>