Spring整合成小demo

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

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', '测试', '男');

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

君问归期魏有期

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

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

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

打赏作者

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

抵扣说明:

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

余额充值