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