5、通过在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.ConfigurationClassPostProcessor.importAwareProcessor'
Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@469a9b65]
Returning cached instance of singleton bean 'lifecycleProcessor'
Publishing event in org.springframework.context.support.ClassPathXmlApplicationContext@5e7808b9: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@5e7808b9: startup date [Sun Dec 15 14:14:17 CST 2013]; root of context hierarchy]
getProperty("spring.liveBeansView.mbeanDomain", String)
Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
PropertySource [systemEnvironment] does not contain 'spring.liveBeansView.mbeanDomain'
PropertySource [systemEnvironment] does not contain 'spring_liveBeansView_mbeanDomain'
PropertySource [systemEnvironment] does not contain 'SPRING.LIVEBEANSVIEW.MBEANDOMAIN'
PropertySource [systemEnvironment] does not contain 'SPRING_LIVEBEANSVIEW_MBEANDOMAIN'
Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
Returning cached instance of singleton bean 'functions'
我是扫描装配的!
Closing org.springframework.context.support.ClassPathXmlApplicationContext@5e7808b9: startup date [Sun Dec 15 14:14:17 CST 2013]; root of context hierarchy
Publishing event in org.springframework.context.support.ClassPathXmlApplicationContext@5e7808b9: org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@5e7808b9: startup date [Sun Dec 15 14:14:17 CST 2013]; root of context hierarchy]
Returning cached instance of singleton bean 'lifecycleProcessor'
Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@585976c2: defining beans [functions,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Invoking destroy method on bean 'functions': public void scan.impl.Functions.destroy()
我是销毁方法!