Spring框架——注解

本文介绍了Spring框架中如何使用@Component实现对象管理,@Autowired进行自动装配,以及@Scope定义对象作用域。通过实例展示了如何在配置文件中设置组件扫描,以及在类中使用注解来初始化对象属性,并探讨了单例和原型两种不同作用域的使用场景。
摘要由CSDN通过智能技术生成

1.@Component申请IOC容器管理

  • @Component的作用相当于在Spring配置文件中用< bean >标签创建对象,注解该类归由IOC容器进行管理
    step1:必须由Spring自动扫描包,将注有@Component注解的类归于IOC容器进行管理,需要在spring配置文件中表明扫描范围
<context:component-scan base-package="com.system.entity"></context:component-scan>
  • step2:在需要IOC容器管理的类中添加@Component注解
    可以在注解中给类添加“id”

@Component(value=“id”)
或 @Component(“id”)  当只有一个属性值时,value可省略不写
@Component   默认id为类名的首字符小写,其他不变,如EmpService->empService

@Component
public class Student {
	private int id;
	private String name;
	private int age;
}

2.@Autowired自动装配

  • 默认是按照byType类型进行自动装配
  • 在需要自动装配的对象上面加上该注解即可
  • @Value(“值”)可为每个属性初始化值
@Data
@Component
public class Dept {
	@Value("10")
	private Integer deptno; //部门编号 数据库int类型---- 使用int的封装类Integer
	@Value("设计部")
	private String dname; //部门名称
	@Value("杭州")
	private String loc;
}

@DataData
@Component
public class Emp {
	@Value("7788")
	private Integer empno; //员工编号 8个基本数据类型--》封装类
	@Value("张三")
	private String ename; //员工姓名
	@Value("经理")
	private String job; //职位
	@Value("10000.00")
	private double sal; //工资
	@Value("800.00")
	private double comm; //奖金
	@Autowired//按照数据类型,自动装配
	private Dept dept;
}

3.@Scope作用域 注解

  • 加载在类的上方
  • @Scope(“类型”)  类型取值:{singleton(单例), prototype(原型)}
@Data
@Component
@Scope("prototype")
public class Emp {
	@Value("7788")
	private Integer empno; //员工编号 8个基本数据类型--》封装类
	@Value("张三")
	private String ename; //员工姓名
	@Value("经理")
	private String job; //职位
	@Value("10000.00")
	private double sal; //工资
	@Value("800.00")
	private double comm; //奖金
	@Autowired
	@Qualifier("dept10")
	private Dept dept;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP是Spring框架中的一个重要模块,它提供了面向切面编程(AOP)的支持。AOP是一种编程思想,它可以在不改变原有代码的情况下,通过在程序运行时动态地将代码“织入”到现有代码中,从而实现对原有代码的增强。 Spring AOP提供了基于注解的AOP实现,使得开发者可以通过注解的方式来定义切面、切点和通知等相关内容,从而简化了AOP的使用。 下面是一个基于注解的AOP实现的例子: 1. 定义切面类 ```java @Aspect @Component public class LogAspect { @Pointcut("@annotation(Log)") public void logPointcut() {} @Before("logPointcut()") public void beforeLog(JoinPoint joinPoint) { // 前置通知 System.out.println("执行方法:" + joinPoint.getSignature().getName()); } @AfterReturning("logPointcut()") public void afterLog(JoinPoint joinPoint) { // 后置通知 System.out.println("方法执行完成:" + joinPoint.getSignature().getName()); } @AfterThrowing(pointcut = "logPointcut()", throwing = "ex") public void afterThrowingLog(JoinPoint joinPoint, Exception ex) { // 异常通知 System.out.println("方法执行异常:" + joinPoint.getSignature().getName() + ",异常信息:" + ex.getMessage()); } } ``` 2. 定义业务逻辑类 ```java @Service public class UserService { @Log public void addUser(User user) { // 添加用户 System.out.println("添加用户:" + user.getName()); } @Log public void deleteUser(String userId) { // 删除用户 System.out.println("删除用户:" + userId); throw new RuntimeException("删除用户异常"); } } ``` 3. 在配置文件中开启AOP ```xml <aop:aspectj-autoproxy/> <context:component-scan base-package="com.example"/> ``` 在这个例子中,我们定义了一个切面类LogAspect,其中通过@Aspect注解定义了一个切面,通过@Pointcut注解定义了一个切点,通过@Before、@AfterReturning和@AfterThrowing注解分别定义了前置通知、后置通知和异常通知。 在业务逻辑类中,我们通过@Log注解标注了需要增强的方法。 最后,在配置文件中,我们通过<aop:aspectj-autoproxy/>开启了AOP功能,并通过<context:component-scan>扫描了指定包下的所有组件。 这样,当我们调用UserService中的方法时,就会触发LogAspect中定义的通知,从而实现对原有代码的增强。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值