【问题标题】:Why suddenly spring asks me for aspectj?为什么春天突然向我要aspectj?
【发布时间】:2017-07-02 22:51:00
【问题描述】:

我目前正在为我的项目使用 Spring Security。 添加此 bean 后(记住我功能的用户):

    <b:bean id="jdbcTokenRepository" class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
        <b:property name="createTableOnStartup" value="false"/>
        <b:property name="dataSource" ref="dataSource"/>
    </b:bean>

用户令牌仍未保存。都是因为 'dataSource' 被配置为 autoCommit=false。

所以我做的第一件事是(真的很丑,因为我必须再创建一个类,但它正在工作):

 import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
 import org.springframework.transaction.annotation.Transactional;

 @Transactional
 public class TransactionalJdbcTokenRepositoryImpl extends JdbcTokenRepositoryImpl {}

对我来说更好的解决方案如下:

    <b:bean id="jdbcTokenRepository"
                class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
        <b:property name="createTableOnStartup" value="false"/>
        <b:property name="dataSource" ref="dataSource"/>
    </b:bean>

    <tx:advice id="tokenRepoAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <aop:config proxy-target-class="false" expose-proxy="false">
        <aop:advisor id="tokenRepoAdvisor"
                             advice-ref="tokenRepoAdvice"
                             pointcut="bean(jdbcTokenRepository)"/>
    </aop:config>

纯 XML 没有不必要的类:

但由于某种原因,spring 现在需要 aspectj!

Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
... 68 more

为什么?如果没有这个配置,我的 dao 层在 @Transactional 注释下工作得很好。如何避免添加 aspectj 库但仍保留 xml 配置?

【问题讨论】:

    标签: java spring transactions aop aspectj


    【解决方案1】:

    重读spring docs后,我找到了解决办法:

        <tx:advice id="tokenRepoAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>
    
        <bean id="jdbcTokenRepository"
                class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyInterfaces" alue="org.springframework.security.web.authentication.rememberme.PersistentTokenRepository"/>
            <property name="target">
                <bean class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
                    <property name="createTableOnStartup" value="false"/>
                    <property name="dataSource" ref="dataSource"/>
                </bean>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>tokenRepoAdvice</value>
                </list>
            </property>
    </bean>
    

    最初的问题是 JdbcTokenRepositoryImpl 的父类(它有最终方法 - 这就是为什么需要 aspectj)

    【讨论】:

    • 之所以需要 AspectJ 是因为使用了&lt;aop:advisor pointcut-"" /&gt; 切入点是用 AspectJ 解析的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2017-04-27
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多