求帮助。。。
1.事物和切面配置都是采用AOP方式,在同一个方法调用中是否有冲突?
2.如下方法不能被代理警告,不知道该怎么解决,写法有问题吗?
3.输出不理解,怎么没有业务方法本身的输出?
配置部分:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="test*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pointcut_a" expression="execution(* com.base.control.*.*(..))"/>
<aop:advisor pointcut-ref="pointcut_a" advice-ref="txAdvice"/>
</aop:config>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="menu" class="com.base.control.Menu">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="testAOP" class="com.base.test.TestAOP"></bean>
<aop:config proxy-target-class="true">
<aop:pointcut id="pointcut" expression="execution(* com.base.control.Menu.test*(..))"/>
<aop:aspect id="myAOP" ref="testAOP">
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:around method="around" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
java代码:
public class Menu extends JdbcDaoSupport {
private Log log = LogFactory.getLog(Menu.class);
public void test2(){
log.info("业务方法开始执行:");
int rSize = getJdbcTemplate().update("update users set status='1'");
log.info("响应结果条数:"+rSize);
}
}
public class TestAOP {
private final Log log = LogFactory.getLog(TestAOP.class);
public void before(){
log.info("进入方法before()...");
}
public void around(){
log.info("进入方法around()...");
}
public void after(){
log.info("进入方法after()...");
}
}
@Test
public void testAop(){
log.info("测试spring aop ...");
BeanFactory bFactory = new ClassPathXmlApplicationContext(new String[]{"conf/applicationContext.xml", "conf/applicationContext-C.xml","conf/applicationContext-security.xml", "conf/applicationContext-test.xml"});
Menu menu = (Menu) bFactory.getBean("menu");
menu.test2();
}
输出:
[2015-04-16 14:36:58,171] INFO [main] com.base.test.JUnitTestClass(72)-> 测试spring aop ...
[2015-04-16 14:36:59,129] WARN [main] org.springframework.aop.framework.CglibAopProxy(261)-> Unable to proxy method [public final void org.springframework.jdbc.core.support.JdbcDaoSupport.setDataSource(javax.sql.DataSource)] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2015-04-16 14:36:59,129] WARN [main] org.springframework.aop.framework.CglibAopProxy(261)-> Unable to proxy method [public final javax.sql.DataSource org.springframework.jdbc.core.support.JdbcDaoSupport.getDataSource()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2015-04-16 14:36:59,129] WARN [main] org.springframework.aop.framework.CglibAopProxy(261)-> Unable to proxy method [public final org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.core.support.JdbcDaoSupport.getJdbcTemplate()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2015-04-16 14:36:59,130] WARN [main] org.springframework.aop.framework.CglibAopProxy(261)-> Unable to proxy method [public final void org.springframework.jdbc.core.support.JdbcDaoSupport.setJdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2015-04-16 14:36:59,131] WARN [main] org.springframework.aop.framework.CglibAopProxy(261)-> Unable to proxy method [public final void org.springframework.dao.support.DaoSupport.afterPropertiesSet() throws java.lang.IllegalArgumentException,org.springframework.beans.factory.BeanInitializationException] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2015-04-16 14:37:00,021] INFO [main] com.base.test.TestAOP(11)-> 进入方法before()...
[2015-04-16 14:37:00,022] INFO [main] com.base.test.TestAOP(15)-> 进入方法around()...
[2015-04-16 14:37:00,022] INFO [main] com.base.test.TestAOP(23)-> 进入方法after()...
------解决思路----------------------
final的方法需要使用cglib代理,jdk的代理是不行的,配置成<aop:config proxy-target-class="true">试试