@DeclareParents 引入新方法失败
@DeclareParents fail to introduce new method
各位,我是 Spring 的初学者,我在使用 @DeclareParents 时遇到了一些问题。我按照 Spring In Action 中的说明进行操作,但我无法实现介绍。
这是我的代码。
我先定义Interface performance
public interface Performance {
void perform();
}
然后实现接口。
@Component
public class OnePerformance implements Performance {
@Autowired
public OnePerformance(){
}
public void perform() {
System.out.println("The Band is performing....");
}
}
我想将方法 void performEncore() 引入 Performance。
所以我定义了接口,
public interface Encoreable {
void performEncore();
}
实施它,
@Aspect
public class DefaultEncoreable implements Encoreable{
public void performEncore() {
System.out.println("performEncore");
}
}
并介绍一下,
@Aspect
@Component
public class EncoreableIntroduction {
@DeclareParents(value="Performance+",
, defaultImpl=DefaultEncoreable.class)
public static Encoreable encoreable;
}
我使用自动配置,
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig {
}
但是,在测试时,我引入方法void performEncore()失败。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= ConcertConfig.class)
public class OnePerformanceTest {
@Autowired
private Performance performance;
@Test
public void perform() throws Exception {
performance.perform();
}}
而且我还启用了 AspectJ 支持插件。
仔细看了书和几篇博客,还是找不到原因。那么这个问题的原因可能是什么?提前致谢。
感谢 M. Deinum、NewUser 和 Wim Deblauwe。在他们的帮助下,我终于弄明白了问题所在。之前的JUnit4class不正确
解决这个问题的正确方法是将Performance转换为Encoreable,然后调用performEncore()方法。
代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= ConcertConfig.class)
public class OnePerformanceTest {
@Autowired
private Performance performance;
@Test
public void perform() throws Exception {
Encoreable encoreable = (Encoreable)(performance);
encoreable.performEncore();
}
}
各位,我是 Spring 的初学者,我在使用 @DeclareParents 时遇到了一些问题。我按照 Spring In Action 中的说明进行操作,但我无法实现介绍。
这是我的代码。 我先定义Interface performance
public interface Performance {
void perform();
}
然后实现接口。
@Component
public class OnePerformance implements Performance {
@Autowired
public OnePerformance(){
}
public void perform() {
System.out.println("The Band is performing....");
}
}
我想将方法 void performEncore() 引入 Performance。 所以我定义了接口,
public interface Encoreable {
void performEncore();
}
实施它,
@Aspect
public class DefaultEncoreable implements Encoreable{
public void performEncore() {
System.out.println("performEncore");
}
}
并介绍一下,
@Aspect
@Component
public class EncoreableIntroduction {
@DeclareParents(value="Performance+",
, defaultImpl=DefaultEncoreable.class)
public static Encoreable encoreable;
}
我使用自动配置,
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig {
}
但是,在测试时,我引入方法void performEncore()失败。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= ConcertConfig.class)
public class OnePerformanceTest {
@Autowired
private Performance performance;
@Test
public void perform() throws Exception {
performance.perform();
}}
而且我还启用了 AspectJ 支持插件。
仔细看了书和几篇博客,还是找不到原因。那么这个问题的原因可能是什么?提前致谢。
感谢 M. Deinum、NewUser 和 Wim Deblauwe。在他们的帮助下,我终于弄明白了问题所在。之前的JUnit4class不正确
解决这个问题的正确方法是将Performance转换为Encoreable,然后调用performEncore()方法。
代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= ConcertConfig.class)
public class OnePerformanceTest {
@Autowired
private Performance performance;
@Test
public void perform() throws Exception {
Encoreable encoreable = (Encoreable)(performance);
encoreable.performEncore();
}
}