当前位置: 首页 > 知识库问答 >
问题:

Spring MVC+Hibernate 4+Spring Security性

楚嘉
2023-03-14

从几天以来,我一直在努力使所有这些工作,不知道该怎么做。我相信我读了每一篇关于这个主题的文章,读了很多教程...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fruitController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.controller.FruitController.setFruitManager(com.service.FruitManager); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.service.FruitManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

    <!-- Spring MVC -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/mvc-dispatcher-servlet.xml,
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

    <!-- Spring Security -->
    <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-app>
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:annotation-config/>
    <!-- Load everything except @Controllers -->
    <context:component-scan base-package="com">
        <context:exclude-filter expression="org.springframework.stereotype.Controller"
            type="annotation" />
    </context:component-scan>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="save*" />
            <tx:method name="*" read-only="false" />
        </tx:attributes>
    </tx:advice>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.dao.HibernateFruitDAO</value>
            </list>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.service</value>
                <value>com.controller</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/basename" />
        <property name="username" value="xxx" />
        <property name="password" value="yyy" />
    </bean>

</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:annotation-config />

    <context:property-placeholder location="classpath:hibernate.properties" />

    <!-- Load @Controllers only -->
    <context:component-scan base-package="com.controller"
        use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller"
            type="annotation" />
    </context:component-scan>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>mymessages</value>
            </list>
        </property>
    </bean>

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>

</beans>

spring-security.xml http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/security/spring-security-3.1.xsd“>

    <beans:bean id="userDetailsService" class="com.service.UserDetailsServiceImpl">
    </beans:bean>

    <beans:bean id="assembler" class="com.service.Assembler">
    </beans:bean>


    <http auto-config='true' use-expressions='true'>
        <intercept-url pattern="/login*" access="isAnonymous()" />
        <intercept-url pattern="/secure/**" access="hasRole('ROLE_Admin')" />
            <logout logout-success-url="/listing.htm" />
        <form-login login-page="/login.htm" login-processing-url="/j_spring_security_check"
            authentication-failure-url="/login_error.htm" default-target-url="/listing.htm"
            always-use-default-target="true" />
    </http>

    <beans:bean id="com.daoAuthenticationProvider"
        class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="userDetailsService" ref="userDetailsService" />
    </beans:bean>

    <beans:bean id="authenticationManager"
        class="org.springframework.security.authentication.ProviderManager">
        <beans:property name="providers">
            <beans:list>
                <beans:ref local="com.daoAuthenticationProvider" />
            </beans:list>
        </beans:property>
    </beans:bean>

    <authentication-manager>
        <authentication-provider user-service-ref="userDetailsService">
            <password-encoder hash="plaintext" />
        </authentication-provider>
    </authentication-manager>
</beans:beans>

水果控制器:

package com.controller;

@Controller
public class FruitController{

    protected final Log logger = LogFactory.getLog(getClass());


    private FruitManager fruitManager;

    @Autowired
    public void setFruitManager(FruitManager FruitManager) {
        this.fruitManager = fruitManager;
    }

    @RequestMapping(value = "/listing", method = RequestMethod.GET)
    public String getFruits(ModelMap model) {
        model.addAttribute("fruits", this.fruitManager.getFruits());
        return "listing";
    }
}

FruitDAO:公共接口FruitDAO{

public List<Fruit> getFruitList();

public List<Fruit> getFruitListByUserId(String userId);

public void saveFruit(Fruitprod);

public void updateFruit(Fruitprod);

public void deleteFruit(int id);

public Fruit getFruitById(int id);
package com.dao;

@Repository("fruitDao")
public class HibernateFruitDAO implements FruitDAO {


private SessionFactory sessionFactory;

        @Autowired
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }

    public List<Fruit> getList() {
        return (List<Fruit>) getSession().createCriteria ( Fruit.class ).list();
    }

    public List<Fruit> getFruitListByUserId(String userId) {
        return (List<Fruit>)sessionFactory.getCurrentSession().createCriteria("from Fruit where userId =?", userId).list();
    }

    public void saveFruit(Fruit fruit) {
        sessionFactory.getCurrentSession().save(fruit);
    }

    public void updateFruit(Fruit fruit) {
        sessionFactory.getCurrentSession().update(fruit);
    }

    public void deleteFruit(int id) {
        Fruit fruit = (Fruit) sessionFactory.getCurrentSession().load(Fruit.class, id);
        if (null != fruit) {
            sessionFactory.getCurrentSession().delete(fruit);
        }
    }

    public Fruit getFruitById(int id) {
        return (Fruit)sessionFactory.getCurrentSession().load(Fruit.class, id);
    }

    private Session getSession(){
            return sessionFactory.getCurrentSession();
        }
}

