Spring study notes: Realize the IOC and DI of the spring framework based on two configuration methods of XML files and annotations

First open IntelliJ IDEA, create a Maven project spring-lesson, delete the src folder, and only keep the pom file corresponding to maven dependencies. This project is used as the parent project, and the <packaging> node is added to the pom file. The content of the node is pom, indicating the construction Aggregate engineering, as follows:

 <groupId>com.winning</groupId>
  <artifactId>spring-lesson</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--表示构建聚合工程-->
  <packaging>pom</packaging>

Add a new module to the parent project. The module name is day1-spring. First, introduce spring-related dependencies, which are mainly spring-context dependencies, as follows:

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
</dependency>

Then add a package: com.winning.dao, add an interface TestDAO, which has a hello() method:

package com.winning.dao;

public interface TestDAO {
    void hello();
}
package com.winning.dao;

import org.springframework.stereotype.Repository;

@Repository
public class TestDAOImpl implements TestDAO {
    @Override
    public void hello() {
        System.out.println("-->dao层TestDAOImpl执行hello()方法!");
    }
}

Continue to add packages, com.winning.service, and create a TestService interface, which has a CallDaoMethod method:

package com.winning.service;

import com.winning.dao.TestDAO;

public interface TestService {

    void CallDaoMethod();
    void  setTestDAO(TestDAO testDAO);
}
@Service
public class TestServiceImpl implements TestService {

    @Autowired // 把spring容器中TestDAO类型实例注入到testDAO属性中
    TestDAO testDao;

    @Override
    public void CallDaoMethod() {

        System.out.println("-->service层TestServiceImpl执行CallDaoMethod()方法!");
        testDao.hello();
    }

    @Override
    public void setTestDAO(TestDAO testDAO) {
        this.testDao = testDAO;
    }

}

In the service layer code above, there is a property of type TestDAO. To ensure that this property can be injected, a set method must be provided for the property.


Next, let's implement the management of java bean objects through the xml configuration file, and add an applicationContext.xml file under the resources folder

<?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="testDao" class="com.winning.dao.TestDAOImpl" />
    <bean id="testService" class="com.winning.service.TestServiceImpl">
        <!--配置属性的依赖注入,前提是属性有set方法-->
        <property name="TestDAO" ref="testDao" />
    </bean>
</beans>

It should be noted here that the id must be unique, and the class node represents the class path, that is, the package name + class name. At the same time, when the DAO layer object is injected into the service layer, we use attribute injection, and the ref node corresponds to the attribute name.


Next, let's test, load our xml configuration file through the ClassPathXmlApplicationContext class, the parameter name is the xml file name, as follows:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Below we use this method to obtain the DAO layer and service layer objects respectively

//通过xml文件管理java bean对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");

        Object testDaoObj = context.getBean("testDao");
        System.out.println( testDaoObj );

        //TestService.class,表示返回指定Bean类型,无需用Object接收
        TestService testService = context.getBean("testService", TestService.class);
        testService.CallDaoMethod();

Run it to see the result:

It has been successful, and the space is limited. Let’s share the annotation implementation method next time. 

Guess you like

Origin blog.csdn.net/HaoNanEr1989/article/details/115427664