java spring使用_[Java] Spring 使用

本文介绍了Spring框架中IOC(反转控制)和AOP(面向切面编程)的概念及其应用。通过示例展示了如何在JavaEE应用中使用Spring创建对象并进行依赖注入,以及如何利用AOP实现日志记录功能与核心业务的解耦。通过XML配置文件,将日志切面与业务逻辑编织在一起,实现了低耦合的设计,提高了代码的可维护性。
摘要由CSDN通过智能技术生成

背景

JavaEE 应用框架

基于IOC和AOP的结构J2EE系统的框架

IOC(反转控制):即创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象,是Spring的基础

DI(依赖注入):拿到的对象的属性,已经被注入好相关值了,直接使用即可

AOP(Aspect Oriented Program 面向切面编程)

功能分为核心业务功能(登录、增删改查)和周边功能(性能统计、日志、事务管理)

周边功能在Spring的面向切面编程AOP思想里,即被定义为切面

面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发

然后把切面功能和核心业务功能 "编织" 在一起

IOC/DI

新建项目

添加依赖jar包

准备 pojo

配置 applicationContext.xml

7a8133c7afa23a5b2023b6ddd820eb9d.png

Catagory.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.how2java.pojo;2

3 importorg.springframework.stereotype.Component;4

5 @Component("c")6 public classCategory {7

8 public intgetId() {9 returnid;10 }11 public void setId(intid) {12 this.id =id;13 }14 publicString getName() {15 returnname;16 }17 public voidsetName(String name) {18 this.name =name;19 }20 private intid;21 private String name = "catagory 1";22 }

View Code

Product.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.how2java.pojo;2

3 importorg.springframework.beans.factory.annotation.Autowired;4 importorg.springframework.stereotype.Component;5

6 @Component("p")7 public classProduct {8 private intid;9 private String name = "product 1";10

11 @Autowired12 privateCategory category;13 public intgetId() {14 returnid;15 }16 public void setId(intid) {17 this.id =id;18 }19 publicString getName() {20 returnname;21 }22 public voidsetName(String name) {23 this.name =name;24 }25 publicCategory getCategory() {26 returncategory;27 }28 public voidsetCategory(Category category) {29 this.category =category;30 }31 }

View Code

TestSpring.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.how2java.test;2

3 importorg.springframework.context.ApplicationContext;4 importorg.springframework.context.support.ClassPathXmlApplicationContext;5

6 importcom.how2java.pojo.Product;7

8 public classTestSpring {9

10 public static voidmain(String[] args) {11 ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml"});12

13 Product p = (Product) context.getBean("p");14 System.out.println(p.getName());15 System.out.println(p.getCategory().getName());16 }17 }

View Code

applicationContext.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"

5 xsi:schemaLocation="6 http://www.springframework.org/schema/beans

7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

8 http://www.springframework.org/schema/aop

9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

10 http://www.springframework.org/schema/tx

11 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

12 http://www.springframework.org/schema/context

13 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

14

15

16

17

18

19

20

21

22

23

24

View Code

cbe5cb7da991e11ddd034d47daf12b6c.png

AOP

辅助功能和核心业务功能彼此独立开发

“登录”功能没有“性能统计”和“日志输出”也可以正常运行

如果有需要就把“日志输出”和“登录”功能编制在一起,实现登录时看到日志输出

辅助功能叫做切面,这种选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫AOP

7be3de5786af9b6bb29ecbebf436d7c7.png

ProductService.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.how2java.service;2

3 public classProductService {4 public voiddoSomeService() {5 System.out.println("doSomeService");6 }7 }

View Code

LoggerAspect.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.how2java.aspect;2

3 importorg.aopalliance.intercept.Joinpoint;4 importorg.aspectj.lang.ProceedingJoinPoint;5

6 public classLoggerAspect {7 public Object log(ProceedingJoinPoint joinPoint) throwsThrowable{8 System.out.println("start log:" +joinPoint.getSignature().getName());9 Object object =joinPoint.proceed();10 System.out.println("end log:" +joinPoint.getSignature().getName());11 returnobject;12 }13 }

View Code

TestSpring.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.how2java.test;2

3 importorg.springframework.context.ApplicationContext;4 importorg.springframework.context.support.ClassPathXmlApplicationContext;5

6 importcom.how2java.service.ProductService;7

8 public classTestSpring {9 public static voidmain(String[] args) {10 ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml"});11 ProductService s = (ProductService) context.getBean("s");12 s.doSomeService();13 }14 }

View Code

applicationContext.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xmlns:aop="http://www.springframework.org/schema/aop"

5 xmlns:tx="http://www.springframework.org/schema/tx"

6 xmlns:context="http://www.springframework.org/schema/context"

7 xsi:schemaLocation="8 http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd10 http://www.springframework.org/schema/aop11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd12 http://www.springframework.org/schema/tx13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd14 http://www.springframework.org/schema/context15 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

16

17

18

19

20

21

22

23

24 expression=25 "execution(* com.how2java.service.ProductService.*(..))"/>

26

27

28

29

30

31

32

View Code

0dc58cf969d607b7343c1c5c5caa128e.png

通过xml文件配置的方式,把日志记录功能(切面)和核心业务编织在了一起,而TestSpring代码没有发生任何变化(解耦)

67d926186ac251c8ae96aa82a811c164.png

参考

Spring DI原理

Spring的IOC原理

步骤

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值