Spring MVC @Autowired和@Service注解

@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>
    <!-- 部署 DispatcherServlet -->
    <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>
        <!-- 表示容器再启动时立即加载servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 处理所有URL -->
        <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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gary jie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值