开发者社区> 问答> 正文

Spring如何配置多个类-java报错

"

是这样的,我有个工程,有很多的类,我需要对每一个类的每一个方法进行织入,所以需要在xml中进行配置,请问有没有办法不用在xml中编写每一个bean呢,比如类似通过pattern

我从下面的网址下载了一个代码片段

http://www.mkyong.com/spring3/spring-aop-aspectj-in-xml-configuration-example/

其中的例子是这样的:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />

<bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl" />

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @Before -->
<aop:pointcut id="pointCutBefore"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />
<aop:before method="logBefore" pointcut-ref="pointCutBefore" />
<!-- @After -->
<aop:pointcut id="pointCutAfter"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />
<aop:after method="logAfter" pointcut-ref="pointCutAfter" />
<!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))" />
<aop:after-returning method="logAfterReturning" returning="result" 
pointcut-ref="pointCutAfterReturning" />

<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))" />
<aop:after-throwing method="logAfterThrowing" throwing="error" 
pointcut-ref="pointCutAfterThrowing"  />
<!-- @Around -->
<aop:pointcut id="pointCutAround"
expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))" />
<aop:around method="logAround" pointcut-ref="pointCutAround"  />
</aop:aspect>

</aop:config>

</beans>



如上面的代码片段:


customerBo这个bean是在这里配置的,那如果我有几十个bean的话(不想用interface,因为还需要定义),就需要写几十个配置,有没有办法通过包名,或者类似pattern的方式实现呢

"

展开
收起
montos 2020-06-01 20:53:22 443 0
1 条回答
写回答
取消 提交回答
  • "

    不用都写在xml配置文件中哈,可以使用注解

    spring的使用,可参考开源项目 : http://git.oschina.net/wangkang/llsfw

    希望能够帮到你.

    ######用注解呗,扫描包就可以了,一搜就能搜到很多教程######

    1、注解,方便省事;

    2、通配符;

    ######
    <context:component-scan base-package="com.*">  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
    ######注解……"
    2020-06-01 20:53:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载