Declaring beans in applicationContext.xml

I have a question regarding class declarations in applicationContext.xml

In applicationContext.xml do we need to specify all classes from the application? For instance. In my small web application, I have an Entity class, a Service class, and a DAO class. Therefore, it is currently 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 several classes of objects, services and Tao, do I need to specify all these classes in applicationContext.xml ?

Any understanding of this is very noticeable.

Hello

Update 1

Managedbean

 @ManagedBean(name="empMB") @Named @Scope("request") public class EmployeesManagedBean implements Serializable { 

and I have an 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 that when I run my application, I get

 java.lang.NullPointerException at net.test.managed.bean.EmployeesManagedBean.getEmpList(EmployeesManagedBean.java:53) 

What am I doing wrong to get a nullpointer exception ?

+4
source share
3 answers

In applicationContext.xml do we need to specify all classes from the application?

No. Declaring model classes such as net.test.model.Employees is pointless if you do not need a prototype to work, something like initializing its values, but you can do it directly in the class and just instantiate it.

So, if I have several classes of objects, services and Tao, do I need to specify all these classes in applicationContext.xml?

As I explained earlier, there are no entity classes. Services and DAO are fine, because most of the time you need DAOs that are entered into Services (and that is the DI point). But, of course, if you create 3 DAOs and want them to be injected into your 3 services, specify them in the Spring XML Bean definition file (what you call applicationContext.xml ).

But one thing, you can use auto - detect and annotation-based packet scanning to avoid recording everything in the Bean definition file.

+3
source

The bean declaration in the application context is to register the bean in the application container.

If the bean is not registered, the container will not be able to depend on the injection of any instance of this class or apply interceptors to an object of this class.

Therefore, if a reference to a bean is not required for any task, for example, to intercept it or enter it into it or create a singleton object by default, there is no need to declare it in applicationContext.xml

Hope this helps.

+2
source

Ideally, yes, another way might use Spring annotations so that you don't add multiple entries to xml.

0
source

All Articles