spring java code配置_[译]17-spring基于java代码的配置元数据

spring还支持基于java代码的配置元数据。不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下。

spring基于java代码的配置元数据,可以通过@Configuration注解把一个声明为配置类;通过@Bean注解把一个新

创建的类交由spring容器来管理。在这种配置方式下,我们可以手动装配bean,也可以自动装配bean.我感觉在这种

方式下使用手动装配非常不爽,尤其是有多个配置类的时候。

下面看个例子:

1.新建包com.tutorialspoint.javacode,并在包中新建TextEditor.java、SpellChecker.java、HighLighter.java

//TextEditor.java

packagecom.tutorialspoint.javacode;importjavax.annotation.Resource;public classTextEditor {privateSpellChecker spellChecker;privateHighLighter highLighter;

@Resourcepublic voidsetHighLighter(HighLighter highLighter) {this.highLighter =highLighter;

}

@Resourcepublic voidsetSpellChecker(SpellChecker spellChecker) {this.spellChecker =spellChecker;

}publicTextEditor(){}publicTextEditor(SpellChecker spellChecker){this.spellChecker=spellChecker;

}publicTextEditor(HighLighter highLighter){this.highLighter=highLighter;

}public voidinit(){

System.out.println("init method invoked...");

}public voiddestroy(){

System.out.println("destroy method invoked...");

}public voidprint(){

System.out.println("spellChecker: "+spellChecker);

System.out.println("highLighter: "+highLighter);

}

}//SpellChecker.java

packagecom.tutorialspoint.javacode;public classSpellChecker {public voidcheckSpell(){

System.out.println("checking...");

}

}//HighLighter.java

packagecom.tutorialspoint.javacode;public classHighLighter {public voidhighlight(){

System.out.println("highlighting...");

}

}

2.在包com.tutorialspoint.javacode中新建如下三个配置类:

//AppConfig.java

packagecom.tutorialspoint.javacode;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Import;/***使用 @Configuration注解表明一个类属于配置类

*可以通过@Import注解引入其他配置类。

**/@Configuration

@Import(value={SpellCheckerConfig.class,HighLighterConfig.class})public classAppConfig {/*** 可以通过使用@Bean注解的name属性指定该bean在spring容器中的名字

* 使用initMethod属性指定该bean的初始化方法

* 使用destroyMethod属性指定bean的销毁方法*/@Bean(name="textEditor",initMethod="init",destroyMethod="destroy")publicTextEditor textEditor(){return newTextEditor();

}

}//HighLighterConfig.java

packagecom.tutorialspoint.javacode;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;

@Configurationpublic classHighLighterConfig {

@Bean(name="highLighter")publicHighLighter highLighter(){return newHighLighter();

}

}//SpellCheckerConfig.java

packagecom.tutorialspoint.javacode;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;

@Configurationpublic classSpellCheckerConfig {

@Bean(name="spellChecker")publicSpellChecker spellChecker(){return newSpellChecker();

}

}

3.在包com.tutorialspoint.javacode中新建MainApp.java.内容如下:

packagecom.tutorialspoint.javacode;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;importorg.springframework.context.support.AbstractApplicationContext;public classMainApp {public static voidmain(String[] args) {//基于java代码的容器的实现类要使用AnnotationConfigApplicationContext

AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

TextEditor te= (TextEditor) ctx.getBean(TextEditor.class);

te.print();//为了使bean的销毁方法起作用,注册JVM的退出事件

ctx.registerShutdownHook();

}

}

4.运行程序,检查结果:

2a17994b66c74e18008f01c369e752c8.png

但是基于java代码的配置元数据无法支持构造器参数方式的自动依赖注入,必须手动装配构造器参数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值