1、支持通过Spring的xml配置文件来制定要获取注解类的包,如何获取配置文件的参数:
/**
* 通过依赖注入获取配置文件中的属性值
* @param basePackages
*/
@Resource
public void setBasePackages(String... basePackages) {
this.basePackages = basePackages;
}
2、Spring的xml配置文件:
<bean class="com.lvbey.service.ServiceBean" scope="singleton">
<!-- 自动扫描的包 -->
<property name="basePackages">
<list>
<value>com.xxxx.service</value>
<value>com.wwww.service</value>
</list>
</property>
</bean>
3、由于在某些情况下,我们不想把拥有@Service的某些类(少数部分)获取出来,我们可以加上一个自定义注解:@NotAutowired2Service
所以,我们需要自己写一个自定义的注解类:
/**
* 在某个类上添加了该注解后,将不会自动注入到需要被获取的service中
* @author Lvbey
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotAutowired2Service {
}
package com.lvbey.service;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.osgi.service.component.annotations.Component;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import com.lvbey.service.util.ClassUtil;
/**
* @author Lvbey
*/
@Component //交给spring管理方便其他类获取该类对象
@Scope(value="singleto")//单例
public class ServiceBean implements ApplicationContextAware{
private ApplicationContext applicationContext;
/**
* 获取到的类的实例对象
*/
private Map<String, Object> classInstances;
/**
* 待扫描的基础包名
*/
private String[] basePackages;
/**
* 通过依赖注入获取配置文件中的属性值
* @param basePackages
*/
@Resource
public void setBasePackages(String... basePackages) {
this.basePackages = basePackages;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void init(){
if(this.applicationContext == null){
return;
}
if(this.classInstances == null){
this.classInstances = new HashMap<String,Object>();
}
//获取有NotAutowired2Service注解的实例
Map<String, Object> beansWithNotAutowired2ServiceMap = this.applicationContext.getBeansWithAnnotation(com.lvbey.service.annotation.NotAutowired2Service.class);
Map<String, Object> beansWithAnnotationMap = this.applicationContext.getBeansWithAnnotation(org.springframework.stereotype.Service.class);
Class<? extends Object> clazz = null;
for(Map.Entry<String, Object> entry : beansWithAnnotationMap.entrySet()){
clazz = entry.getValue().getClass();//获取到实例对象的class信息
Class<? extends Object> [] interfaces = clazz.getInterfaces();
for(Class<? extends Object> aInterface : interfaces){
String aInterfaceName = aInterface.getName();//接口的完整名
for(String packageName : this.basePackages){
if(aInterfaceName.startsWith(packageName)){//如果这个接口是在指定的包下
//接口实例名(只是将接口的首字母换成小写)
String aInterfaceSimpleName = ClassUtil.getDefaultInstanceName(aInterface);
//如果这个接口没有NotServiceBean注解
if(beansWithNotAutowired2ServiceMap.containsValue(entry.getValue())){
System.out.println(entry.getValue() + " has NotAutowired2Service Annotation");
}else{
classInstances.put(aInterfaceSimpleName, entry.getValue());
}
}
}
}
}
}
public Map<String, Object> getClassInstances() {
if(this.classInstances == null){
init();
}
return this.classInstances;
}
}
里面用到了一个util工具类:
public class ClassUtil {
/**
* 根据这个类来获取默认的实例名(默认:将首字母换成小写)
*
* @param clazz
* 类信息
* @return 默认的实例名
*/
public static String getDefaultInstanceName(Class<?> clazz) {
if (clazz == null) {
return null;
}
String className = clazz.getSimpleName();
String firsrLowerChar = className.substring(0, 1).toLowerCase();
className = firsrLowerChar + className.substring(1);
return className;
}
}
然后怎么使用这个类呢?很简单,在你需要使用的那个类里面加上Spring的自动注入就行:
@Autowired
private ServiceBean serviceBean;
serviceBean.getClassInstances() ;//获取得到的map集合,剩下的想干啥干啥
对于不想写代码,想直接使用的看官。。。。。。轻戳下载源码.jar