Spring中的Autowired为null

时间:2016-07-15 16:37:33

标签: spring autowired

我有一个TestServiceImpl课程,其中注明了@Service@EnabledTransactionManagement注释。 我在其中引用了2个DAO对象@Autowired Service1DAO s1@Autowired Service2DAO s2Service1DAOService2DAO类使用@Repository注释进行了注释。 根据要求,方法使用@Trasanction和必需参数进行注释。

问题是: 我能够获得s1个对象,但是当我尝试获取s2对象时,它显示为null。 它们是彼此定义的。

服务班是:

@Service 
@Scope("prototype")
@EnabledTransactionManagement 
public class TestServiceImpl {
    @Autowired Service1DAO s1; 
    @Autowired Service2DAO s2;

    @Transation(readOnly = false, propogation = Propagation.REQUIRED_NEW)
    public String getXXX1(){
        s1.print();
    }

    @Trsanction(readOnly = false, propogation = Propagation.REQUIRED_NEW)`enter code here`
    public String getXXX2(){
        s2.write();
    }
}

DAO课程是:

@Repository
public class Service1DAO implements Service1{
    @PersistentContext
    EntityManager em;

    public String Print(){
        em.XXXXXX();
    }
}

@Repository
public class Service2DAO implements Service2{
    @PersistentContext
    EntityManager em;

    public String write(){
        em.XXXXXX();
    }
}

xml包含提到的组件扫描pakcage。

1 个答案:

答案 0 :(得分:0)

好的......错误已经解决。

使用new()在控制器中创建的Service类对象,我正在查找服务和dao类中的那些。它与代码实现有关,即使使用Spring仍然遵循java的路径来创建对象。