spring

本文介绍了Spring框架的基础概念,包括控制反转(IOC)、依赖注入(DI)、面向切面编程(AOP),并详细说明了如何通过XML配置管理Bean。此外,还探讨了Spring与其他框架如Struts2和Hibernate的整合方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struts2
显示层:MVC

hibernate:
持久层:ORM






spring:
业务层:

导入jar包:



控制反转(IOC):Inversion of Control
管理bean
配置xml文件:
applicationContext.xml
beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
</beans>
</xml>


依赖注入(DI):(Dependency Injection)
配置文件注入
两种注入方式:
Set方法注入
构造方法注入
注入各种类型
普通类型
其它Bean对象
null值
集合类型
注解注入:
在主配置文件中申明注解

<context:component-scan base-package="spring2test.bean"></context:component-scan>

@Resource(name="" )//用于成员变量
作用一样
@Component//主要用于类(作用域)中
@Service//主要用于业务层
@Controller//主要控制层
@Repository//主要持久层
@Scope("prototype")//设置为多例(原型),默认为单例

API:
Resource
BeanFactory

ApplicationContext(推荐)
ClassPathXmlApplicationContext
方法
getBean();
获取定义的bean的总数量
getBeanDefinitionCount()
getBeanDefinitionNames();
getBeansOfType();//返回String[]
getBeanForType();//返回Map<String,Object>

bean的作用域:
singleton:单例
prototype:原型
request:
session
global session



面向切面编程(AOP):Aspect Oriented Programing
切入点(作用于哪里)+通知(做什么事):横切关注点(切面)
切入点表达式:
executer(* *(..))

申明切入点---->>申明通知--->>生成切面--->增加这个切面生成相应的代理对象


API:






与其他框架整合(SSH)
解耦
SS整合:
导入jar包
在web.xml中配置监听器和struts2的过滤器

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>


<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

添加一个Struts2的插件jar包\
与JDBC的融合
API:
JdbcTemplate

--------------------
代理:
静态代理:
需要都实现接口

JDK方式动态代理:
一定要实现接口:
需要实现:InvocationHandler
Proxy.newProxyInstance(loader, interfaces, h)


cglib方式动态代理
final的类,方法不能代理,
需要实现:MethodInterceptor















































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wscwsc58888

只为正式环境创作

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

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

打赏作者

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

抵扣说明:

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

余额充值