我正在创建一个带有spring aspectj的方面类,如下所示
@Aspect
public class AspectDemo {
@Pointcut("execution(* abc.execute(..))")
public void executeMethods() { }
@Around("executeMethods()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Going to call the method.");
Object output = pjp.proceed();
System.out.println("Method execution completed.");
return output;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想访问类abc的属性名称然后如何在方面类中访问它?我想在profile方法中显示abc类的name属性
我的abc课程如下
public class abc{
String name;
public void setName(String n){
name=n;
}
public String getName(){
return name;
}
public void execute(){
System.out.println("i am executing");
}
}
Run Code Online (Sandbox Code Playgroud)
如何访问方面类中的名称?
有点长的头衔,但这通常是个问题.
我想知道您是否认为执行以下操作是个好主意.
代替:
public void buyItem(int itemId, int buyerId) {
if (itemId <= 0) {
throw new IlleglArgumentException("itemId must be positive");
}
if (buyerId <= 0) {
throw new IlleglArgumentException("buyerId must be positive");
}
// buy logic
}
Run Code Online (Sandbox Code Playgroud)
我希望有类似的东西:
@Defensive("isPositive(#itemId, #buyerId)")
public void buyItem(int itemId, int buyerId) {
// buy logic
}
Run Code Online (Sandbox Code Playgroud)
你认为这是好/可怕/太花哨/太慢?如果你真的认为它很好我想用SpEL来实现它,那么有没有人有更好/更轻/更快的东西?
谢谢,
我花了几个小时试图让Twitter集成与Spring Social一起使用XML配置方法.我可以在Web上(以及在stackoverflow上)找到的所有示例始终使用示例中@Config显示的方法
无论出于何种原因,获取twitter API实例的bean定义都会引发AOP异常:
Caused by: java.lang.IllegalStateException: Cannot create scoped proxy for bean 'scopedTarget.twitter': Target type could not be determined at the time of proxy creation.
Run Code Online (Sandbox Code Playgroud)
这是我的完整配置文件:
<?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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/DefaultDB" />
<!-- initialize DB required to store …Run Code Online (Sandbox Code Playgroud) 如果我有一个适用于某个方法的建议(之前,之后或之后),这个建议会在调用该方法的同一个线程中运行吗?这是Spring的AOP.
我有一个返回对象的方法.我想使用spring AOP在我的日志中打印该对象的值.我怎样才能做到这一点?
请帮忙!
我们有一个在App Engine上运行并使用Spring框架的应用程序.最近我们添加了一些基于AOP的新功能.我们决定使用@AspectJ样式,因此我们添加<aop:aspectj-autoproxy>到基于XML的配置中并实现了各自的方面.一切都在开发服务器上运行正常,但是,当部署到云环境时,我们java.lang.StackOverflowError每次初始化应用程序时都会得到.
无法创建并导致错误的bean是使用@Configuration注释注释的配置类.看来基本上任何配置bean都可能导致错误.
您可以在下面看到相应的堆栈跟踪.
org.springframework.web.context.ContextLoader initWebApplicationContext: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectifyConfig' defined in URL [jar:file:/base/data/home/apps/{app-id}/8.372375422460842231/WEB-INF/lib/{app-name}-1.0-SNAPSHOT.jar!/{path-to-class}/ObjectifyConfig.class]: Initialization of bean failed; nested exception is java.lang.StackOverflowError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.createHandler(AppVersionHandlerMap.java:219)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.getHandler(AppVersionHandlerMap.java:194)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:134)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:446)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:435) …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring框架(4.0.5)和AspectJ进行AOP Logging开发一个java(JDK1.6)应用程序.
我的Aspect类工作正常,但我无法为构造函数对象创建切入点.
这是我的目标:
@Controller
public class ApplicationController {
public ApplicationController(String myString, MyObject myObject) {
...
}
...
..
.
}
Run Code Online (Sandbox Code Playgroud)
这是我的Aspect类:
@Aspect
@Component
public class CommonLogAspect implements ILogAspect {
Logger log = Logger.getLogger(CommonLogAspect.class);
// @Before("execution(my.package.Class.new(..)))
@Before("execution(* *.new(..))")
public void constructorAnnotatedWithInject() {
log.info("CONSTRUCTOR");
}
}
Run Code Online (Sandbox Code Playgroud)
如何为构造函数对象创建切入点?
谢谢
我正在学习春天,我有跟随
考虑以下bean定义:
<bean id="clientService" class="com.myapp.service.ClientServiceImpl" />
Run Code Online (Sandbox Code Playgroud)
现在考虑一下它被声明为切入点*的情况,目标是**clientService bean中的所有方法.
还要考虑ClientServiceImpl类实现3个接口
现在我知道,使用AOP的clientService bean是代理和该代理实现了所有的3个接口.
但是实现所有这三个接口的确切原因是什么?
所以在我看来,存在两种代理(如果我说错误的断言,请纠正我):
JDK代理:默认使用Spring(是真的吗?),我有一个接口,用于定义我想要代理的对象的方法.所以这个接口的具体实现是由代理包装的.所以当我在我的对象上调用一个方法时,我在它的代理上调用它.调用由最终执行方面的方法拦截器识别,然后执行调用的方法.
CGLIB代理:在我看来,代理扩展了包装对象的实现,为它添加了额外的逻辑功能
像这样的东西:

所以在我看来,Spring使用第一种基于接口实现的代理(是不是?):

我认为在AOP中,额外的逻辑由方法拦截器的实现来表示(是真的吗?),标准逻辑由定义在接口中的方法的实现来表示.
但是,如果之前的推理是正确的,我的疑问是:为什么我需要定义这些接口,并且由对象包装的对象实现这些接口?(我无法理解代理本身是否实现了这些接口).
为什么?究竟如何运作?
TNX
我正在研究Spring AOP,我有以下疑问.
据我所知,有两种方法可以将AOP行为实现到Java应用程序中:
AspectJ:这是第一个使用字节码修改进行方面编织的原始AOP技术.
Spring AOP:基于Java的AOP框架,使用AspectJ集成,使用动态代理进行方面编织.
我的疑问是:究竟什么意味着Spring AOP是一个带有AspectJ集成的AOP框架?那么它依次使用AspectJ?或者是什么?
第二个疑问与Spring AOP的Spring配置有关,我知道我可以这样做:
1)使用Java配置类:
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages=“com.example”)
public class AspectConfig {
...
}
Run Code Online (Sandbox Code Playgroud)
2)使用XML:
<beans>
<aop:aspectj-autoproxy />
<context:component-scan base-package=“com.example” />
</beans>
Run Code Online (Sandbox Code Playgroud)
因此,在两种配置中,Spring AOP似乎都使用AspectJ,因为在这些配置中我有:@EnableAspectJAutoProxy和
它究竟意味着什么?
spring-aop ×10
java ×7
spring ×7
aop ×5
aspectj ×3
coding-style ×1
java-ee ×1
junit4 ×1
unit-testing ×1