2012-12-18 45 views
0

我一直在努力使所有这些工作从现在开始,不知道该怎么做。我相信,通过我在这里,使拍摄对象的每一个岗位去,并通过教程douzens去......春天MVC +休眠4 +春季安全

这里是我在这个时刻,具有消息:

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: {} 

我已经能够在过去解决这个错误,但只有一个新的“没有找到当前线程的会话”。当没有这个时,我在我的spring-security.xml文件中无法识别我的Assembler和UserDetailsS​​erviceImpl bean ...

我不认为问题来自我的代码,我只是无法正确设置我的配置文件,我可能在这里丢失了一些东西。

下面是配置文件:

的web.xml:

<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> 

的applicationContext.xml:

<?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> 

MVC-调度-servlet.xml中:

<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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/security的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> 

FruitController:

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); 

}

HibernateFruitDAO

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); 
    } 
} 

回答

2

一目了然,似乎你从不了解春天的ApplicationContexts如何结合在一起,使一个共同的问题的痛苦一个Web应用程序。看我其他的答案完全相同的问题,看它是否清除事情:

Declaring Spring Bean in Parent Context vs Child Context

您也可以通过这个答案上类似的话题,可链接到我前面提到的答案,以及一个开明其他:

Spring XML file configuration hierarchy help/explanation

几个简短的提示,让你在正确的方向前进......

按照惯例,春节的ContextLoaderListener负载从豆类WEB-INF/applicationContext.xml来创建根应用程序上下文。当你重写默认值时,就像你在做的那样,该文件不再被加载。

提示#1:坚持使用传统的行为。它会让你的生活变得更简单。

同样按照惯例,启动Spring DispatcherServlet从WEB-INF/<servlet name>-context.xml加载bean来创建用于配置调度程序servlet的上下文。此上下文成为根上下文的子节点。

提示#2:看到提示#1

所以你看,你目前过度配置的东西。阅读链接的答案和其中链接的参考资料。学会与Spring合作而不是反对它。

+0

谢谢你的启发答案,那对我帮助很大。我想我已经清理了大部分配置文件并理解它的工作原理。我有一个尚未解决的问题与弹簧的security.xml这个时候:“没有名为‘UserDetailsS​​ervice的’豆的定义”。 userDetailsS​​ervice bean未找到。我在我的spring-security.xml(Assembler和UserDetailsS​​erviceImpl)中评论了两个bean声明。我删除它们的原因是它们应该已经被根上下文扫描了。 – dukable

+0

这个答案和提供的链接帮助我获得了有意义的干净配置文件。我也更好地理解它应该如何工作的方式以及Spring应该如何使用的方式。非常有用的答案。 – dukable

0

在web.xml文件, applicationContext.xml永远不会被加载。你应该把它放在context-param的位置。放mvc-dispatcher-servlet.xml位置(含控制器相关Bean)作为INIT-参数有关的DispatcherServlet来代替:

<init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
     </init-param> 
0

我想你一定要使用这个在DaoImpl获取会话:

@Autowired 
private SessionFactory sessionFactory;