Announcement Announcement Module
Collapse
No announcement yet.
cannot find non-autowire components Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • cannot find non-autowire components

    My application will not start up properly because the sessionFactory (non-autowired) is not visible to the autowired components, can any of you tell me why?

    I have a spring setup that loads multiple configuration files, and here's the web.xml that's supposed to do this...

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app 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"
        version="2.4">
    
        <servlet>  
            <servlet-name>gps</servlet-name>  
            <servlet-class>  
                org.springframework.web.servlet.DispatcherServlet  
            </servlet-class>  
    		<load-on-startup>1</load-on-startup>
        </servlet>  
        <servlet-mapping>  
            <servlet-name>gps</servlet-name>  
            <url-pattern>*.html</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/web-context.xml
    			/WEB_INF/service-context.xml
    			/WEB_INF/dao-context.xml
        	</param-value>
        	
        </context-param>
        
    </web-app>
    I tried to load a classpath param, but it didn't seem to read all the files.

    Anyway, the corresponding gps-servlet.xml file looks like this...

    Code:
    <?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"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
          
        <context:annotation-config/>
        
    	<context:component-scan base-package="com.djpteam.bid.web,com.djpteam.bid.service,com.djpteam.bid.dao"/>
    	      
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  
          
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
          
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <property name="prefix" value="/jsp/"/>  
            <property name="suffix" value=".jsp"/>  
        </bean>
        
    </beans>
    In the deployed web app, these are both in the top-level of the WEB-INF directory.

    Now, the issues start in the other context files. Basically, I have a controller that references a service, which, in turn references a dao. The controller is scanned in as an annoted controller and looks like this...

    Code:
    @Controller
    public class ShowLocalitiesController {
    	
    	static final Logger LOG = LoggerFactory.getLogger(ShowLocalitiesController.class);
    
    	private LocalityService localityService;
    	
    	@RequestMapping("/gps/showLocalities")
    	protected ModelAndView showLocalities()
    	{
    		LOG.debug("How are you today?");
    		List<Locality> localities = localityService.fetchAllLocalities();
    		
    		return new ModelAndView("localities", "localities", localities);
    
    	}
    
    	@Autowired(required=true)
    	public void setLocalityService(LocalityService localityService) {
    		this.localityService = localityService;
    	}
    }
    The service is (hopefully) scanned in with...

    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:context="http://www.springframework.org/schema/context"
    	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
    				http://www.springframework.org/schema/aop
    				http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    	<tx:annotation-driven />
    	
    	<context:annotation-config/>
    	<context:component-scan base-package="com.djpteam.bid.service"/>  
    	
    	<bean id="transactionManager"
    				class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    
    </beans>
    and looks like this...

    Code:
    @Service("localityService")
    public class LocalityServiceImpl implements LocalityService {
    	
    	private LocalityDao localityDao;
    
    	@Override
    	@Transactional(readOnly = true)
    	public List<Locality> fetchAllLocalities() {
    		return this.localityDao.fetchAllLocations();
    	}
    	
    	@Autowired(required=true)
    	public void setLocalityDao(LocalityDao localityDao) {
    		this.localityDao = localityDao;
    	}
    }
    The dao is (again hopefully) read in with...

    Code:
    <?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:context="http://www.springframework.org/schema/context"
    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"
    default-autowire="byName">
    
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="${hibernate.connection.driver_class}"/>
    		<property name="url" value="${hibernate.connection.url}"/>
    		<property name="username" value="${hibernate.connection.username}"/>
    		<property name="password" value="${hibernate.connection.password}"/>
    	</bean>
    	
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		
    		<property name="hibernateProperties">
    			<value>
    				${hibernate.dialect}
    			</value>
    		</property>
    		<property name="hibernate.c3p0.min_size"><value>${hibernate.c3p0.min_size}</value></property>
    		<property name="hibernate.c3p0.max_size"><value>${hibernate.c3p0.max_size}</value></property>
    		<property name="hibernate.c3p0.timeout"><value>${hibernate.c3p0.timeout}</value></property>
    		<property name="hibernate.c3p0.max_statements"><value>${hibernate.c3p0.max_statements}</value></property>
    		<property name="hibernate.c3p0.idle_test_period"><value>${hibernate.c3p0.idle_test_period}</value></property>
    		<!-- Show and print nice SQL on stdout -->
    		<property name="show_sql"><value>${hibernate.show_sql}</value></property>
    		<property name="format_sql"><value>${hibernate.format_sql}</value></property>
    		
    		<!-- List of annotated classes-->
    		   <property name="annotatedClasses">
    		     <list>
    		       <value>com.djpteam.bid.model.Locality</value>
    		     </list>
      		 </property>
    		
    		
    	</bean>
    
    	<context:annotation-config/>
    	<context:component-scan base-package="com.djpteam.bid.dao"/>  
    	
    </beans>
    and I was hoping that the session factory got read in with it. Finally the dao itself

    Code:
    @Repository("localityDao")
    public class LocalityDao {
    
    	private SessionFactory sessionFactory;
    	
    	public List<Locality> fetchAllLocations(){
    		Session sess = this.sessionFactory.getCurrentSession();
    		
    		List<Locality> localities = sess.createQuery("from com.djpteam.bid.model.Locality").list();
    		return localities;
    	}
    
    	@Autowired(required=true)
    	public void setSessionFactory(SessionFactory sessionFactory) {
    		this.sessionFactory = sessionFactory;
    	}	
    	
    }
    The config and the classes are deployed from various projects in eclipse to jars in the lib directory, so I'm pretty sure they're all visible.

    Basically the application fails to start up presenting the error...

    Code:
    [WARNING] [talledLocalContainer] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'showLocalitiesController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.djpteam.bid.web.ShowLocalitiesController.setLocalityService(com.djpteam.bid.service.LocalityService); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'localityService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.djpteam.bid.service.LocalityServiceImpl.setLocalityDao(com.djpteam.bid.dao.LocalityDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'localityDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.djpteam.bid.dao.LocalityDao.setSessionFactory(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    The error is, of course, much more verbose, but I think the line above captures the essence of it.

    From the documentation I've seen, the non-autowired sessionFactory should load up with everything else, but it doesn't seem to. So I'm figuring that I've missed some small detail. Can any of you spot the error?

    thanks

  • #2
    can anyone help? I'm really stuck on this

    Comment

    Working...
    X