我想知道当你用方法注释方法时实际发生了@Transactional什么?当然,我知道Spring会将该方法包装在Transaction中.
但是,我有以下疑问:
注意:由于此机制基于代理,因此只会拦截通过代理进入的"外部"方法调用.这意味着'自调用',即目标对象中调用目标对象的其他方法的方法,即使被调用的方法被标记,也不会在运行时导致实际的事务
@Transactional!
资料来源:http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html
为什么只有外部方法调用才会在Transaction下而不是自调用方法?
我的印象是Spring AOP最适合用于特定于应用程序的任务,例如安全性,日志记录,事务等,因为它使用自定义Java5注释作为框架.然而,AspectJ似乎更加友好的设计模式.
任何人都可以强调在Spring应用程序中使用Spring AOP和AspectJ的各种优缺点吗?
我想监视具有指定注释的所有类的所有公共方法(比如@Monitor)(注意:注释在类级别).什么可能是一个可能的切入点?注意:我使用的是@AspectJ样式的Spring AOP.
我是Spring Transaction的新手.我发现的一些事情很奇怪,可能我确实理解了这一点.我希望在方法级别有一个事务处理,并且我在同一个类中有一个调用方法,看起来它不喜欢它,它必须从单独的类调用.我不明白这是怎么可能的.如果有人知道如何解决这个问题,我将不胜感激.我想使用相同的类来调用带注释的事务方法.
这是代码:
public class UserService {
@Transactional
public boolean addUser(String userName, String password) {
try {
// call DAO layer and adds to database.
} catch (Throwable e) {
TransactionAspectSupport.currentTransactionStatus()
.setRollbackOnly();
}
}
public boolean addUsers(List<User> users) {
for (User user : users) {
addUser(user.getUserName, user.getPassword);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在学习面向方面的编程概念和Spring AOP.我无法理解Pointcut和Joinpoint之间的区别 - 它们对我来说似乎都是一样的.Pointcut是您应用建议的地方,Joinpoint也是我们可以应用我们建议的地方.那有什么区别?
切入点的一个例子可以是:
@Pointcut("execution(* * getName()")
Run Code Online (Sandbox Code Playgroud)
什么是Joinpoint的例子?
我有下面的注释.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
Run Code Online (Sandbox Code Playgroud)
public class SomeAspect{
@Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")
public Object procede(ProceedingJoinPoint call) throws Throwable {
//Some logic
}
}
Run Code Online (Sandbox Code Playgroud)
public class SomeOther{
@MyAnnotation("ABC")
public String someMethod(String name){
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的类中我在@MyAnnotation中传递" ABC " .现在我如何在SomeAspect.java类的procede方法中访问" ABC "值?
谢谢!
ABC.java中定义了两种方法
public void method1(){
.........
method2();
...........
}
public void method2(){
...............
...............
}
Run Code Online (Sandbox Code Playgroud)
我想有AOP上的呼叫方法2.所以,我创建了一个类,AOPLogger.java,具有在方法提供方面功能的checkAccess
在配置文件中,我不喜欢的东西下面
<bean id="advice" class="p.AOPLogger" />
<aop:config>
<aop:pointcut id="abc" expression="execution(*p.ABC.method2(..))" />
<aop:aspect id="service" ref="advice">
<aop:before pointcut-ref="abc" method="checkAccess" />
</aop:aspect>
</aop:config>
Run Code Online (Sandbox Code Playgroud)
但是当调用我的method2时,不会调用AOP功能,即不会调用checkAccess方法的AOPLogger类.
我错过了什么?
问题很简单:有没有办法从apsectj ProceedingJoinPoint获取Method对象?
目前我在做
Class[] parameterTypes = new Class[joinPoint.getArgs().length];
Object[] args = joinPoint.getArgs();
for(int i=0; i<args.length; i++) {
if(args[i] != null) {
parameterTypes[i] = args[i].getClass();
}
else {
parameterTypes[i] = null;
}
}
String methodName = joinPoint.getSignature().getName();
Method method = joinPoint.getSignature()
.getDeclaringType().getMethod(methodName, parameterTypes);
Run Code Online (Sandbox Code Playgroud)
但我不认为这是要走的路......
我正在从xml配置转到annoations.我想转换一个会话范围的bean
<aop:scoped-proxy>
Run Code Online (Sandbox Code Playgroud)
这可以通过注释来完成,如果没有,我还能做些什么来保持该声明的有效性?
编辑: 我有兴趣在Spring 2.5中这样做
我们已经开始使用spring aop来实现我们应用程序的交叉切割(目前的安全性和缓存).
我的经理担心这项技术对性能的影响,尽管他完全理解这些技术带来的好处.
我的问题,您是否遇到过使用aop(特别是spring aop)引入的性能问题?
spring-aop ×10
spring ×9
java ×6
aspectj ×5
aop ×4
java-ee ×1
java-ee-7 ×1
performance ×1
pointcut ×1
spring-3 ×1
spring-jdbc ×1