问题1:使用反射调用*serviceImpl时,使用注解的*Dao对象是空,解决办法在*serviceImpl类中,直接去spring容器获取bean
问题2;在一个类TestServiceImple的方法中(使用事务控制)中调用另外一个类SupplementaryBudgetServiceImpl的方法(事务控制),另外一个类SupplementaryBudgetServiceImpl的方法在数据库中不会commit。

@Service
@Transactional
public class TestServiceImple implements TestService{
public void test1() {

 String className="SupplementaryBudget";
 Properties properties=new Properties();
 String rootpath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
 BufferedReader read=new BufferedReader(new FileReader(rootpath+"properties/workFlowClass.properties"));
        properties.load(read);
        String FullclassName = properties.getProperty(className);
        Class   bean = Class.forName(FullclassName);

        //获取构造方法
        Constructor con=bean.getDeclaredConstructor(Integer.class,Character.class);
        //通过构造方法实例化
        Object obj=con.newInstance(id,state);

        //通过反射调用*serviceIMPl的updateState方法
        String FullclassNameServiceImpl = properties.getProperty(className+"ServiceImpl");
        Class   serviceImpl = Class.forName(FullclassNameServiceImpl);
        Method m=serviceImpl.getDeclaredMethod("updateState",bean);
        Object obj2=serviceImpl.getConstructor().newInstance();
        Object o=m.invoke(obj2, obj);//执行方法
}
}


---------------------------------------------------
@Service
@Transactional
public class SupplementaryBudgetServiceImpl   implements SupplementaryBudgetService{

@Autowired
private SupplementaryBudgetDao supplementaryBudgetDao;

public void updateState(SupplementaryBudget supplementaryBudget) {
        supplementaryBudgetDao.updateState(supplementaryBudget);
    }

}
问题1:supplementaryBudgetDao是null

解决:直接从Spring上下文中获取bean
@Service
@Transactional
public class SupplementaryBudgetServiceImpl   implements SupplementaryBudgetService{

    public static ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");

    public static Object getBean(String serviceName){
          return context.getBean(serviceName);
    }


public void updateState(SupplementaryBudget supplementaryBudget) {
    if(supplementaryBudgetDao==null){
        System.out.println("11111111");
        SupplementaryBudgetDao supplementaryBudgetDao = (SupplementaryBudgetDao)getBean("supplementaryBudgetDao");
        supplementaryBudgetDao.updateState(supplementaryBudget);
    }else{
        supplementaryBudgetDao.updateState(supplementaryBudget);
    }

}

问题2:
TestServiceImple的test1方法,使用反射,调用SupplementaryBudgetServiceImpl类的updateState方法,
结果:数据库有做sql的update语句,但是执行完没有进行事务的提交
解决方法:不要在事务控制的方法中,调用别的事务控制的方法。可以把test1的方法写到control层.

posted on 2017-09-17 13:36  2637282556  阅读(106)  评论(0编辑  收藏  举报