Announcement Announcement Module
Collapse
No announcement yet.
No bean named ... is defined Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No bean named ... is defined

    Hello guys, i was following this tutorial http://stackoverflow.com/questions/2...with-hibernate, and I'm stuck on this error

    Code:
    2011-03-12 23:42:43,131 [main] ERROR (ContextLoader.java:220) ? Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot create inner bean '(inner bean)' of type [org.springframework.security.config.authentication.AuthenticationManagerFactoryBean] while setting bean property 'parent'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' while setting bean property 'providers' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean 'userDetailsService' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userDetailsService' is defined
    userDetailsService

    Code:
    package cz.xkadle21.dip.service.impl;
    
    ...
    
    /**
     * 
     * 
     * @author CVnek
     * 
     */
    @Service("userDetailsService")
    public class DiUserContextSecurityService implements UserDetailsService,
    		IDiUserContextSecurityService {
    
    	@Autowired
    	private IDiUsersDAO usersDao;
    
    	@Autowired
    	private IDiSecurityUserFactory securityUserFactory;
    
    	public DiUserContextSecurityService() {
    		super();
    	}
    
    	// @Autowired
    	// public DiUserContextSecurityService(IDiUsersDAO usersDAO,
    	// IDiSecurityUserFactory securityUserFactory) {
    	// super();
    	// this.usersDao = usersDAO;
    	// this.securityUserFactory = securityUserFactory;
    	// }
    
    	public Authentication getCurrentUser() {
    		return SecurityContextHolder.getContext().getAuthentication();
    	}
    
    	@Transactional(readOnly = true)
    	public User loadUserByUsername(String username) {
    
    		DiUser user = usersDao.findUserByUsername(username);
    
    		UserDetails userDetails = null;
    
    		if (user != null) {
    
    			throw new UsernameNotFoundException("user not found");
    		}
    
    		return securityUserFactory.buildSecurityUserFromEntity(user);
    	}
    
    }
    apllicationContext-security.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <b:beans xmlns="http://www.springframework.org/schema/security"
    	xmlns:b="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
    
    
    	<http use-expressions="true">
    		<intercept-url pattern="/" access="permitAll" />
    		<intercept-url pattern="/admin/**" access="isAuthenticated()" />
    		<intercept-url pattern="/js/**" filters="none" />
    		<intercept-url pattern="/css/**" filters="none" />
    		<intercept-url pattern="/images/**" filters="none" />
    
    		<form-login />
    		<logout />
    	</http>
    
    
    	<security:authentication-manager>
    		<security:authentication-provider
    			user-service-ref="userDetailsService" />
    	</security:authentication-manager>
    
    </b:beans>
    spring-servlet.xml
    Code:
    <?xml  version="1.0" encoding="UTF-8"?>
    
    ... only 10 000 Chars :-)
    
    	<context:annotation-config />
    	<context:component-scan base-package="cz.xkadle21.dip.controller" />
    	<context:component-scan base-package="cz.xkadle21.dip.controller.impl" />
    	<context:component-scan base-package="cz.xkadle21.dip.dao" />
    	<context:component-scan base-package="cz.xkadle21.dip.dao.impl" />
    	<context:component-scan base-package="cz.xkadle21.dip.service" />
    	<context:component-scan base-package="cz.xkadle21.dip.service.impl" />
    	<context:component-scan base-package="cz.xkadle21.dip.validator" />
    	<context:component-scan base-package="cz.xkadle21.dip.validation.impl" />
    	<context:component-scan base-package="cz.xkadle21.dip.factory" />
    	<context:component-scan base-package="cz.xkadle21.dip.factory.impl" />
    
    <!--	<bean name="userDetailsService" id="userDetailsService" -->
    <!--		class="cz.xkadle21.dip.service.impl.DiUserContextSecurityService">-->
    <!--	</bean>-->
    
    
    	<bean id="viewResolver"
    		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="viewClass">
    			<value>
    				org.springframework.web.servlet.view.tiles2.TilesView
            </value>
    		</property>
    	</bean>
    	<bean id="tilesConfigurer"
    		class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    		<property name="definitions">
    			<list>
    				<value>/WEB-INF/tiles.xml</value>
    			</list>
    		</property>
    	</bean>
    
    
    	<!-- ************ interceptors ************ -->
    	<bean
    		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
    		p:interceptors-ref="menuInterceptor" />
    
    	<bean id="menuInterceptor" class="cz.xkadle21.dip.interceptor.DiMenuInterceptor" />
    	<!-- ************************************** -->
    
    
    	<bean
    		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    
    	<!-- ************ properties ************ -->
    	<bean id="messageSource"
    		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    		<property name="basename" value="classpath:messages" />
    		<property name="defaultEncoding" value="UTF-8" />
    		<!-- ************************************ -->
    
    	</bean>
    	<bean id="propertyConfigurer"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    		p:location="/WEB-INF/jdbc.properties" />
    
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close" p:driverClassName="${hibernate.connection.driver_class}"
    		p:url="${hibernate.connection.url}" p:username="${hibernate.connection.username}"
    		p:password="${hibernate.connection.password}" />
    
    	<!-- ************** libraries, js, css ************** -->
    	<mvc:default-servlet-handler />
    
    	<mvc:resources mapping="/css/**" location="/css/" />
    	<mvc:resources mapping="/images/**" location="/images/" />
    	<mvc:resources mapping="/js/**" location="/js/" />
    	<!-- ************************************************ -->
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="configLocation">
    			<value>classpath:hibernate.cfg.xml</value>
    		</property>
    
    
    		<property name="configurationClass">
    			<value>org.hibernate.cfg.AnnotationConfiguration</value>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.show_sql">true</prop>
    <!--				<prop key="hibernate.hbm2ddl.auto">update</prop>-->
    			</props>
    		</property>
    	</bean>
    
    	<tx:jta-transaction-manager />
    
    	<tx:annotation-driven />
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
    
    </beans>
    When i put into apllicationCiontext-security.xml

    Code:
    	<b:bean name="userDetailsService"
    		class="cz.xkadle21.dip.service.impl.DiUserContextSecurityService">
    	</b:bean>

    its working elsewhere "normally", but i got NullPointerIxception on usersDao, when I'am calling loadUserByUsername function. Fields are not injected.

    or with

    Code:
    	<b:bean name="userDetailsService"
    		class="cz.xkadle21.dip.service.impl.DiUserContextSecurityService" >
    		<b:constructor-arg ref="userDAO" />
    		<b:constructor-arg ref="securityUserFactory" />
    	</b:bean>
    
            <b:bean id="userDAO"
                    class="cz.xkadle21.dip.dao.impl.DiUserDAO"/>
            <b:bean id="securityUserFactory"
                    class="cz.xkadle21.dip.factory.impl.DiSecurityUserFactory"/>
    than i got NullPointer on sessionFactory ...

    Some russian guy have exactly same problem http://pda.javatalks.ru/sutra103362....449a10ae38edfd
    , but component scan i have.

    In advance thank for help. I'am working on this issue almost all day.
    Last edited by rizler; Mar 12th, 2011, 07:21 PM.

  • #2
    If you are using component-scan in your servlet config file, this is a different ApplicationContext from the Spr Sec one (which I assume you are loading explicitly in web.xml context config loader or something similar - you didn't post the web.xml, so I can't be sure). In this case, the Spr Sec context is being created first, and can't "see" beans initialized in the WebApplicationContext created by the -servlet.xml file.

    The fix is easy - keep only those beans that are related to web stuff in the -servlet.xml file, and put all the rest in another XML config that is initialized at the same time as the Spr Sec config file.

    Comment


    • #3
      The fix is easy - keep only those beans that are related to web stuff in the -servlet.xml file, and put all the rest in another XML config that is initialized at the same time as the Spr Sec config file.
      Well, now is this more spring than spring security problem.

      When i move beans not related to web stuff to another config file applicationContext-common-business.xml except sessionFactory, i got null pointer on sessionFactory. So i move sessionFactory too and i got null pointer on some of DAOs

      Code:
      13.3.2011 11:31:19 org.apache.catalina.core.StandardWrapperValve invoke
      SEVERE: Servlet.service() for servlet spring threw exception
      org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
      	at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
      	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
      	at cz.xkadle21.dip.dao.impl.DiCategoryDAO.findAllTree(DiCategoryDAO.java:30)
      	at cz.xkadle21.dip.service.impl.DiCategoryService.getCategoryInTree(DiCategoryService.java:55)
      	at cz.xkadle21.dip.interceptor.DiMenuInterceptor.postHandle(DiMenuInterceptor.java:35)
      It is also necessary to create than defitinition all DAOs and Services in applicationContext-common-business.xml too?

      applicationContext-common-business.xml

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      	xmlns:tx="http://www.springframework.org/schema/tx" 
      	xmlns:p="http://www.springframework.org/schema/p"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
      
      
      	 <bean name="userDetailsService"
      		class="cz.xkadle21.dip.service.impl.DiUserContextSecurityService" >
      		<constructor-arg ref="userDAO" />
      		<constructor-arg ref="securityUserFactory" />
      	 </bean>
      
               <bean id="userDAO"
                       class="cz.xkadle21.dip.dao.impl.DiUserDAO"/>
                                   
               <bean id="securityUserFactory"
                       class="cz.xkadle21.dip.factory.impl.DiSecurityUserFactory"/>                   
      
          
      	<bean id="propertyConfigurer"
      		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      		p:location="/WEB-INF/jdbc.properties" />
      
      	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      		destroy-method="close" p:driverClassName="${hibernate.connection.driver_class}"
      		p:url="${hibernate.connection.url}" p:username="${hibernate.connection.username}"
      		p:password="${hibernate.connection.password}" />
      
      	<bean id="sessionFactory"
      		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      		<property name="dataSource" ref="dataSource" />
      		<property name="configLocation">
      			<value>classpath:hibernate.cfg.xml</value>
      		</property>
      		<property name="configurationClass">
      			<value>org.hibernate.cfg.AnnotationConfiguration</value>
      		</property>
      		<property name="hibernateProperties">
      			<props>
      				<prop key="hibernate.show_sql">true</prop>
      				<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
      			</props>
      		</property>
      	</bean>
      
      	<tx:jta-transaction-manager />
      
      	<tx:annotation-driven />
      
      	<bean id="transactionManager"
      		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      		<property name="sessionFactory" ref="sessionFactory" />
      	</bean>
      
                
      
      </beans>
      web.xml

      Code:
      	<context-param>
      		<param-name>contextConfigLocation</param-name>
      		<param-value>
      			/WEB-INF/applicationContext-common-business.xml 
      			/WEB-INF/applicationContext-security.xml
      		</param-value>
      	</context-param>
      	<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>
      	
      
      	<listener>
      		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      	</listener>

      Comment

      Working...
      X