Reputation: 42957
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
Upvotes: 0
Views: 911
Reputation: 41200
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. :)
Upvotes: 1
Reputation: 28706
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
Upvotes: 0