Announcement Announcement Module
Collapse
No announcement yet.
Another @Autowired problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Another @Autowired problem

    Hi all,

    even though this seems to be the 10.000st thread concerning this topic I can't get it to work reading the present responses to other topics.

    I have a layered web-application. DAO-layer, service-layer and controller-layer. So using the @Repository, @Service and @Controller annotations for spring bean-config fits perfectly for me. Now comes the problem:

    I have my daos correctly autowired into my service layer using @Autowired annotations. But in the controller-layer the service members don't get autowired. I get no exception. But when accessing the service member in the controller class its just null. Even more strange is, that when trying to get a bean instance via the application-context it works!

    Here is the sample code:

    Code:
    @Repository
    public class HibernateEmployeeDao extends HibernateBaseDao implements EmployeeDao
    {
    ...
    }
    Code:
    @Service
    @Transactional
    public class EmployeeServiceImpl implements EmployeeService
    {
            @Autowired
    	private EmployeeDao employeeDao;
    ...
    }
    So far everything works perfectly!


    Code:
    @Controller
    public class EmployeeWebServiceImpl implements DomainServicesSkeletonInterface
    {
    	private final static Logger log = Logger.getLogger(EmployeeWebServiceImpl.class);
    
    	@Autowired
    	private EmployeeService employeeService;
    
    	@Override
    	@SecurityDomain(ServiceOperation.READ)
    	public FindResponse find(FindRequest p_findRequest)
    	{
    		FindResponse findResponse = null;
    		SearchResult<Employee> searchResult = null;
    		employeeService = ApplicationContextHolder.getApplicationContext().getBean(EmployeeService.class);
    }
    Here is my problem: the employeeService member is never autowired at all, while the line ...getBean(EmployeeService.class) works fine.

    I have to mention that there are interfaces for daos and services of course. I am working in eclipse for the moment.

    Now at last here comes my spring configuration.

    spring.xml in dao/service layer
    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"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	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/tx 
         	 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    	<!-- Tell spring where to search for beans -->
    	<context:component-scan base-package="eu.inform"/>
    	
    	<!-- Tell spring that beans are configured via annotations -->
    	<context:annotation-config/>
    
       ...
    web.spring.xml in web layer
    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: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/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!-- import configuraton from service layer -->
    	<import resource="classpath:spring.xml"/> 
    	
       	<!-- Tell spring where to search for beans -->
    	<context:component-scan base-package="eu.inform"/>
    	
    	<!-- Tell spring that beans are configured via annotations -->
    	<context:annotation-config/>
        
        <!-- Tell spring to enable aop with proxies -->
    	<aop:aspectj-autoproxy/>
    
    </beans>
    I am using spring 3.1.1 with Oracle JDK 1.7 and an Apache Tomcat 7 application container.
    Can anyone see where I am wrong?

    Thank you all,

    Sebastian

  • #2
    If it isn't injected I suspect your bean isn't managed by Spring at least you aren't getting the managed instance from spring. Not sure what the @SecurityDomain annotation does or is but for me that is probably an indication you are using some other framework to expose something as a webservice?

    Some other observations

    1) Don't use HibernateDaoSupport/HibernateTemplate it should be considered deprecated
    2) Your configuration is flawed as you have duplicate instances of your beans (you are scanning for the same components twice).

    Comment


    • #3
      Hi Marten,

      the @SecurityDomain annotation is part of my permission concept. I use this annotation to define pointcuts for aspects with spring aop. Nice that you mention this here because it seems that these aspects also are not triggered when executing methods annotated with this @SecurityDomain annotation. I guess this is related to my stated problem with the autowiring.

      1) Don't use HibernateDaoSupport/HibernateTemplate it should be considered deprecated
      I do not use HibernateDaoSupport since it is - as you mentioned - deprecated

      2) Your configuration is flawed as you have duplicate instances of your beans (you are scanning for the same components twice).
      Thanks for this hint. I did not know that spring will scan twice when there are identical component-scan configurations. I guessed that these paths would be summarized to a global pattern.

      According to that hint I changed my spring configurations to:

      Code:
      <!-- Tell spring where to search for beans -->
      <context:component-scan base-package="eu.inform">
      	<!-- Make all daos a bean -->
      	<context:include-filter type="regex" expression=".model.dao" />
      	<!-- Make all services a bean -->
      	<context:include-filter type="regex" expression=".service" />
      </context:component-scan>
      and

      Code:
      <context:component-scan base-package="eu.inform.ws" />
      but that didn't make any difference. I also made a count on how many beans of the desired type exist:

      Code:
      INFO 2012-06-08 10:38:40,447 [http-bio-8081-exec-5][] root - # of beans of type eu.inform.service.EmployeeService: 1
      Any other idea?

      Sebastian

      Comment

      Working...
      X