这是我的服务等级:
@Validated
public final class SenderImpl implements Sender {
private static final Logger LOGGER = LoggerFactory.getLogger(SenderImpl.class);
private final ExchangeService exchangeService;
private final IntegratorService integratorService;
@Autowired
public SenderImpl(ExchangeService exchangeService, IntegratorService integratorService) {
this.exchangeService = exchangeService;
this.integratorService = integratorService;
}
@Override
public String send(@NotNull String rawData, @NotNull String channelName) throws ClientException {
(...)
return rawMessage;
}
}
这是我对此课程的IT测试,需要自动连接 ApplicationContext :
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class SenderTestIT {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private Sender sender;
@MockBean
private SecurityHelper securityHelper;
@Before
public void setup() {
(...)
}
@Test
public void testSending() throws ClientException {
// given
// when
String response = sender.send("...", "...");
// then
assertThat(response).isNotNull();
}
}
运行后出现以下错误:
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class com.projects.pl.services.SenderImpl
at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:565)
at org.springframework.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.springframework.aop.framework.CglibAopProxy$ClassLoaderAwareUndeclaredThrowableStrategy.generate(CglibAopProxy.java:1004)
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329)
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
这可能是由我的 SenderImpl 类中的 final 修饰符引起的。这是好方法吗?如果是,那么有什么解决方法?
答案 0 :(得分:1)
不要将您的SenderImpl
课程定下来。
您的日志跟踪显示Spring正在使用CGLIB
。
CGLIB
将扩展SenderImpl
成为代理。
CGLIB
正常运行的要求是使您的班级不最终。
因此例外
Cannot subclass final class com.projects.pl.services.SenderImpl
您可以阅读有关CGLIB here的更多信息