Spring 中的自动装配为空
Autowired is null in Spring
我有一个 class TestServiceImpl
,它带有 @Service
和 @EnabledTransactionManagement
注释。
我在其中引用了 2 个 DAO 对象 @Autowired Service1DAO s1
和 @Autowired Service2DAO s2
。 Service1DAO
和 Service2DAO
class 用 @Repository
标注。
这些方法用 @Trasanction
注释,根据要求需要参数。
问题是:
我能够获取 s1
对象,但是当我尝试获取 s2
对象时,它显示为空。
它们由另一个定义。
服务class是:
@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 class是:
@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 包含提到的组件扫描包。
好的...错误已解决。
使用 new() 在控制器中创建的服务 class 对象,我正在寻找服务和 dao classes 中的对象。这是代码实现的错误,即使使用 Spring 仍然按照 java 的路径创建对象。
我有一个 class TestServiceImpl
,它带有 @Service
和 @EnabledTransactionManagement
注释。
我在其中引用了 2 个 DAO 对象 @Autowired Service1DAO s1
和 @Autowired Service2DAO s2
。 Service1DAO
和 Service2DAO
class 用 @Repository
标注。
这些方法用 @Trasanction
注释,根据要求需要参数。
问题是:
我能够获取 s1
对象,但是当我尝试获取 s2
对象时,它显示为空。
它们由另一个定义。
服务class是:
@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 class是:
@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 包含提到的组件扫描包。
好的...错误已解决。
使用 new() 在控制器中创建的服务 class 对象,我正在寻找服务和 dao classes 中的对象。这是代码实现的错误,即使使用 Spring 仍然按照 java 的路径创建对象。