首页 > 解决方案 > java - 如何用Java配置替换spring.xml配置?

问题描述

我正在使用旨在拦截和计时方法调用的 Spring AOP 教程。它使用 XML 配置,但我的公司框架使用 Java 配置。我是 Spring 新手,可以使用一些帮助将其从 xml 配置转换为 java 配置

spring.xml 内容如下:

    <?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:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!-- Enable AspectJ style of Spring AOP -->
    <aop:aspectj-autoproxy />

    <!-- Configure Employee Bean and initialize it -->
    <bean name="employee" class="com.journaldev.spring.model.Employee">
        <property name="name" value="Dummy Name"></property>
    </bean>

    <!-- Configure EmployeeService bean -->
    <bean name="employeeService" class="com.journaldev.spring.service.EmployeeService">
        <property name="employee" ref="employee"></property>
    </bean>

    <!-- Configure Aspect Beans, without this Aspects advices wont execute -->

    <bean name="employeeAroundAspect" class="com.journaldev.spring.aspect.EmployeeAroundAspect" />
    <bean name="employeeAnnotationAspect" class="com.journaldev.spring.aspect.EmployeeAnnotationAspect" />
    <bean name="employeeXMLConfigAspect" class="com.journaldev.spring.aspect.EmployeeXMLConfigAspect" />

    <!-- Spring AOP XML Configuration -->
    <aop:config>
        <aop:aspect ref="employeeXMLConfigAspect" id="employeeXMLConfigAspectID" order="1">
            <aop:pointcut expression="execution(* com.journaldev.spring.model.Employee.getName())" id="getNamePointcut"/>
            <aop:around method="employeeAroundAdvice" pointcut-ref="getNamePointcut" arg-names="proceedingJoinPoint"/>
        </aop:aspect>
    </aop:config>
    </beans>

如何在 java config 中启用 AspectJ 样式?

XML 块:

<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy />

这需要简单地在 Java 文件中导入 AspectJ 库吗?

如何初始化 Employee 和 Employee Service bean?

xml块:

<!-- Configure Employee Bean and initialize it -->
<bean name="employee" class="com.journaldev.spring.model.Employee">
    <property name="name" value="Dummy Name"></property>
</bean>

<!-- Configure EmployeeService bean -->
<bean name="employeeService" class="com.journaldev.spring.service.EmployeeService">
    <property name="employee" ref="employee"></property>
</bean>

<!-- Configure Aspect Beans, without this Aspects advices wont execute -->

<bean name="employeeAroundAspect" class="com.journaldev.spring.aspect.EmployeeAroundAspect" />
<bean name="employeeAnnotationAspect" class="com.journaldev.spring.aspect.EmployeeAnnotationAspect" />
<bean name="employeeXMLConfigAspect" class="com.journaldev.spring.aspect.EmployeeXMLConfigAspect" />

可能的 Java Config 块?不确定如何使用 Application.config 传递属性名称和值?将bean放入一个配置文件中可能更容易吗?

package com.journaldev.spring.*;

import org.springframework.context.annotation.*;


@Configuration

public class EmployeeConfig {    

    @Bean 
    public Employee employee(){
        return new Employee();    
 } 

    @Bean 
    public EmployeeService employeeService(){
        return new EmployeeService();    

}

    @Bean 
    public EmployeeAroundAspect employeeAroundAspect(){
        return new EmployeeAroundAspect();    

}

    @Bean 
    public EmployeeAnnotationAspect employeeAnnotationAspect(){
        return new EmployeeAnnotationAspect();    

}

    @Bean 
    public EmployeeXMLConfigAspect employeeXMLConfigAspect(){
        return new EmployeeXMLConfigAspect();    

}

我将如何配置 Spring AOP XML 块:

<!-- Spring AOP XML Configuration -->
<aop:config>
    <aop:aspect ref="employeeXMLConfigAspect" id="employeeXMLConfigAspectID" order="1">
        <aop:pointcut expression="execution(* com.journaldev.spring.model.Employee.getName())" id="getNamePointcut"/>
        <aop:around method="employeeAroundAdvice" pointcut-ref="getNamePointcut" arg-names="proceedingJoinPoint"/>
    </aop:aspect>
</aop:config>
</beans>

Java 配置块:

我是否创建了一个带有“Around Advice 对象”和“ProceedingJoinPoint”接口的“ConfigAspect.java”,该接口引用了 AspectPointCut.java 中的方法?

package com.journaldev.spring.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class ConfigAspect {

    public Object AroundAdvice(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("ConfigAspect:: Before invoking getName() method");
        Object value = null;
        try {
            value = proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("ConfigAspect:: After invoking getName() method. Return value="+value);
        return value;
    }
}

创建一个带有“Pointcut”注释的 AspectPointCut.java 文件,类似于 -

package com.journaldev.spring.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectPointcut {

    @Before("getNamePointcut()")
    public void loggingAdvice(){
        System.out.println("Executing loggingAdvice on getName()");
    }

    @Before("getNamePointcut()")
    public void secondAdvice(){
        System.out.println("Executing secondAdvice on getName()");
    }

    @Pointcut("execution(public String getName())")
    public void getNamePointcut(){}

    @Before("allMethodsPointcut()")
    public void allServiceMethodsAdvice(){
        System.out.println("Before executing service method");
    }

    //Pointcut to execute on all the methods of classes in a package
    @Pointcut("within(com.journaldev.spring.service.*)")
    public void allMethodsPointcut(){}

}

标签: springloggingmethodsaopspring-java-config

解决方案


推荐阅读