1、什么是注解
- 注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值,…)
- 使用注解,注解作用在类上面,方法上面,属性上面
- 使用注解目的:简化xml配置
2、Spring针对Bean管理中创建对象提供的注解
- @Component //组件
- @Service //用于service层
- @Controller //用于web层
- @Repository //一般用于dao层
四个注解功能是一样的,都可以用来创建bean实例
3、基于注解方式实现对象创建
第一步,引入aop.jar包
第二步,开启组件扫描
<?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:p="http://www.springframework.org/schema/p"
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">
<!--开启组件扫描
1 如果扫描多个包,多个包使用逗号隔开
2 扫描包上层目录
-->
<context:component-scan base-package="com.daijunyi"></context:component-scan>
</beans>
第三步创建 UserService类
package com.daijunyi.service;
import org.springframework.stereotype.Component;
//在注解里面value属性值可以省略不写,
//默认值是类名称,首字母小写 UserService--->userService
@Component(value = "userService")//<bean id="userService"/>
public class UserService {
public void add(){
System.out.println("add方法");
}
}
第四步 测试
@Test
public void testUserService(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
4、开启组件扫描细节问题
1、自己指定包下扫描内容
<!--示例1
use-default-filters="false"表示现在不使用默认filter,自己配置filter
context:include-filter,设置扫描那些内容
-->
<context:component-scan base-package="com.daijunyi" use-default-filters="false">
<!--表示只扫描 Component注解的类-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
2、排除包下某些内容不扫描
<!--示例2
下面配置扫描包所有内容,但是排除Repository注解
-->
<context:component-scan base-package="com.daijunyi">
<!--表示不扫描 Repository注解的类-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
5、注解属性注入
- @AutoWired //根据属性类型自动注入
- @Qualifier //根据属性名称进行注入 但是得在@AutoWired注解使用的时候作为一个补充
- @Resource//可以根据类型注入,可以根据名称注入
以上三种是针对对象注入
- @Value //对基本类型进行注入
可以用来取属性文件中的值,前提是得在xml配置文件中引入了属性值文件。@Value(value = "${file.name}") private String name;
引入外部属性值文件<context:property-placeholder location="classpath:data.properties"></context:property-placeholder>
6、纯注解开发
- 创建配置类,替代xml配置文件 ```java package com.daijunyi.config;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;
@Configuration // 作为替换xml 配置类 @ComponentScan(basePackages = {“com.daijunyi”}) public class SpringConfig { }
2. 测试配置,就得使用AnnotationConfigApplicationContext来加载配置类
```java
package com.daijunyi.test;
import com.daijunyi.config.SpringConfig;
import com.daijunyi.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = (UserService) context.getBean("userService");
userService.userService();
}
}