Spring03-注解

89 阅读1分钟

注解取代xml的配置

注解实现bean标签

@Component (来个最简单的注解Demo)

本Demo需要 applicationContext.xml 配合

  • 扫包 可以扫描到所有的@Component

image.png

  • 创建一个Dog类

image.png

image.png

通过value属性设置bean的id,默认会讲类名的首字母小写形式作为bean的id

@Component 相当于 <bean>

ComponentScan:扫描 但是不咋提示

@Autowired:注入bean

其实如果项目中有类型 不写Autowired 也会自动注入

Qualifier:修饰符;限定符;限定词;修饰词;限定语

@Autowired:默认按照类型注入bean实例

  • 可以写在成员变量、setter、构造方法上 (不会调用setter)
  • 可以配合使用哦@Qualifier、需要设置bean的id
  • required设置false:找不到对应的bean时不会抛出异常

@Value

用于Stirng、基本类型、BigDecimal等类型 配合配置文件 @PropertySource

image.png

注解实现AOP

@Aspect:标记切面类

@Around:用来设置切入点

切入点复用 @Pointcut

image.png

image.png

image.png

注解实现事务管理

@Transactional

image.png

记得扫描包

image.png

@Configuration、@Bean

image.png

image.png

创建工厂的入口

context = new AnnotationConfigApplicationContext(Dog.class);
context = new AnnotationConfigApplicationContext("com.mj");

纯注解开发 - AOP

@EnableAspectJAutoProxy

纯注解开发 - 整合MyBatis

纯注解开发 - 事务管理

JSR注解

@Resource

@Inject

image.png

image.png

@PostConstruct @PreDestroy

property-placeholder的底层

image.png

component-scan

image.png

image.png

custom

<context:include-filter type="custom" expression="com.mj.filter.MyTypeFilter"/>

image.png

注解

image.png