<?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:jdbc="http://www.springframework.org/schema/jdbc"  
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" 
default-lazy-init="true"> <description>Spring公共配置 </description> <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 --> 
<context:component-scan base-package="com.zz.auth"> 
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan> <!-- Jpa Entity Manager 配置 --> 
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
<property name="dataSource" ref="dataSource"/> 
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/> 
<property name="packagesToScan" value="com.zz.auth.entity"/> 
<property name="jpaProperties"> 
<props> 
<!-- 命名规则 My_NAME->MyName --> 
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
</props> 
</property> 
</bean> <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
<property name="database" value="SQL_SERVER" />  
        <property name="showSql" value="true" /> 
</bean> <!-- Jpa 事务配置 --> 
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/> 
</bean> <!-- Spring Data Jpa配置 --> 
<jpa:repositories base-package="com.zz.auth.dao"  transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/> 
   
<!-- 使用annotation定义事务 --> 
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- DBCP连接池 --> 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
destroy-method="close"> 
<property name="driverClassName" value="${jdbc.driver}" /> 
<property name="url" value="${jdbc.url}" /> 
<property name="username" value="${jdbc.username}" /> 
<property name="password" value="${jdbc.password}" /> 
<property name="defaultAutoCommit" value="false" /> 
</bean> 
</beans> 
这事原来的啊!代码!我想加入aop切面作为才做日志!不知道怎么下手!请高手讲解一下。springjpaaop

解决方案 »

  1.   

    <aop:config>
        <!-- 配置aspect切面类 -->
    <aop:aspect ref="userAspect">
        <!-- 配置pointcut,即切入点,对哪些类的哪些方法起到AOP的作用 -->
    <aop:pointcut id="userServiceMethods"
    expression="execution(* org.wiki.spring.service.UserServiceImpl.*(..))" />
    <!-- 配置advice,即Aspect类中的logging()方法,这里采用在业务方法执行前进行拦截 -->
    <aop:before method="logging" pointcut-ref="userServiceMethods" />
    </aop:aspect>

    </aop:config>
    这么加上可以吗?
      

  2.   

    但是我这么加上报错的啊!org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
      

  3.   

    首先写个切面(Aspect),然后配置一个切入点和通知类型就可以了,当初我才写的时候也是折腾了一部分时间,后面自己看来一下文档,很快就会了。
      

  4.   

    <?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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
    default-lazy-init="true"> <description>Spring公共配置 </description> <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
    <context:component-scan base-package="com.zz.auth">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan> <!-- Jpa Entity Manager 配置 -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
    <property name="packagesToScan" value="com.zz.auth.entity"/>
    <property name="jpaProperties">
    <props>
    <!-- 命名规则 My_NAME->MyName -->
    <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
    </props>
    </property>
    </bean>

    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="database" value="SQL_SERVER" />  
            <property name="showSql" value="true" />
    </bean> <!-- Jpa 事务配置 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean> <!-- Spring Data Jpa配置 -->
      <jpa:repositories base-package="com.zz.auth.dao"  transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
       
    <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

    <context:property-placeholder
    ignore-resource-not-found="true" location="classpath*:/application.properties" /> <!-- DBCP连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="defaultAutoCommit" value="false" />
    </bean>


    <!-- 配置aspect切面类 -->
    <bean id="userAspect" class="org.wiki.spring.aspect.Aspect" />    <!-- 配置AOP -->
    <aop:config>
        <!-- 配置aspect切面类 -->
    <aop:aspect ref="userAspect">
        <!-- 配置pointcut,即切入点,对哪些类的哪些方法起到AOP的作用 -->
    <aop:pointcut id="userServiceMethods"
    expression="execution(* com.zz.auth.service.impl.*.*(..))" />
    <!-- 配置advice,即Aspect类中的logging()方法,这里采用在业务方法执行前进行拦截 -->
    <aop:before method="logging" pointcut-ref="userServiceMethods" />
    </aop:aspect>

    </aop:config>
    </beans>
    我这么配置后!原来的login都登陆不进去了!为什么啊?