spring dependency injection using bean configuration

One import dependency pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xingxue.spring</groupId>
    <artifactId>spring.day1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- Spring related dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <!-- The spring web module provides a listener to start the spring container -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
    </dependencies>

</project>

2. Configure beans.xml

<?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">


    <!--beans.xml file, you can think of it as a spring container:
        The bean tag configures the class A, which means that the class A is handed over to the Spring container for management.
        The creation of class A is created by the container, and new can no longer be used
    -->
   <bean id="cid" class="com.xingxue.spring.controller.UserController">
       <property name="userService" ref="sid"></property>

   </bean>
    <bean id="sid" class="com.xingxue.spring.service.UserService">
        <property name="userDao" ref="did"></property>        
    </bean>
    <bean id="did" class="com.xingxue.spring.dao.UserDao"></bean>


</beans>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324804113&siteId=291194637
Recommended