注解方式xml里面就不需要注册bean了。

构建注解需要 1、导入spring-aop-4.2.1.RELEASE.jar 包  2、需要更换配置文件头,即添加相应的约束。

现在的Student类就要这样定义了,如下图

下面的Student类的School注入是以jdk方式的注入(javax.annotation.Resource 是jdk里面的类)

基于注解的DI(DI:Dependency Injection 依赖注入)

xml配置如下(就下面的一句话<context:component-scan base-package="com.bjpowernode.di01")

基于注解的DI(DI:Dependency Injection 依赖注入)

基于注解的DI(DI:Dependency Injection 依赖注入)

 

执行如下:
基于注解的DI(DI:Dependency Injection 依赖注入)

 

下面的Student类的School属性不使用以jdk方式的注入,使用spring自己的饿注解方式如下

Student.java

基于注解的DI(DI:Dependency Injection 依赖注入)

 

执行如下:

基于注解的DI(DI:Dependency Injection 依赖注入)

 

 

 

 Bean的生命始末@PostConstruct与@PreDestroy (构造之后,销毁之前)

 基于注解的DI(DI:Dependency Injection 依赖注入)

 执行如下:

基于注解的DI(DI:Dependency Injection 依赖注入)

 

 基于注解的DI-JavaConfig注解,使用这种方式applicationContext.xml 也必须要有

基于注解的DI(DI:Dependency Injection 依赖注入)

执行如下

基于注解的DI(DI:Dependency Injection 依赖注入)

 

相关文章:

  • 2021-08-20
  • 2021-08-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-04
  • 2022-02-13
  • 2023-03-02
  • 2021-11-03
  • 2021-07-20
  • 2021-05-26
相关资源
相似解决方案