文章目录
使用注解开发
1. 说明
在spring4之后,想要使用注解形式,必须得要引入aop的包
在配置文件当中,还得要引入一个context约束
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
2. Bean的实现@Component
我们之前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解!
- 配置扫描哪些包下的注解
<!--指定注解扫描包-->
<context:component-scan base-package="com.xxc.pojo"/>
- 在指定包下编写类,增加注解
@Component("user") // 不写括号的时候默认为类首字母小写名
// 相当于配置文件中 &