【发布时间】:2016-02-08 13:45:01
【问题描述】:
我开发网络应用程序,需要存储重量级文件并为此目的使用 Apache FTP 服务器。当新用户注册他的帐户时,必须在远程服务器上创建以他的用户名命名的文件夹。为了建立连接,在执行 UserCreatingServiceImpl.createUser() 方法之前,我使用 Spring AOP:
@Component
@Aspect
public class RemoteServerConnectionEstablisher {
private static boolean connectionEstablished = false;
@Autowired
private RemoteServerConnector serverConnector;
@Pointcut("execution(* com.storehouse.business.services.impl.UserCreatingServiceImpl.createUser(..)) ||"
+ " execution (* com.storehouse.business.services.impl.ItemCreatingServiceImpl.createItem(..)) ||"
+ "execution (* com.storehouse.business.services.impl.FileDownloadingServiceImpl.downloadFile(..))")
public void pointcut() {
}
@Before("pointcut()")
public void establishConnection(JoinPoint jp) {
if (!connectionEstablished) {
if (serverConnector.connectToRemoteServer()) {
connectionEstablished = true;
}
}
}
@After("pointcut()")
public void disconnect(JoinPoint jp) {
if (connectionEstablished) {
if (serverConnector.disconnect()) {
connectionEstablished = false;
}
}
}
}
这里是带有 createUser() 方法的服务类:
@Service
public class UserCreatingServiceImpl implements UserCreatingService {
@Autowired
private UserService userService;
@Autowired
private FTPClient ftpClient;
@Override
public boolean createUser(UserDto userDto) {
try {
ftpClient.makeDirectory(userDto.getUsername());
UserMapper userMapper = new UserMapper();
userService.persistUser(userMapper.dtoToEntity(userDto));
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
@Transactional
public void checkIfUsernameExist(String username) {
}
}
一切正常,直到我将@Transactional 方法添加到服务类:
@Transactional
public void checkIfUsernameExist(String username) {
}
现在不调用 Aspect 类的方法。你能解释一下原因吗。提前感谢您的帮助。
【问题讨论】:
-
你定义了自动扫描吗?
-
如果你的意思是
比是 -
你的切入点是错误的,你不应该以类为目标,但接口将
UserCreatingServiceImpl替换为UserCreatingService+(如果不在impl包中,也将其删除。跨度> -
是的,你是对的。我已经更改了切入点,现在一切正常。
标签: java spring spring-aop spring-aspects