【发布时间】:2018-05-27 11:05:11
【问题描述】:
我正在试验AspectJ。我试图在 String 类上应用方面。我将 Spring 配置文件创建为:
<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 ">
<!-- Enable @AspectJ annotation support -->
<aop:aspectj-autoproxy />
<!-- Employee manager -->
<bean id="employeeManager" class="com.test.advice.EmployeeManager" />
<!-- Logging Aspect -->
<bean id="loggingAspect" class="com.test.advice.LoggingAspect" />
<bean id="bean1" class="java.lang.String">
<constructor-arg value="abx" />
</bean>
</beans>
然后是一个 Aspect 类,
package com.test.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class LoggingAspect
{
@Around("execution(* java.lang.String.*(..))")
public void logAroundGetEmployee(ProceedingJoinPoint joinPoint) throws Throwable
{
System.out.println("works");
}
}
之后创建了一个带有 main 方法的类,例如:
package com.test.advice;
package com.test.advice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AspectJAutoProxyTest
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Customer.xml");
String pqr = (String) context.getBean("bean1");
pqr.trim();
}
}
在运行时它应该向控制台输出“works”。但它没有说,
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy5 cannot be cast to java.lang.String
at com.test.advice.AspectJAutoProxyTest.main(AspectJAutoProxyTest.java:13)
有什么问题?我们不能将代理应用于 java.lang 对象吗?请帮忙。
【问题讨论】:
标签: java spring aspectj aspect