0

I am studyng Spring MVC and I have find this simple Hello World tutorial: http://www.tutorialspoint.com/spring/spring_hello_world_example.htm

In this tutorial the author creat a Bean Configuration file named Beans.xml, something like this:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

Using tis file the Spring Framework can create all the beans defined and assign them a unique ID as defined in tag. And I can use tag to pass the values of different variables used at the time of object creation.

Is this the well know Bean Factory?

My doubt is related to the following thing: in my previous example I don't use a Bean Configuration file to define my bean, but I use the annotation to define what is a Spring bean and how this bean work (for example I use @Controller annotation to say Spring that a class act as a Controller Bean)

Use the bean configuration file and use the annotation have the same meaning?

Can I use both?

For example, if I have to configure JDBC can I configure it inside beans.xml file and at the same time can I use annotaions for my Controller class?

Tnx

2 Answers 2

1

Yes you can do that thing. Find a example below where controller has written with annotation and sessionFactory and data source has been created as xml bean which is wired into services -

<beans ...>
    <!-- Controller & service base package -->
    <context:component-scan base-package="com.test.employeemanagement.web" />
    <context:component-scan base-package="com.test.employeemanagement.service"/>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           ...
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <!-- <value>com.vaannila.domain.User</value> -->
            <value>com.test.employeemanagement.model.Employee</value>
            ...
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            ...
        </props>
    </property>
    </bean>
    ...
</beans>

Services Example where SessionFactory is injected.

@Repository
public class EmployeeDaoImpl implements EmployeeDao {

    @Autowired
    private SessionFactory sessionFactory;
}

I hope it helps you. :)

Sign up to request clarification or add additional context in comments.

2 Comments

I think that what you would say me is pretty clear... I have only another question related to what you say me: When I creat a new Spring MVC project using STS\Eclipse I don't have this Beans.xml file but I have the following xml configuration file: web.xml and servlet-context.xml (this second one contain the component scan definition) Can I put these beans configurations (the database connetcion configurations) into servlet-context.xml or I need a Beans.xml file?
yes you can do that. Put all db confg into servlet-context which will be less maintenance.
0

You can use both bean configuration through xml and through annotation. You can even define your controller in xml configuration files.

Using the @Controller annotation allows you to use component scanning to find your bean. A Controller is a simple stereotype bean (that's why you can simply declare it your xml files).

More details about usage/semantic of @Controller here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.