spring context xml definition
<?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.xsd">
<bean id="userService" class="com.example.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="userDao" class="com.example.UserDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/users"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
</beans>
Explanation:
- <?xml version="1.0" encoding="UTF-8"?>
: Declares the XML version and encoding used in the document.
- <beans xmlns="http://www.springframework.org/schema/beans"
: Defines the root element <beans>
and sets the XML namespace for Spring beans.
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
: Declares the XML namespace for XML Schema instance.
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
: Specifies the location of the XML Schema that defines the structure of the XML document.
- <bean id="userService" class="com.example.UserService">
: Defines a bean named "userService" of class "com.example.UserService".
- <property name="userDao" ref="userDao"/>
: Sets the property "userDao" of the "userService" bean by referencing the bean with id "userDao".
- <bean id="userDao" class="com.example.UserDao">
: Defines a bean named "userDao" of class "com.example.UserDao".
- <property name="dataSource" ref="dataSource"/>
: Sets the property "dataSource" of the "userDao" bean by referencing the bean with id "dataSource".
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
: Defines a bean named "dataSource" of class "org.springframework.jdbc.datasource.DriverManagerDataSource".
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
: Sets the property "driverClassName" of the "dataSource" bean to the MySQL JDBC driver class.
- <property name="url" value="jdbc:mysql://localhost:3306/users"/>
: Sets the property "url" of the "dataSource" bean to the URL of the MySQL database.
- <property name="username" value="root"/>
: Sets the property "username" of the "dataSource" bean to the database username.
- <property name="password" value="password"/>
: Sets the property "password" of the "dataSource" bean to the database password.
```