Spring手动装配之Spring 模式注解装配
:
1、模式注解
Spring Framework 注解 | 场景 | 起始版本 |
---|---|---|
@Repository | 数据仓储模式注解 | 2.0 |
@Component | 通用组件 | 2.5 |
@Service | 服务 | 2.5 |
@ControllerWeb | 控制器 | 2.5 |
@Configuration | 配置类 | 3.0 |
2、装配方式
1)<context:component-scan>
(since spring2.5)
<?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">
<!-- 注解驱动 -->
<context:annotation-config/>
<!-- 把被 @Component 或者其 Annotation 标记的类,注册为 Spring Bean -->
<context:component-scanbase-package="com.example.learndemo"/>
</beans>
2)@ComponentScan
方式(since spring 3.1)
@ComponentScan(basePackages="com.example.learndemo")
public class SpringConfiguration {
//...
}
3、自定义模式注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repository
public @interface ARepository {
String value() default"";
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ARepository
public @interface BRepository {
String value() default"";
}