通过在classpath自动方式把组件纳入spring容器中管理例子.docVIP

  • 60
  • 0
  • 约 5页
  • 2016-10-13 发布于重庆
  • 举报

通过在classpath自动方式把组件纳入spring容器中管理例子.doc

通过在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

文档评论(0)

1亿VIP精品文档

相关文档