8. 使用注解开发
说明
-
在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>
Bean的实现
- 之前使用的都是bean的标签进行bean注入,但是实际开发中,我们一般会使用注解!
- 配置扫描哪些包下的注解
<!--指定注解扫描包-->
<context:component-scan base-package="pojo"/>
- 在指定包下编写类,增加注解
@Component("user")
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
public String name = "vv"