Spring day-02-DI,IoC,AOP
DI 注解
- @Autowired:
用于自动装配(自动注入)Bean的依赖关系。
可以用在构造方法、Setter 方法、成员变量、方法上。
Spring 会根据类型(或名称)自动寻找匹配的 Bean 并注入。
▼java复制代码@Component public class MyService { @Autowired @Setter private MyMapper mapper; }
- @Value:
用于注入属性值。
可以注入基本数据类型、字符串、表达式等。
▼java复制代码@Value(" xxx ") private String apiKey;
IoC 注解
@Component: 用于声明一个类为 Spring 组件,会被 Spring 自动扫描并注册为 Bean。 可以使用它来标记普通的 Java 类。
▼java复制代码@Component public class MyComponent { // ... }
@Service、@Repository、@Controller:
这些是特定类型的组件注解,用于标记服务类、仓库类和控制器类。 Spring 通常使用这些注解来更好地组织应用程序的不同层。
▼java复制代码@Service public class MyService { // ... } @Repository public class MyRepository { // ... } @Controller public class MyController { // ... }
IoC 和DI 注解的扫描器
DI扫描器可以不写,但建议都加上.
▼xml复制代码<!--使用 <context:component-scan> 扫描器配置组件扫描 (IoC 注解的扫描器)--> <context:component-scan base-package="com.example.myapp"/> <!--在上面的示例中,base-package 属性指定了要扫描的基础包路径, Spring 会自动扫描该包及其子包下的类,将标记为组件的类注册为 Bean。--> <!--使用 <context:annotation-config> 启用注解驱动 DI注解的扫描器 --> <context:annotation-config/> <!--这个元素会告诉 Spring 启用注解处理器, 以便解析和处理类中的注解,例如 @Autowired、@Value 等。-->
AOP
AOP(面向切面编程)是一种编程范式,它允许你在应用程序的不同模块中插入自定义的行为,而无需修改原始代码。AOP 的核心思想是通过将横切关注点(cross-cutting concerns)从主要业务逻辑中分离出来,然后将这些关注点模块化,以便重复使用。(增强功能,在指定类的指定方法上植入逻辑代码)
具体操作: where(aspectJ的语法), when(before, after-returning, after-throwing), what(自行编写需要增强的功能代码)
xml配置文件织入事务管理器
spring - xml 配置文件(推荐)
▼xml复制代码<!--做什么功能增强--> <bean id="txManager" class="xyz.bycor._03_aop.MyTransactionManager"/> <!-- service --> <bean id="service" class="xyz.bycor._03_aop.EmployeeServiceImpl"/> <!-- aop--> <aop:config> <aop:aspect ref="txManager"> <!--在哪个类的哪个方法织入功能增强代码--> <aop:pointcut id="txPointcut" expression="execution(* xyz.bycor._03_aop.EmployeeServiceImpl.*(..))"/> <!-- 在这个方法运行前织入xxx代码--> <aop:before method="begin" pointcut-ref="txPointcut"/> <!-- 在这个方法运行后织入xxx代码--> <aop:after-returning method="commit" pointcut-ref="txPointcut"/> <!-- 在这个方法异常后织入xxx代码--> <aop:after-throwing method="rollback" pointcut-ref="txPointcut"/> </aop:aspect> </aop:config>
注解
xml 文件配置 -- 添加AOP 扫描器
▼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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- ioc 扫描器 --> <context:component-scan base-package="xyz.bycor._04_aop_annootion"/> <!-- aop注解 扫描器 --> <aop:aspectj-autoproxy /> </beans>
要织入的增强java 代码示例:
▼java复制代码// 模拟事务管理器 @Component // 将织入的类交给Spring 管理 @Aspect //表示切面 public class MyTransactionManager { // 定义切入点 @Pointcut("execution(* xyz.bycor._04_aop_annootion.*ServiceImpl.*(..))") public void txPoint(){ } @Before("txPoint()") public void begin(){ System.out.println("开启事务"); } @AfterReturning("txPoint()") public void commit(){ System.out.println("提交事务"); } @AfterThrowing("txPoint()") public void rollback(){ System.out.println("回滚事务"); } }
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论

