- 60
- 0
- 约 5页
- 2016-10-13 发布于重庆
- 举报
通过在classpath自动方式把组件纳入spring容器中管理例子
1.此处要注入一股Functions类,采用扫描注入的组件的方式须该类有一个接口,先写出Functions接口FunctionsInterface.java
package?scan.impl;public?interface?FunctionsInterface {? ??public?abstract?void?show();}
2.Functions.java实现接口
package?scan.impl;import?javax.annotation.PostConstruct;import?javax.annotation.PreDestroy;import?org.springframework.stereotype.Service;@Service(functions)public?class?Functions?implements?FunctionsInterface {? ??/* (non-Javadoc)? ? ?* @see scan.impl.FunctionsInterface#show()? ? ?*/? ? @Override? ??public?void?show() {? ? ? ? System.out.println(我是扫描装配的!);? ? }? ??? ? @PostConstruct? ??public?void?init() {? ? ? ? System.out.println(我是初始化方法!);? ? }? ??? ? @PreDestroy? ??public?void?destroy() {? ? ? ? System.out.println(我是销毁方法!);? ? }}
3.测试类FunctionTest.java
package?scan.impl;import?javax.annotation.PostConstruct;import?javax.annotation.PreDestroy;import?org.springframework.stereotype.Service;@Service(functions)public?class?Functions?implements?FunctionsInterface {? ??/* (non-Javadoc)? ? ?* @see scan.impl.FunctionsInterface#show()? ? ?*/? ? @Override? ??public?void?show() {? ? ? ? System.out.println(我是扫描装配的!);? ? }? ??? ? @PostConstruct? ??public?void?init() {? ? ? ? System.out.println(我是初始化方法!);? ? }? ??? ? @PreDestroy? ??public?void?destroy() {? ? ? ? System.out.println(我是销毁方法!);? ? }}
4.运行结果:
我是初始化方法!
Finished creating instance of bean functions
Returning cached instance of singleton bean org.springframework.context.annotation.internalConfigurationAnnotationProcessor
Returning cached instance of singleton bean org.springframework.context.annotation.internalAutowiredAnnotationProcessor
Returning cached instance of singleton bean org.springframework.context.annotation.internalRequiredAnnotationProcessor
Returning cached instance of singleton bean org.springframework.context.annotation.internalCommonAnnotationProcessor
Returning cached instance of singleton bean org.springframework.context.annotation.ConfigurationClassPostPro
您可能关注的文档
最近下载
- 方洪波:美的凭什么建立“第二跑道”?.doc VIP
- 酒店礼仪培训ppt课件洲际.ppt VIP
- 中石油职称俄语翻译(中俄对照).pdf VIP
- T_CCAA 87-2024 数据中心基础设施检测与评价要求.pdf
- 2025届湖南师范大学附属中学高三高考模拟考试一英语试题(含答案与解析).docx VIP
- 2025届湖南师范大学附属中学高三高考模拟考试一英语试题(原卷版+解析版).docx VIP
- 固定翼无人机的组装.pptx VIP
- 中石油职称俄语考试教材.pdf
- 《交往与合作(第三版)》中职完整全套教学课件.pdf VIP
- 技工院校通用职业素质课程《交往与合作》教学设计方案.pdf VIP
原创力文档

文档评论(0)