Sprinp基础注解和Aop笔记

1、对象产生用配置文件

2、类中对象属性用注解

=======================================================================

@Component::一般类

@Serviec:服务层

@Controller:控制层

@Repository:持久层

一、注解

重点: 有注解,就一定要有扫描器

<!--自动扫描指定包及其子包下的所有Bean类--> 我在这边学到的Bean类,感觉有点懂。 

<context:component-scan base-package="com.company.entity"/> 其中com.company.entity是包

1、@Autowired   

一般用在类中对象属性,在XML中一定要有一个与之匹配的Bean类(现在还不知道是啥)。

相当于new 了一个对象 ,进行了初始化。可以使用类中对象了。但是在XML只有一个与之相同。

2、@Resource(name = "student1")

这和@Autowired 差不多,但是name可以与Bean类中的id值配,可以有多个与之相同类型的Bean类。

3、@Conponent(value = "student")

     @Scope("singleton")     //是ton 不是tom,我之前写错了

     @Value("s001") 

@Component(value = "student1)  其中的value和Bean类中的id一个意思,扫描器扫描到了,就会创建一个id为student1的Bean类

@Scope("singleton")  // Scope 范围  singleton 单类

@Value("s001") 给类中成员赋值

二、Aop  面向切面编程  日志 |  事务  |  可以实现功能增强

1、先把要用到的Bean类建好  (切点/切面)、

2、代码  这个是环绕 aop:around

<aop:config>
      <!--切点-->
      <aop:pointcut id="pointcut1" expression="execution(* 
      com.company.entity.User.goShopping(..))"/>
      <--切面-->
      <aop:aspect ref="addClass1">
           <aop:around method="getDate" pointcut-ref="pointcut1"></aop:around>
      </aop:aspect>
</aop:config>

在method="getDate" 方法中传入参数 ProceedingJoinPoint proceedingJoinPoint

代码

//参数  ProceedingJoinPoint proceedingJoinPoint;
Date date = new Date();      //时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");   //设置时间格式
String sdfstr = sdf.format(date);   //转换
System.out.println(sdfstr);
  proceedingJoinPoint.proceed();    //要环绕的点
System.out.println(sdfstr);

结果

时间: 2023-08-18 16-36-12
正在购物
时间: 2023-08-18 16-36-12

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值