Spring基础知识点

本文详细解读Springbean的定义,包括Bean的生命周期管理、装配方式,以及Spring IoC容器的作用。涵盖自动装配、XML配置和Spring Boot YAML配置实例。还介绍了Spring ORM整合和Spring JDBC配置,涉及对象关系映射和数据源管理。

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

Spring bean的定义

spring bean 是一个被实例化,组装,并通过 Spring IoC(Inversion of Control) 容器所管理的对象。

Spring bean的生命周期

Bean的定义——(BeanDefinition)Bean的初始化——Bean的使用——Bean的销毁

Spring bean的装配方式

  • 隐式的bean扫描发现机制和自动装配
  • 在java中进行显示配置
  • 在XML中进行显示配置
    <bean id="xxService" class="com.xx.service">
          <constructor-arg ref="xxMapper"/>
            <constructor-arg ref="yyMapper"/>
    </bean>

Spring 容器

是bean对象的集合,通过IoC也叫DI(dependency injection)将被spring管理的bean放到spring容器中。

Spring 切面的定义和使用

@Aspect定义切面类

@Aspect
public class Aspect{

}

通过@Before、@After、@Around、@AfterReturning、@AfterThrowing注解标识切面执行的方式。需要注意,切面的目标必须是spring容器管理的组件,一般切面目标都会加@Component之类的注解。

Spring JDBC

传统的spring基于xml配置jdbc数据源

<!-- 1.配置数据源 -->
<bean id="dataSource"
	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<!-- 1.1.数据库驱动 -->
	<property name="driverClassName"
		value="com.mysql.jdbc.Driver"></property>
	<!-- 1.2.连接数据库的url -->
	<property name="url"
		value="jdbc:mysql://localhost:3306/数据库名称?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true"></property>
	<!-- 1.3.连接数据库的用户名 -->
	<property name="username" value="root"></property>
	<!-- 1.4.连接数据库的密码 -->
	<property name="password" value="12345678"></property>
</bean>

<!-- 2配置JDBC模板 -->
<bean id="jdbcTemplate"
	class="org.springframework.jdbc.core.JdbcTemplate">
	<!-- 默认必须使用数据源 -->
	<property name="dataSource" ref="dataSource"></property>
</bean>

springboot使用yaml配置

spring:
  datasource:
  	# 连接池类型
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    # jdbc连接url
    url: jdbc:mysql://localhost:3306/数据库名称?characterEncoding=UTF-8
    username: 用户名
    password: 密码

Spring ORM

对象关系映射(Object Relational Mapping),简单来说就是数据表和面向对象编程语言中具体类的一种对应关系。常用的ORM框架有Jpa,Mybatis等,通过spring可以快速整合ORM框架。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

兴趣使然的程序猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值