Spirng入门

Spirng简介

SringIOC

前言

Spring的诞生是为了满足企业级系统的一些需求,那么企业级系统有那些特点,spring究竟带来了那些好处呢

简单介绍企业级应用的特点
EJB的不足
  1. 学习比较难,开发难度大

  2. 依赖应用服务器

  3. 运用大量的设计模式

Spring的优势
  1. 引入spring之后,spring的依赖注入可以统一管理和生成javabean,哪有需要调用就往哪注入,这种方式大大降低了开发难度
  2. 降低了代码的耦合度,给后期的维护也带来了方便
  3. 同时spring的aop还能讲系统中那些类似于日志管理,事务等分布性比较强,但又必须有的代码集中生成,无需开发人员关注,提高工作效率
  4. 业务复杂:设计一个系统,需求说明书就可能有几千页
Spring

轻量级框架, Java EE的春天,当前主流框架

目标

使现有技术更加易用,推进编码最佳实践

内容
  • IoC容器

  • AOP实现

  • 数据访问支持

  • 简化JDBC/ORM 框架

  • 声明式事务

  • Web集成

    Spring开发流程

开发流程
  1. 创建项目
  2. 导入对应的Spring架包
  3. 创建Java类
  4. 创建applicationContext.xml配置文件
  5. 加入日志
  6. 创建单元测试,使用
SpringAOP的开发流程
  1. 创建自己要增强的业务功能
    1. UserServiceLogger
      1. before();
  2. IOC 日志功能
  3. 增加配置文件,对于AOP配置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Spring入门案例,有很多不同的示例代码。在这里,我提供一个简单的Spring MVC示例代码,用于展示如何使用Spring框架构建一个Web应用程序: 1. 首先,需要创建一个基本的Maven项目。 2. 添加Spring MVC依赖项到pom.xml文件中: ```xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.7</version> </dependency> </dependencies> ``` 3. 创建一个HomeController类: ```java package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Hello, World!"); return "home"; } } ``` 4. 创建一个home.jsp文件: ```html <!DOCTYPE html> <html> <head> <title>Home</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 5. 创建一个WebConfig类: ```java package com.example.demo; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp().prefix("/WEB-INF/views/").suffix(".jsp"); } } ``` 6. 创建一个web.xml文件: ```xml <web-app> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` 7. 创建一个springmvc-config.xml文件: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.example.demo" /> <mvc:annotation-driven /> <mvc:view-controller path="/" view-name="home" /> </beans> ``` 8. 将home.jsp文件放在/WEB-INF/views/目录下。 9. 运行应用程序,并在浏览器中访问http://localhost:8080/。您应该能够看到“Hello, World!”消息。 这是一个简单的Spring MVC示例,它展示了如何使用Spring框架构建一个Web应用程序。当然,Spring框架有很多其他功能和用例,这只是一个入门示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值