如果我在Spring bean中的私有方法上有@Transactional -annotation,那么注释是否有效?
如果@Transactional注释是在公共方法上,则它可以工作并打开事务.
public class Bean {
public void doStuff() {
doPrivateStuff();
}
@Transactional
private void doPrivateStuff() {
}
}
...
Bean bean = (Bean)appContext.getBean("bean");
bean.doStuff();
Run Code Online (Sandbox Code Playgroud) 我尝试使用Spring 3.x的以下代码失败了BeanNotFoundException,它应该根据我之前问过的问题的答案 - 我可以使用Spring注入相同的类吗?
@Service
public class UserService implements Service{
@Autowired
private Service self;
}
Run Code Online (Sandbox Code Playgroud)
自从我用Java 6尝试这个以来,我发现以下代码工作正常:
@Service(value = "someService")
public class UserService implements Service{
@Resource(name = "someService")
private Service self;
}
Run Code Online (Sandbox Code Playgroud)
但我不明白它如何解决循环依赖.
编辑:
这是错误消息.OP在其中一个答案的评论中提到它:
由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[com.spring.service.Service]的匹配bean:期望至少有一个bean可以作为此依赖项的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
我有一个关于Spring的@Async注释以及如何正确使用它的问题.假设我有这些方法:
@Async
public void test(String param1) {
test2(param1, null);
}
@Async
public void test2(String param1, String param2) {
test3(param1, param2, null);
}
@Async
public void test3(String param1, String param2, String param3) {
// do some heavy work
}
Run Code Online (Sandbox Code Playgroud)
我是否需要@Async在所有三种方法上进行异步调用,或者只是将其置于test3实际上才能完成工作?