applicationContext.xml中的bean声明

编程入门 行业动态 更新时间:2024-10-27 02:19:47
本文介绍了applicationContext.xml中的bean声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个关于 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中,我有如

的注释

最后在applicationContext.xml中我有   

< context:component-scan base-package =net.test/>

现在问题在于当我运行我的应用程序时,我得到了

$ b $处的java.lang.NullPointerException net.test.managed.bean.EmployeesManagedBean.getEmpList(EmployeesManagedBean.java:53)

我是如何错误地获取空指针异常?

在applicationContext.xml中,我们是否需要指定应用程序中的所有类?

/ blockquote>

没有。声明模型类像 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声明

本文发布于:2023-10-28 17:47:31,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声明   applicationContext   xml   bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!