IOCBean管理基于注解方式

基于注解方式IOCBean管理

1 简介

注解概念

  • 注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值)
  • 注解的位置:注解可以作用在类、属性、方法。
  • 使用目的:简化XML配置

IOC的两个步骤

  • 创建对象
  • 注入属性

2 基于注解方式创建对象

实现创建对象的注解

四个注解功能是一样的,都可以用来创建bean实例。

  • @Component 组件层
  • @Service service层
  • @Controller web层
  • @Repository DO层

使用注解创建对象bean的步骤

  1. 引入依赖。spring-aop(该jar包实现了注解)
  2. 开启组件扫描(context命名空间,添加扫描)
    1. 检测到组件扫描,会扫描包下的所有类。
    2. 可以通过filters过滤
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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">

<!--开启组件扫描
1. 如果扫描多个包,多个包之间使用逗号隔开
2. 扫描包的上层目录-->
<context:component-scan base-package="com.ykl"></context:component-scan>

</beans>
  1. 在类上添加注解,创建对象的bean
1
2
3
4
5
6
7
8
9
10
/**
* 与xml配置等价,value值可以省略不写
* 默认值是类名称,首字母小写。
*/
@Component(value="userService")
public class UserService {
public void add(){
System.out.println("service add ......");
}
}

context扫描设置

  • 使用自己配置的filter。use-default-filter=”false
    • context:include-filter只扫描指定注解
    • context:exclude-filter不扫描指定注解
1
2
3
4
5
6
7
<context:componet-scan base-package="com.atguigu" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<context:componet-scan base-package="com.atguigu" use-default-filters="false">
<context:exlude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

3 基于注解方式实现属性注入

实现属性注入的注解

  • @Autowired:根据属性类型进行自动装配
  • @Qualifier:根据属性名称进行注入
  • @Resource:可以根据属性类型、属性名称进行注入
  • @Value:注入普通类型的属性

@Autowire基于注解的属性注入步骤

  1. 在service和dao对象类上添加创建对象的注解。
  2. 在service中注入dao对象,在Service类中添加dao类型属性,在属性上使用注解。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Service
public class UserService {

@Autowired
private UserDo userDo;
public void add(){
System.out.println("service add ... ...");

userDo.add();
}
}

@Repository
public class UserDoImpl implements UserDo{
@Override
public void add() {
System.out.println("dao add ... ...");
}
}

@Qualifier根据名称进行注入

  • 必须跟Autowire配合使用。因为根据类型注入存在歧义的时候,使用名称进行区分(id)
1
2
3
4
5
6
7
8
@Autowired
@Qualifier(value = "userDoImpl")
private UserDo userDo;
public void add(){
System.out.println("service add ... ...");

userDo.add();
}

@Resource属性注入

Resource是javax扩展包中的注解

  • 默认值为根据类型进行注入
  • 根据名称进行注入,使用name属性
1
2
3
@Resource

@Resource(name="service")

@Value注入普通属性

1
2
@Value("abc")
pirvate String name;

4 完全注解开发

可以不用xml配置文件。

相关注解

  • @Configuration
  • @ComponentScan

完全注解开发流程

  • 创建配置类,代替xml配置文件。
1
2
3
4
5
@Configuration
@ComponentScan(basePackages = {"com.ykl"})
public class SpringConfig {

}
  • 编写测试类
1
2
3
4
5
6
7
@Test
public void testConfig(){
//加载配置类
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}