Spring xml学习2

通过注解配置 Bean。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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-4.1.xsd">
<!-- 需要开启context -->
<!-- 指定IOC容器的扫描的包 resource-pattern可以过滤特定的类-->
<!-- <context:component-scan base-package="ContextTest"

resource-pattern="repository/*.class"></context:component-scan>
-->
<!-- context:exclude-filter 子节点指定排除哪些指定表达式的组件 -->
<!-- context:include-filter 子节点指定包含哪些表达式的组件,该子节点需要 use-default-filters 配合使用-->
<!-- type="annotation 标注了XXXannotation 的类 type="assinable" 继承或扩展 XXXService 的类 -->
<context:component-scan base-package="ContextTest"
use-default-filters="false"> <!-- use-default-filters 是否使用默认filter 默认值 true -->
<!-- <context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/> -->

<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//这里包括主包和它的子包
package ContextTest;

public interface UserRespository {
void save();
}

package ContextTest.controller;

import org.springframework.stereotype.Controller;

@Controller //@Controller: 标识表现层组件
public class UserController {
public void execute() {
System.out.println("UserController");
}
}

package ContextTest.service;

import org.springframework.stereotype.Service;

@Service //@Service: 标识服务层(业务层)组件
public class UserService {
public void add() {
System.out.println("UserService");
}
}

package ContextTest.testObject;

import org.springframework.stereotype.Component;

@Component //@Component: 基本注解, 标识了一个受 Spring 管理的组件
public class TestObject {

}

package ContextTest.UserRespositoryImpl;

import org.springframework.stereotype.Repository;

import ContextTest.UserRespository;

@Repository("userRespository") //@Respository: 标识持久层组件

public class UserRespositoryImpl implements UserRespository {

@Override
public void save() {
System.out.println("UserRespository");
}

}

//主函数
package ContextTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import ContextTest.controller.UserController;
import ContextTest.service.UserService;
import ContextTest.testObject.TestObject;

public class Main {

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.annotation.xml");
TestObject test = (TestObject) ctx.getBean("testObject");
System.out.println(test);

UserController user = (UserController) ctx.getBean("userController");
System.out.println(user);

UserService service = (UserService) ctx.getBean("userService");
System.out.println(service);

UserRespository UserRespositor = (UserRespository) ctx.getBean("userRespository");
System.out.println(UserRespositor);
}

}

context:component-scan元素还会自动注册 AutowiredAnnotationBeanPostProcessor 实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性.
@Autowired 注解自动装配具有兼容类型的单个 Bean属性
构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解
默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
@Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
@Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.
@Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值
Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似
@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
@Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
建议使用 @Autowired 注解

原创技术分享,您的支持将鼓励我继续创作
0%