@Repository annotation:- Annotate all your DAO classes with @Repository. All of the database access logic in the DAO classes. this annotation indicate that this class can done the database related operations like insert,delete,update etc. this is specialized form of @Component annotation .
HelloController.java
package com.evon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.evon.service.IStudentService;
@Controller
public class HelloController {
@Autowired
IStudentService studentService;
@RequestMapping(value="/welcome",method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
System.out.println("Controller******");
String message=studentService.studentServiceMethod();
model.addAttribute("message", message);
return "hello";
}
}
StudentService.java
package com.evon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.evon.dao.IStudentDAO;
public interface IStudentService {
public String studentServiceMethod();
}
@Service("studentService")
public class StudentService implements IStudentService {
@Autowired
IStudentDAO studentDAO;
@Override
public String studentServiceMethod() {
// TODO Auto-generated method stub
System.out.println("Service******");
return studentDAO.studentDAOMethod();
}
}
StudentDAO.java
package com.evon.dao;
import org.springframework.stereotype.Repository;
public interface IStudentDAO {
public String studentDAOMethod();
}
@Repository("studentDAO")
public class StudentDAO implements IStudentDAO {
@Override
public String studentDAOMethod() {
// TODO Auto-generated method stub
System.out.println("DAO******");
return "Hello SPRING";
}
}
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:component-scan base-package="com.evon" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
hello.jsp
<html>
<body>
<h1>Message : ${message}</h1>
</body>
</html>
0 Comment(s)