I have created a spring-config.xml file.In that file I have created all beans for service class and DAO class.Now I want to call the bean in my Spring controller.The method I already know is
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
SeviceClassName objService = (SeviceClassName) context.getBean("BeanName");
But the problem is I have put the file in WEB-INF folder. To metigate the problem I have used
ApplicationContext context = new FileSystemXmlApplicationContext("C:/Users/xyz/Desktop/HelloWeb/WebContent/WEB-INF/spring-config.xml");
It's working fine. But it doesn't seem to be a good practise. Then I have tried to initialize the DispatcherServlet by spring-config.xml.
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
But I don't konw how to call the bean in controller.
I tried to use @Autowired in my spring controller.But it is not working.
Whenever I try this (@Autowired) and try to excecute a jsp file of the same application in eclipse it shows 'requested resource not avaliable".
Can any one suggest me how to solve the problem.
Or
Can anyone suggest me a better approach by which I can invoke a bean of service class which I have created in spring-config.xml.
Or
any other approach for invoking service class methods in spring controller.
I am using spring 3.0. I have created a Dynamic Web Project in Eclipse.In my WEB-INF folder I have put 4 xml file --web.xml,spring-config.xml,HelloWeb-servlet.xml . My HelloWeb-servlet contains
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.tutorialspoint" />
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
</bean>
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>
My spring-config contains all the bean configuration.In the spring controller I just to invoke the service method which are written in the service class for which I have already created bean in spring-config.xml.