@Autowired 注解属于 org.springframework.beans.factory. annotation 包,可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。
@Service 注解属于 org.springframework.stereotype 包,会将标注类自动注册到 Spring 容器中。
在配置文件中需要添加 元素来扫描依赖基本包。
<context:component-scan base-package=“net.biancheng.service”/>
User 实体类
public class User{
private String name;
private String pwd;
public String setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
}
创建 UserService 接口
public interface UserService{
boolean login(User user);
}
创建 UserServiceImpl 类,实现 UserService 接口⭐
@Service
public class UserServiceImpl implements UserService{
@Override
public boolean login(User user){
if("yyyjjj".equals(user.getName()) && "12345".equals(user.getPwd())){
return ture;}
return false;
}
}
为了使类能被 Spring 扫描到,必须为其标注 @Service。
创建 UserController 类⭐
@Controller
@RequestMapping("/users")
public class UserController{
@Autowired
private UserService userService;
@RequestMapping("/login")
public String getLogin(Model model) {
User us = new User();
us.setName("bianchengbang");
userService.login(us);
model.addAttribute("user", us);
return "login";
}
}
在 UserService 上添加 @Autowired 注解会使 UserService 的一个实例被注入到 UserController 实例中。
springmvc-servlet.xml 代码
<context:component-scan base-package="com.biancheng"/>
<mvc:annotation-driven>
<bean id="viewResolver"
class="com.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="?WEB_INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
web.xml 代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>springMVC</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.jsp 文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
未注册的用户,请
<a href="${pageContext.request.contextPath }/user/register"> 注册</a>!
<br /> 已注册的用户,去
<a href="${pageContext.request.contextPath }/user/login"> 登录</a>!
</body>
</html>
login.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录页面! 欢迎 ${user.name} 登录
</body>
</html>