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