Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加spring安全性后自动布线不起作用_Spring_Spring Mvc_Spring Security - Fatal编程技术网

添加spring安全性后自动布线不起作用

添加spring安全性后自动布线不起作用,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我正在从事一个spring项目,我正在尝试应用sprin安全性。问题是,当我出于某种原因在contextConfigurationLocation参数中添加spring-security.xml时,我无法确定服务bean在控制器上不是自动连接的,我得到了 org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.ibios.services.ArticleCateg

我正在从事一个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安全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"/>

根上下文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>

类路径:/META-INF/persistence.xml
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;
    }
}
公共类ArticleCategoryService impl实现ArticleCategoryService{
@资源
Article CategoryRepository Article CategoryRepository;
@凌驾
公共无效创建(ArticleCategory ArticleCategory){
articleCategoryRepository.saveAndFlush(articleCategory);
}
@凌驾
公共无效更新(ArticleCategory ArticleCategory){
articleCategoryRepository.saveAndFlush(articleCategory);
}
@凌驾
公共无效删除(ArticleCategory ArticleCategory){
articleCategoryRepository.删除(articleCategory);
}
@交易的
@凌驾
公共列表findAll(){
List articleCategories=ArticleCategorityRepository.findAll();
退货类别;
}
@凌驾
公共页findAll(可分页){
PageRequest page1=新的PageRequest(pageable.getPageNumber(),
pageable.getPageSize(),Direction.DESC,“id”);
页面类别=articleCategoryRepository.findAll(第1页);
退货类别;
}
@凌驾
public ArticleCategory getById(整数id){
ArticleCategory=articleCategoryRepository.findOne(id);
退货类别;
}
@凌驾
公共列表findLatest(){
List latest=articleCategoryRepository.findLatest();
返回最新版本;
}
}
控制器类是

   @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;
    }
}
@控制器
公共类AddArticleCategoryController{
@自动连线
私人物品类别服务物品类别服务;
@RequestMapping(value=“/addArticleCategory”,method=RequestMethod.POST)
公共字符串addArticleCategory(@ModelAttribute(“articleCategory”)articleCategory,BindingResult){
articleCategory.setCreated(Calendar.getInstance());
articleCategoryService.create(articleCategory);
返回“addArticleCategory”;
}
@RequestMapping(value=“/addArticleCategory”,method=RequestMethod.GET)
公共模型和视图显示RTicleCategory(@ModelAttribute articleContegory,BindingResult){
返回新模型和视图(“addArticleCategory”、“articleCategory”、new articleCategory());
}
@RequestMapping(value=“/showArticleCategories”,method=RequestMethod.GET)
公共字符串showArticleCategories(模型,@PageableDefaults(pageNumber=0,value=15)可分页可分页){
Page Page=articleCategoryService.findAll(可分页);
model.addAttribute(“页面”,第页);
返回“showArticleCategories”;
}
@RequestMapping(value=“/editArticleCategory”,method=RequestMethod.GET)
公共字符串editArticleCategory(@RequestParam(“id”)字符串id,模型){
ArticleCategory=articleCategoryService.getById(Integer.parseInt(id));
model.addAttribute(“articleCategory”,articleCategory);
返回“editArticleCategory”;
}
@RequestMapping(value=“/editArticleCategory”,method=RequestMethod.POST)
公共字符串EditArticleCategories(@ModelAttribute(“articleCategory”)articleCategory,BindingResult){
System.out.println(“创建:”+(字符串)result.getFieldValue(“创建”);
SimpleDataFormat格式化程序=新的SimpleDataFormat(“dd/MM/yyyy”);
试一试{
Date createdDate=formatter.parse((字符串)result.getFieldValue(“创建”);
Calendar created=Calendar.getInstance();
created.setTime(createdDate);
articleCategory.setCreated(已创建);
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
articleCategoryService.update(articleCategory);
返回“showArticleCategories”;
}
公共ArticleCategoryService getArticleCategoryService(){
返回articleCategoryService;
}
公共无效集合ArticleCategoryService(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> 

上下文配置位置
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/spring-security.xml
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*

到目前为止,我一直尝试使用ArticleCategoryService接口而不是实现类,我在事务管理器中将代理目标类设置为“true”,但都没有结果。

尝试将
@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>

上下文配置位置
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/spring-security.xml
致:


上下文配置位置
/WEB-INF/spring/root-context.xml,/WEB-INF/spring/spring-security.xml

感谢您回答Jens。尝试了。结果相同。@kafr如果创建了名为
articleCategoryService?
的bean,请清除日志文件中的任何消息。不幸的是,没有。仅此例外。我将尝试删除服务的bean声明,在组件扫描中添加服务包,并用@Compon标记服务impl类
<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>