Note that there are some explanatory texts on larger screens.

plurals
  1. POspring and @transactional, is this normal?
    text
    copied!<p>i wrote this simple example:</p> <pre><code>//file TestController.java public interface TestController { public List&lt;Test&gt; findAll(); } //file TestControllerImp.java @Controller public class TestControllerImp implements TestController{ @Autowired private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory=sessionFactory; } public List&lt;Test&gt; findAll() { return sessionFactory.getCurrentSession().createQuery("from Test").list(); } } //file TestService.java @Service public class TestService { @Autowired private TestController controller; public boolean flag=true; public void setController(TestController controller){ this.controller=controller; } @Transactional public List&lt;Test&gt; useController(){ flag=false; return controller.findAll(); } } </code></pre> <p>And this is my try:</p> <pre><code>TestService s1=context.getBean(TestService.class); TestService s2=context.getBean(TestService.class); List&lt;Test&gt; list=s1.useController(); System.out.println(s1.flag+" "+s2.flag); </code></pre> <p>Now the strange behaviour (im very new with spring):</p> <ol> <li>If i declare <code>@Transactional</code> the method "useController()", the output is: true true</li> <li>If i move <code>@Transactional</code> from <code>TestService</code> to <code>TestControllerImp</code>, and i declare "findAll()" with <code>@Transactional</code>, the output is: false false.</li> </ol> <p>Why i have this behaviour? I know by default <code>@Autowired</code> classes are singletone, but why in the first case the flag still remains true?</p> <p>Thanks all.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload