本文将从多个方面对Spring6知识点进行详细的阐述,包括:IoC容器、AOP、MVC等。
一、IoC容器
1、IoC介绍
控制反转(IoC)是指在我们的程序中,对象的创建及其依赖关系的维护交给了容器,而不是由开发人员直接去创建和维护。Spring框架提供了一个IoC容器简化了应用程序中组件的配置及创建。Spring IoC容器实现了IoC设计模式,是应用程序的基础。
2、IoC容器的类型
Spring框架提供了两种类型的IoC容器:
1. BeanFactory: Bean工厂是最简单的容器,提供仅基本功能。
2. ApplicationContext: ApplicationContext基于BeanFactory构建并提供了更多企业级的功能。
3、IoC容器的生命周期
IoC容器在启动的时候,会加载XML配置文件,解析其中声明的各种Bean,根据配置创建Bean实例,然后交给Spring框架管理。
4、IoC容器的使用示例
在编写Spring应用程序时,我们需要提供一些文件来配置依赖关系和Bean。下面是一个简单的配置文件:
<!-- config.xml -->
<beans>
<bean id="helloWorld" class="com.example.HelloWorld"/>
</beans>
其中,id属性用于标识Bean,class属性指定了Bean的类。在Java代码中,可以使用ApplicationContext接口加载配置文件,并通过getBean()方法获取Bean实例:
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage(); // 输出:Hello World!
二、AOP
1、AOP介绍
面向切面编程(AOP)是一种编程范式,它将横跨应用程序的行为(例如性能监控、日志记录等)与业务逻辑相分离,这样可以使开发人员更加关注业务逻辑,而不必处理其他横跨应用程序的行为。
2、AOP术语
- Joinpoint(连接点)
在应用程序中,一些离散的点被称为连接点。
- Pointcut(切入点)
切入点是在连接点处应用Advice的条件。
- Advice(通知)
通知是在切入点执行的代码。通知类型包括前置通知、后置通知、返回通知、环绕通知和抛出通知。
- Aspect(切面)
切面是通知和切入点的结合。
- Weaving(织入)
将切面与Java对象组合在一起创建一个通知对象的过程被称为织入。
3、AOP的实现方式
- 基于Java代码实现AOP:
public class LoggingAspect {
public void beforeAdvice(JoinPoint jp){
System.out.println("Before method:"+ jp.getSignature());
}
public void afterAdvice(JoinPoint jp){
System.out.println("After method:"+ jp.getSignature());
}
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString());
}
}
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
在XML配置文件中指定切面:
<bean id="loggingAspect" class="com.example.LoggingAspect"/>
<aop:config>
<aop:aspect id="logAspect" ref="loggingAspect">
<aop:pointcut expression="execution(* com.example.*.*(..))" id="businessService"/>
<aop:before pointcut-ref="businessService" method="beforeAdvice"/>
<aop:after pointcut-ref="businessService" method="afterAdvice"/>
<aop:after-returning pointcut-ref="businessService" returning="retVal" method="afterReturningAdvice"/>
</aop:aspect>
</aop:config>
- 基于注解实现AOP:
@Aspect
public class LoggingAspect {
@Before("execution(* com.example.*.*(..))")
public void beforeAdvice(JoinPoint jp){
System.out.println("Before method:"+ jp.getSignature());
}
@After("execution(* com.example.*.*(..))")
public void afterAdvice(JoinPoint jp){
System.out.println("After method:"+ jp.getSignature());
}
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "retVal")
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString() );
}
}
三、MVC
1、MVC介绍
MVC(Model-View-Controller)模式是一种设计模式,它包括三个部分:模型(Model)、视图(View)和控制器(Controller)。Spring MVC框架是一种基于MVC模式的Web框架。
2、MVC的基本架构
- 前端控制器:
在Spring MVC架构下,DispatcherServlet相当于前端控制器(Front Controller),用于统一处理所有请求。当请求进入应用程序时,它会被DispatcherServlet捕获并根据请求匹配相应的控制器。
- 控制器:
控制器是应用程序核心的控制部分,用于处理请求并根据需要将数据返回给视图或进行其他处理。它是后端系统的中心。
- 模型:
模型是处理数据及其状态的部分,它通常包括业务逻辑处理以及与数据存储交互的代码。模型主要是后端系统的职责。
- 视图:
视图是呈现数据及其状态的部分,它在客户端浏览器上呈现信息,例如HTML、JSON或XML等。
3、MVC的使用示例
首先需要配置Spring MVC的DispatcherServlet,下面是一个简单的配置文件:
<!-- spring-servlet.xml -->
<beans>
<context:component-scan base-package="com.example.web"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
配置完成后,需要编写控制器和视图。下面是一个简单的控制器:
@Controller
@RequestMapping("/hello")
public class HelloWorldController {
@RequestMapping(method = RequestMethod.GET)
public String hello(ModelMap model) {
model.addAttribute("message", "Hello World!");
return "hello";
}
}
在上述代码中,RequestMapping注解指定了处理请求的映射路径,使用ModelMap将数据传递给视图,返回视图名称。
下面是一个JSP视图:
<!-- hello.jsp -->
<html>
<body>
<h2>${message}</h2>
</body>
</html>
当在浏览器中请求/hello时,DispatcherServlet会把请求交给HelloWorldController处理。处理完毕后,它会找到配置文件中指定的viewResolver并呈现对应的视图。
四、结语
本文对Spring6框架中的IoC容器、AOP和MVC进行了详细的阐述,它们为开发人员提供了基础、便利的应用程序开发方式。在实际应用开发中,我们可以根据需要适当选择和组合这些知识点,提升我们的编程效率。