我不明白为什么我的2测试相同的AOP结果。 =>测试1调用AOP(@After和@AfterThrowing),但测试2。 你能帮助我吗 ?我想直接使用spring inject而不用getBeans ...
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationcontext.xml" })
@TestExecutionListeners(listeners = {
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
@Component("Testing")
public class Testing {
private final static Logger logger = LoggerFactory
.getLogger(Testing.class);
@Autowired
ApplicationContext context;
@Test
public void testAOP() {
logger.info("\n\n ********* test 1 // AOP");
Testing testing = (Testing) context.getBean("Testing");
logger.info("Object 1 = " + testing.toString());
logger.info(testing.findBar());
}
@Test
public void testAOP2() {
logger.info("\n\n ********* test 2 // AOP");
logger.info("Object 2 = " + this.toString());
logger.info(this.findBar());
}
public String findBar() {
String tst = null;
//LEVE ERRREUR
if (tst.length() > 2)
logger.error("Impossible");
logger.error("Impossible");
return "tst";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- post-processors for all standard config annotations -->
<context:annotation-config />
<!-- List of packages containing Bean that uses Spring annotation -->
<context:component-scan base-package="fr" />
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:property-placeholder location="classpath:config.properties" />
-
INFO - Could not instantiate TestExecutionListener org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
INFO - Using TestExecutionListeners
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1ba9117e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@732c2a62]
INFO - Loading XML bean definitions from class path resource [applicationcontext.xml]
INFO - Refreshing org.springframework.context.support.GenericApplicationContext@22ef9844: startup date [Tue Dec 22 19:53:41 CET 2015]; root of context hierarchy
INFO - Loading properties file from class path resource [config.properties]
INFO - @After
INFO -
********* test 1 // AOP
INFO - @After
INFO - Object 1 = fr.tests.Testing@4248ed58
INFO - @After
INFO - ************* @AfterThrowing ************
INFO -
********* test 2 // AOP
INFO - Object 2 = fr.tests.Testing@faa3fed
INFO - Closing org.springframework.context.support.GenericApplicationContext@22ef9844: startup date [Tue Dec 22 19:53:41 CET 2015]; root of context hierarchy