接口FruitManager:

package com.service;

import java.io.Serializable;
import java.util.List;

import com.domain.Fruit;


public interface FruitManager extends Serializable{

    public List<Fruit> getFruits();

    public List<Fruit> getFruitsByUserId(String userId);

    public void addFruit(Fruit fruit);

    public void removeFruit(int id);

    public Fruit getFruitById(int id);

    public void updateFruit(Fruit fruit);
}

FruitManager的实现:

package com.service;

@Repository("fruitManager")
@Transactional
public class SimpleFruitManager implements FruitManager {

    /**
     * 
     */
    private static final long serialVersionUID = ...;

    @Autowired
    private FruitDAO fruitDao;

    public List<Fruit> getFruits() {
        return fruitDao.getFruitList();
    }

    public List<Fruit> getFruitsByUserId(String userId){
        return fruitDao.getFruitListByUserId(userId);
    }

    public void setFruitDao(FruitDAO fruitDao) {
        this.fruitDao = fruitDao;
    }

    public void addFruit(Fruit fruit) {
        fruitDao.saveFruit(fruit);
    }

    public void removeFruit(int id) {
        fruitDao.deleteFruit(id);
    }

    public  getFruitById(int id) {
        return fruitDao.getFruitById(id);
    }

    public void updateFruit(Fruit fruit) {
        fruitDao.updateFruit(fruit);
    }
}

共有1个答案

周浩博
2023-03-14

乍一看,您似乎遇到了一个常见的问题,即不理解Spring ApplicationContexts如何组合在一起生成web应用程序。看看我对同样问题的另一个答案,看看它是否能把事情弄清楚:

在父上下文和子上下文中声明Spring Bean

你也可以从这个关于一个类似主题的答案中得到启发,它与我前面提到的答案以及另一个链接:

技巧2:参见技巧1

所以你看,你现在过度配置了东西。阅读链接的答案和其中链接的参考资料。学会和Spring一起工作,而不是反对它。

 类似资料:
  • 我的项目是在Windows-SpringIDE-TomcatV8上进行的。0用于windows,MySql5。该项目还涉及java7、Tomcat7。我所有的课都是自动注释的。问题是,当我在Centos上部署project时,我有java8、Tomcat8和一个异常。请提出一些解决问题的建议。 以下是linux上的例外情况: 组织。springframework。豆。工厂BeanCreationE

  • 我正在学习springsecurity(基于java的配置),我无法使注销正常工作。当我点击注销时,我看到URL更改为http://localhost:8080/logout并获取“HTTP 404-/logout”。登录功能工作正常(即使使用自定义登录表单),但问题是注销,我怀疑重定向的url“localhost:8080/logout”应该类似于“localhost:8808/springte

  • 本文向大家介绍JavaWeb开发之Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架,包括了JavaWeb开发之Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架的使用技巧和注意事项,需要的朋友参考一下 简单介绍一下,本框架的基本功能点

  • 主要内容:1.入门,2.设置用户名和密码1.入门 1.启动一个SpringBoot项目 2.导入SpringSecurity相关依赖 3.编写Controller TestController.java 用户是user 密码是刚刚的 2.设置用户名和密码 1.在配置文件中设置 2.在配置类中设置 3.自定义实现类 2.1 配置文件中设置 2.2 在配置类中设置 设置用户名为zZZ,密码为root 2.3 自定义实现类 配置类: 业务类:

  • 在WAR的情况下,它试图将请求转发到/error页面,并寻找它的处理程序方法(请参见底部的日志)。 最后我得到以下回应: 我该换什么才能得到401?

  • 1.导入jar包 web.xml spring-security.xml

  • 问题内容: 他们幸福地结婚了吗? 我正在使用最新版本的hibernate(4)和1.3版的joda-time hibernate支持,我也相信这是当前的最新版本。 使用批注时,一切似乎都正常(按预期方式创建了日期列): 一起使用这些版本是否存在任何已知问题? Update Well证明已创建列,但无法填充任何数据: 处理程序处理失败; 嵌套的异常是java.lang.AbstractMethodE

  • 我的web应用程序使用的是Spring3.1和hibernate4。在这里,我试图为eh缓存,但得到一些错误,这是我的配置,我已经使用:- dispatcher-servlet.xml ehcache.xml 这是依赖性:— 我得到以下错误:-- 严重:在org.springframework.beans.factory中上下文初始化失败。BeanCreationException:创建名为“or