简体   繁体   English

在applicationContext.xml中声明bean

[英]Declaration of beans in applicationContext.xml

I have a question regarding declaration of classes in applicationContext.xml 我有一个关于applicationContext.xml中类声明的问题

In applicationContext.xml do we need to specify all the classes from the application? applicationContext.xml中 ,我们需要指定应用程序中的所有类吗? Eg In my small web application I have a Entity class, Service class and DAO class. 例如,在我的小型Web应用程序中,我有一个Entity类, Service类和DAO类。 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 ? 因此,如果我有多个实体,服务和dao类,是否需要在applicationContext.xml提及所有这些类?

Any insight into this is highly appreciable. 任何对此的见解都是高度可观的。

Regards 问候

Update 1 更新1

ManagedBean 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 在EmployeesService中,我有类似的注释

@Named
public class EmployeesService implements IEmployeesService {

@Inject
EmployeesDAO employeesDAO;

@Override
public List<Employees> getEmployees() {
    return getEmployeesDAO().getEmployees();
}

and finally in applicationContext.xml I have 最后在applicationContext.xml中,我有

<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 ? 我为得到nullpointer异常做错了什么?

In applicationContext.xml do we need to specify all the classes from the application? 在applicationContext.xml中,我们需要指定应用程序中的所有类吗?

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. 不需要。声明模型类(如net.test.model.Employees是没有意义的,除非您需要使用原型(如初始化其值)之类,但是您可以在类中直接执行并实例化它。

So if I have multiple entity, service and dao classes do I need to mention all those classes in applicationContext.xml? 因此,如果我有多个实体,服务和dao类,是否需要在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). 服务和DAO是可以的,因为大多数时候您需要将DAO注入到服务中(这就是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 ). 但是,当然,如果您创建3个DAO,并且希望将它们注入3个服务中,请在您的Spring XML Bean Definition文件(称为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. 但是有一件事,您可能希望使用包扫描自动检测和基于注释的配置来避免将所有内容写入Bean定义文件中。

The bean declaration in the application context is to register the bean in the application container. 应用程序上下文中的bean声明是在应用程序容器中注册bean。

If the bean is not registered, the container wouldn't be able to dependency inject any instance of that class, or apply interceptors to the object of the class. 如果未注册Bean,则容器将无法依赖注入该类的任何实例,或者将拦截器应用于该类的对象。

So unless the reference of bean is not required for any task like intercepting it or inject it, or create default singleton object of it, there is no need to declare it in the applicationContext.xml 因此,除非对bean进行任何引用(例如拦截它,注入它或创建它的默认单例对象)都不需要引用bean,否则无需在applicationContext.xml中声明它。

Hope this helps. 希望这可以帮助。

理想情况下是,另一种方式可以使用Spring注释,这样您就不必在xml中添加多个条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM