0

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.

priya kar
  • 11
  • 4
  • How did you set up your project and whats the hierarchy? Where is your configuration for beans. Post related files and code. – Darshan Lila Aug 28 '14 at 13:47
  • I have created a Dynamic Web Project in eclipse. – priya kar Aug 28 '14 at 13:54
  • Please post relevant code and files. – Darshan Lila Aug 28 '14 at 13:54
  • @Darshan Lila can U check the modified question .Do U need any more details. – priya kar Aug 28 '14 at 14:15
  • Which bean are you trying to access? If you have declared it in `HelloWeb-servlet.xml`, it should be accessed the way you are accessing. Otherwise use `@Service`, `@Component`, `@Resource` etc. for spring to discover it, and later on use `@Autowired` to get them. Note that all your classes should fall under `com.tutorialspoint` package. – Darshan Lila Aug 29 '14 at 05:15

1 Answers1

0

Check this Stackoverflow answer. It is probably a much more detailed answer than what you are looking for, but it will definitely help you go through the basics.

Essentially, a root context established via a Spring context loader listener, and a servlet context established via the corresponding servlet parameter. The param name for both are contextConfigLocation(former is a listener context-param and the latter is a servlet init-param).

@Autowired or @Inject can be used to inject the service bean in the controller. Make sure that the service bean is annotated with @Service or @Resource so the bean is picked up during component scanning.

Community
  • 1
  • 1
Web User
  • 7,438
  • 14
  • 64
  • 92