[英]How to hide service classes and prevent @Autowire?
我創建了一個框架,它有一些接口的默認實現。
問題:如何隱藏接口的任何默認實現,以便框架的用戶被迫使用DefaultService
接口,例如注入@Autowired DefaultService
?
interface DefaultService {
}
@Component
@Async
@ConditionalOnNotWebApplication
public class MyService1 implements DefaultService {
}
@Component
@Async
@ConditionalOnWebApplication
public class MyService2 implements DefaultService {
}
我的服務條件確保每個環境中始終只有一個服務連線。
作為@M。 Deinum在上面評論說實現服務包是私有的是正確的解決方案。 我不知道spring仍然會看到這些類,因為通常spring需要公共訪問注釋。
一種可能的解決方案似乎是為實現創建一個額外的@Configuration
類:
@Configuration
public class DefaultServiceConfiguration {
@Bean
@ConditionalOnNotWebApplication
public DefaultService getDefaultService1() {
return new MyService1();
}
@Bean
@ConditionalOnWebApplication
public DefaultService getDefaultService2() {
return new MyService2();
}
}
public interface DefaultService {
}
@Async
protected class MyService1 {
}
@Async
protected class MyService2 {
}
這樣,框架用戶只能看到DefaultService
接口。
以下對我有用。
創建自定義注釋:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by pyaswan on 8/18/17.
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotAutowiredBean {
}
BeanFactoryPostProcessor的:
import com.yaswanth.NotAutowiredBean;
import java.lang.reflect.Field;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationNotAllowedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
/**
* Created by pyaswan on 8/18/17.o
*/
@Component
public class NotAutowiredBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
try {
String[] illegalAutowiredBeans = beanFactory
.getBeanNamesForAnnotation(NotAutowiredBean.class);
String[] allBeans = beanFactory.getBeanDefinitionNames();
for (String beanName : allBeans) {
String beanClassName = beanFactory.getBeanDefinition(beanName).getBeanClassName();
if (beanClassName != null) {
Class<?> clazz = Class.forName(beanClassName);
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Autowired.class)) {
if (field.getType().isAnnotationPresent(NotAutowiredBean.class)) {
throw new BeanCreationNotAllowedException(beanName, "");
}
}
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
春豆:
interface DefaultService {
}
@NotAutowiredBean
@Component
@Async
@ConditionalOnNotWebApplication
public class MyService1 implements DefaultService {
}
@NotAutowiredBean
@Component
@Async
@ConditionalOnWebApplication
public class MyService2 implements DefaultService {
}
您需要使用@NotAutowiredBean
注釋實現。 NotAutowiredBeanFactoryPostProcessor
查看所有bean,迭代使用每個bean類的Autowired
批注注釋的字段。 它采用字段的類型,並在字段的類類型上查找NotAutowiredBean
注釋。 如果存在,它會拋出BeanCreationNotAllowedException
,這將使應用程序開始失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.