0

Service class

 package com.company.service;

 @org.springframework.stereotype.Service
 public class UserWorkflow {
     private Logger logger = Logger.getLogger(UserWorkflow.class)
     @Autowired
     public UserMessageViewDAO MessageViewDAO;
 }    

Dao class

package com.company.dao;

@org.springframework.stereotype.Repository
public class UserMessageViewDAO extends BaseDao{
    public UserMessageViewDAO () {

    }
}

in applicationContexe.xml

<context:component-scan base-package="com.company.dao"/>
<context:component-scan base-package="com.company.service"/>
<context:annotation-config/>

@Autowire annotation is not working properly. It is displaying value of MessageViewDAO as null when called methods in service class .But there is no errors in the log when application is deploying. Could you help me to find a problem?

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
SRK
  • 130
  • 1
  • 9
  • How do you obtain an instance of UserWorkflow? – JB Nizet Aug 28 '16 at 12:20
  • instance of UserWorkflow is obtained by Autowiring the UserWorkflow.This is not working – SRK Aug 29 '16 at 06:00
  • I don't understand the '@Autowired public UserMessageViewDAO MessageViewDAO. Your code shows that UserMessageViewDAO is a concrete class/implementation. Is MessageViewDAO an Interface? If so, then '@Autowired MessageViewDAO UserMessageViewDAO would be correct. – Rudolf Held Aug 29 '16 at 18:33
  • Here i used only classes.no interfaces used.@Autowired public UserMessageViewDAO MessageViewDAO.Here the UserMessageViewDAO is a class.MessageViewDAO is not an interface – SRK Aug 30 '16 at 05:06

1 Answers1

0

The second context:component-scan overwrites the first one, which is why com.company.dao is not scanned. If you want to use several packages, include them with something like:

<context:component-scan base-package="com.company.dao, com.company.service" />

or, in short (package scan is recursive):

<context:component-scan base-package="com.company" />

You might also want to have a look at: multiple packages in context:component-scan, spring config

Edit 2016-09-12: Use the annotations @Service and @Repositiory. The Annotations @org..springframework.stereotype. ... look weird.

Community
  • 1
  • 1
Rudolf Held
  • 503
  • 3
  • 7