Spring整合成小demo
这个demo涉及到我学的spring的所有操作 以注解的方式实现
导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.13</version>
</dependency>
config类
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
@ComponentScan("com")
@Configuration
@EnableAspectJAutoProxy//扫描AOP文件
@Import(TextConfigration.class)//强行把TextConfigration类注册bean
public class Configration {
}
config测试类 用于强行注册bean
import com.text.entry.Teacher;
import org.springframework.context.annotation.Bean;
public class TextConfigration {
@Bean//将teacher类注册为bean
public Teacher teacher(){
return new Teacher();
}
}
aop类
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Log//日志包,可以不要,用sout一样
@Aspect//AOP操作
@Component//bean
public class AopText {
//============================
// 1.正常切片操作
@Before("execution(* com.text.entry.Student.say(*))")
public void before(JoinPoint joinPoint){
System.out.println(Arrays.toString(joinPoint.getArgs()));
log.info("这是say方法之前的日志");
}
@AfterReturning(value = "execution(* com.text.entry.Student.say(*))",returning = "obj")
public void after(Object obj){
System.out.println(obj);
log.info("这是say方法之后的日志");
}
//============================================
// 2.around方式,注意,around方法需要有返回值
@Around("execution(* com.text.entry.Student.say(*))")
public Object text(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("say方法执行之前的操作");
Object proceed = joinPoint.proceed();
System.out.println("say方法执行之后的操作");
return proceed;
}
}
实体类
student实体
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ToString//lombok依赖的注解,调用student对象可以直接打印其tostring方法
@Component//bean文件注解
public class Student {
String name;
@Value("12")//可以为数据赋值
int age;
Card card;
//可以为数据赋值
@Value("wei")
public void setName(String name) {
this.name = name;
}
public String say(String say){
System.out.println("这是学生类的方法");
return say;
}
@Resource//设置数据源
public void setCard(Card card) {
this.card = card;
}
}
card实体
import lombok.ToString;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@ToString
@Component//bean注解
public class Card {
public Card(){
System.out.println("我是汽车中的构造方法");
}
@PostConstruct//初始化方法注解
public void init(){
System.out.println("我是汽车的初始化方法");
}
@PreDestroy//销毁方法注解
public void destroy(){
System.out.println("汽车的销毁方法");
}
}
teacher实体
public class Teacher {
}
main
import com.text.config.Configration;
import com.text.entry.Student;
import com.text.entry.Teacher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
// spring基础整合-注解篇:1.ioc; 2.aop; 3.强行注册bean
public class Main {
public static void main(String[] args) {
// 加载配置文件类
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Configration.class);
// 加载bean注解类
Student student = context.getBean(Student.class);
//======================
// 1.测ioc操作类
System.out.println("1.测ioc操作类");
System.out.println(student);
System.out.println("==================");
System.out.println();
//======================
// 2.测试aop操作
System.out.println("2.测试aop操作");
student.say("hello");
System.out.println("==================");
System.out.println();
//======================
// 3..测试强行注册bean操作
System.out.println("3..测试强行注册bean操作");
Teacher teacher = context.getBean(Teacher.class);
// 测试注册结果
System.out.println(teacher);
}
}
student实体类对应数据库
/*
Navicat MySQL Data Transfer
Source Server : fiveGods
Source Server Version : 80016
Source Host : localhost:3306
Source Database : db10
Target Server Type : MYSQL
Target Server Version : 80016
File Encoding : 65001
Date: 2022-03-02 10:55:04
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`sex` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'wei', '男');
INSERT INTO `student` VALUES ('2', '测试', '男');
INSERT INTO `student` VALUES ('6', '测试', '男');
INSERT INTO `student` VALUES ('7', '测试', '男');

该博客演示了一个使用Spring注解实现的完整示例,包括配置类、AOP切面、实体类以及主程序。通过注解实现了IoC容器管理、AOP切面拦截和Bean的强制注册。示例中展示了如何在Student类的方法上应用Before、AfterReturning和Around注解,以及Card和Teacher类的生命周期方法。

被折叠的 条评论
为什么被折叠?



