当前位置: 移动技术网 > IT编程>开发语言>Java > 在Spring中基于Java类进行配置的完整步骤

在Spring中基于Java类进行配置的完整步骤

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

javaconfig 原来是 spring 的一个子项目,它通过 java 类的方式提供 bean 的定义信息,在 spring4 的版本, javaconfig 已正式成为 spring4 的核心功能 。

本文将详细介绍关于spring中基于java类进行配置的相关内容,下面话不多说了,来一起看看详细的介绍吧

1 定义 bean

普通的 pojo 只要标注了 @configuration 注解,就可以为 spring 容器提供 bean 的定义信息。

@configuration
public class systemconfig {
 /**
  * 定义 bean,并实例化
  *
  * @return
  */
 @bean
 public userdao userdao() {
  return new userdao();
 }

 @bean
 public deptdao deptdao() {
  return new deptdao();
 }

 /**
  * 定义 userservice,并把之前定义的 userdao 与 deptdao 注入进来
  *
  * @return
  */
 @bean
 public userservice userservice() {
  userservice service = new userservice();
  service.setuserdao(userdao());
  service.setdeptdao(deptdao());
  return service;
 }
}

这个类的方法标注了 @bean 注解,即为定义 bean, bean 的类型由方法返回值的类型决定,名称默认和方法名同名,也可以通过入参显示指定 bean 名称,比如 @bean(name=”xxx”)。 @bean 所标注的方法体提供了 实例化 bean 的逻辑 。

以上配置和下面的 xml 是等效的:

<bean id="userdao" class="net.deniro.spring4.conf.userdao"/>
<bean id="deptdao" class="net.deniro.spring4.conf.deptdao"/>
<bean id="userservice" class="net.deniro.spring4.conf.userservice"
p:userdao-ref="userdao" p:deptdao-ref="deptdao"/>

基于 java 类的配置方式和基于 xml 或者基于注解的配置方式相比——

  • java 类的配置方式通过代码编程的方式,可以更加灵活地实例化 bean 和装配 bean 之间的关系。
  • xml 或者基于注解的方式都是通过声明来定义配置的,所以灵活性上要逊一些,但在配置上更简单 。

因为 @configuration 注解类本身已经标注了 @component,所以这些类可以像那些普通的 bean 一样被注入到其他的 bean 中。

@configuration
public class applicationconfig {
 @autowired
 private systemconfig systemconfig;
 @bean
 public authorityservice authorityservice() {
  authorityservice service = new authorityservice();
  service.setuserdao(systemconfig.userdao());
  service.setdeptdao(systemconfig.deptdao());
  return service;
 }
}

spring 会对配置类中所有标注了 @bean 的方法使用 aop 增强,引入 bean 的生命周期管理逻辑。比如上面的 systemconfig.userdao(),它返回的是对应 bean 的单例。

在 @bean 中,我们还可以通过标注 @scope 注解来控制 bean 的作用范围:

@scope("prototype")
@bean
public deptdao deptdao() {
 return new deptdao();
}

这样每次调用 deptdao() 方法都会返回一个新的实例:

assertnotsame(authorityservice.getdeptdao().hashcode(),authorityservice
    .getdeptdao().hashcode());

注意: 使用基于 java 类进行配置,类路径下必须有 spring aop 与 cglib 库。

2 启动 spring 容器

2.1 只使用 @configuration 类

可以使用 annotationconfigapplicationcontext 类的构造函数传入标注了 @configuration 的 java 类来启动 spring 容器 。

applicationcontext context=new annotationconfigapplicationcontext(systemconfig
  .class);
userservice userservice= (userservice) context.getbean("userservice");
assertnotnull(userservice);

如果存在多个 @configuration 配置类,那么可以 annotationconfigapplicationcontext 中注册它们,然后再通过刷新容器应用这些配置类:

annotationconfigapplicationcontext context=new annotationconfigapplicationcontext();

//注册多个配置类
context.register(systemconfig.class);
context.register(applicationconfig.class);

//刷新容器(应用这些配置类)
context.refresh();

applicationconfig config=context.getbean(applicationconfig.class);
assertnotnull(config);

也可以通过 @import 将多个配置类组装到一个配置类中,然后仅需注册这个组装好的配置类 ,即可启动容器:

@configuration
@import(systemconfig.class)
public class applicationconfig2 {
 @autowired
 private systemconfig systemconfig;
 @bean
 public authorityservice authorityservice() {
  authorityservice service = new authorityservice();
  service.setuserdao(systemconfig.userdao());
  service.setdeptdao(systemconfig.deptdao());
  return service;
 }
}

单元测试:

annotationconfigapplicationcontext context=new annotationconfigapplicationcontext(applicationconfig2.class);

applicationconfig2 config=context.getbean(applicationconfig2.class);
assertnotnull(config);
final authorityservice authorityservice = config.authorityservice();
assertnotnull(authorityservice.getdeptdao());

assertnotsame(authorityservice.getdeptdao().hashcode(),authorityservice
  .getdeptdao().hashcode());

2.2 使用 xml 文件引用 @configuration 类的配置

标注了 @configuration 的配置类也是一个 bean,所以它也可以被 spring 的 <context:component-scan> 扫描到 。 因此如果希望将配置类组装到 xml 的配置文件中,并通过 xml 的配置文件启动 spring,那么仅需要在 xml 中通过 <context:component-scan> 扫描到相应的配置类即可 。

<context:component-scan base-package="net.deniro.spring4.conf"
  resource-pattern="applicationconfig2.class"
  />

2.3 在 @configuration 类中引用 xml 文件的配置

在 @configuration 配置类中可以直接通过 @importresource 引入 xml 的配置文件,这样就可以直接通过 @autowired 引用 xml 配置文件中定义的 bean。

配置文件:

<bean id="groupdao" class="net.deniro.spring4.conf.groupdao"/>
<bean id="roledao" class="net.deniro.spring4.conf.roledao"/>

@configuration 类:

@importresource("classpath:beans5-11.xml")
@configuration
public class serviceconfig {
 @bean
 @autowired
 public relationservice relationservice(groupdao groupdao,roledao roledao){
  relationservice service=new relationservice();
  service.setgroupdao(groupdao);
  service.setroledao(roledao);
  return service;
 }
}

单元测试:

annotationconfigapplicationcontext context=new annotationconfigapplicationcontext
  (serviceconfig.class);
serviceconfig config=context.getbean(serviceconfig.class);
assertnotnull(config);
relationservice service=config.relationservice((groupdao) context.getbean
    ("groupdao"),
  (roledao) context
  .getbean
    ("roledao"));
assertnotnull(service.getroledao());

只要这些不同形式 bean 的定义信息能够加载到 spring 容器中,那么 spring 就可以智能的完成 bean 之间的装配 。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网