java 注解获取配置_java中借助Spring获取所有带有指定注解的接口、类、对象-续集...

1、支持通过Spring的xml配置文件来制定要获取注解类的包,如何获取配置文件的参数:

/**

* 通过依赖注入获取配置文件中的属性值

* @param basePackages

*/

@Resource

public void setBasePackages(String... basePackages) {

this.basePackages = basePackages;

}

2、Spring的xml配置文件:

com.xxxx.service

com.wwww.service

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 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();

}

//获取有NotAutowired2Service注解的实例

Map beansWithNotAutowired2ServiceMap = this.applicationContext.getBeansWithAnnotation(com.lvbey.service.annotation.NotAutowired2Service.class);

Map beansWithAnnotationMap = this.applicationContext.getBeansWithAnnotation(org.springframework.stereotype.Service.class);

Class extends Object> clazz = null;

for(Map.Entry 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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值