I have been tasked with updating a old project that I did not write.
The project is Spring MVC based and has an older Spring Controller configuration that I am unfamiliar with.
The controllers have bean configurations as follows
<bean id="controllerName" class="the.project.controller.class">
<property name"serviceName">
<ref bean="serviceName">
</property>
<property name"successView">
<value>viewName</value>
</property>
</bean>
where serviceName refers to a class annotated with @Service as follows
@Service(value=serviceName)
Is this the correct replacement for the xml configuration ?
@Autowired
@Qualifier("serviceName")
ServiceNameImpl serviceName
thanks
edit here is the organization of the serviceName class and interface
public interface ServiceName {
// methods omitted
}
@Service(value="serviceName")
public class ServiceNameImpl implments ServiceName {
//methods omitted
}
The @Resource annotation is not available to me ( Spring 3.0.7) and Autowire as above fails ( as it appears the type is not as expected as described below )
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching beans of type [the.project.ServiceNameImpl] found for dependency
Given the edits, what am I doing wrong here ( Apologies for leaving out this information )?
in the end I need to be able to access the methods of the interface and its implementation
for example
serviceName.doSomething(someVar);
That’s correct, but consider using
privatemodifier forserviceName. Another way would be to use@Resource:Note that in this case you don’t need a
@Qualifier("serviceName")–@Resourceautowires by (field) name while@Autowireduses type by default. Only a problem when you have several beans of the same/compatible type.Also you can skip the
controllerNamebean definition altogether by annotating controller class with@Controller.BTW you can also shorten the XML configuration a bit by using the following syntax:
(IntelliJ suggests this transformation and performs it for you).