applicationContext.xml中的bean声明 [英] Declaration of beans in applicationContext.xml
问题描述
我有一个关于 applicationContext.xml中的类声明的问题
$ b applicationContext.xml 需要指定应用程序中的所有类?
例如
在我的小型Web应用程序中,我有一个实体类,服务类和 DAO 类。所以目前它被定义为
<! - Beans声明 - >
< bean id =Employees
class =net.test.model.Employees/>
<! - 用户服务声明 - >
< bean id =
EmployeeServiceclass =net.test.employees.service.EmployeeService>
< property name =employeesDAOref =EmployeeDAOImpl/>
< / bean>
<! - 用户DAO声明 - >
< bean id =EmployeeDAOclass =net.test.employee.dao.EmployeeDAOImpl>
< property name =sessionFactoryref =SessionFactory/>
< / bean>
所以如果我有多个实体,服务和dao类,我需要在 applicationContext.xml
?
任何对此的洞察都非常可观。
问候
更新1
ManagedBean
($ request)
public class EmployeesManagedBean implements Serializable {
并且我有注入注解
@Inject
EmployeesService employeesService;
在EmployeesService中,我有如
的注释@Named
public class EmployeesService实现IEmployeesService {
@Inject
EmployeesDAO employeesDAO;
@Override
公共列表<雇员> getEmployees(){
return getEmployeesDAO()。getEmployees();
}
最后在applicationContext.xml中我有
< context:component-scan base-package =net.test/>
现在问题在于当我运行我的应用程序时,我得到了
$ b $处的java.lang.NullPointerException net.test.managed.bean.EmployeesManagedBean.getEmpList(EmployeesManagedBean.java:53)
我是如何错误地获取空指针异常?
在applicationContext.xml中,我们是否需要指定
应用程序中的所有类?
没有。声明模型类像 net.test.model.Employees
是没有意义的,除非你需要一个原型来处理,比如初始化它的值,但是你可以直接在class
$ b
所以如果我有多个实体,服务和dao类,我需要
提及所有这些在applicationContext.xml中的类?
正如我之前解释的,实体类没有。服务和DAO是好的,因为大部分时间你需要注入服务的DAO(这就是DI的要点)。但是,当然,如果您创建了3个DAO,并且希望将它们注入到3服务中,那么请在您的Spring XML Bean定义文件中提及它们(您称之为 applicationContext.xml
)。
但有一点,您可能需要使用软件包扫描 autodetection和基于注解的配置以避免在Bean定义文件中写入所有内容。
I have a question regarding declaration of classes in applicationContext.xml
In applicationContext.xml do we need to specify all the classes from the application? E.g. In my small web application I have a Entity class, Service class and DAO class. So currently it is defined as
<!-- Beans Declaration -->
<bean id="Employees"
class="net.test.model.Employees" />
<!-- User Service Declaration -->
<bean id="
EmployeeService" class="net.test.employees.service.EmployeeService">
<property name="employeesDAO" ref="EmployeeDAOImpl" />
</bean>
<!-- User DAO Declaration -->
<bean id="EmployeeDAO" class="net.test.employee.dao.EmployeeDAOImpl">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
So if I have multiple entity, service and dao classes do I need to mention all those classes in applicationContext.xml
?
Any insight into this is highly appreciable.
Regards
Update 1
ManagedBean
@ManagedBean(name="empMB")
@Named
@Scope("request")
public class EmployeesManagedBean implements Serializable {
and I have Inject annotation
@Inject
EmployeesService employeesService;
In EmployeesService I have annotations like
@Named
public class EmployeesService implements IEmployeesService {
@Inject
EmployeesDAO employeesDAO;
@Override
public List<Employees> getEmployees() {
return getEmployeesDAO().getEmployees();
}
and finally in applicationContext.xml I have
<context:component-scan base-package="net.test" />
Now the problem is when I run my application I am getting
java.lang.NullPointerException at
net.test.managed.bean.EmployeesManagedBean.getEmpList(EmployeesManagedBean.java:53)
What am I doing wrongly to get nullpointer exception?
In applicationContext.xml do we need to specify all the classes from the application?
No. Declaring model classes like your net.test.model.Employees
is pointless unless you need a prototype to work with, something like initializing its values, but you can do this directly in the class and just instantiate it.
So if I have multiple entity, service and dao classes do I need to mention all those classes in applicationContext.xml?
As I explained before, entity classes no. Services and DAOs are ok because most of the time you need DAOs injected to the Services (and that's the point of DI). But of course, if you create 3 DAOs and you want them to be injected in your 3 Services, then mention them in your Spring XML Bean Definition file (what you call applicationContext.xml
).
But one thing, you may want to use package scanning autodetection and annotation based config to avoid writing everything in your Bean Definition File.
这篇关于applicationContext.xml中的bean声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!