Spring IOC for the MVC decoupling project preparation

sequence

Last week wrote two articles about MVC decoupling, in fact, in the Spring family bucket, spring ioc is also decoupled to exist, then we step by step transformation of this project it.

  • Article Portal:

MVC factory pattern to decouple combat

+ Singleton factory model combat

Create a project

Maven is a project to create a selection of projects, because you can automatically import SpringFramework package.

  • Once you've created a long way:

Create a maven project

Log Import analog source

Log into the simulation program to copy all under the java package paste to create a good project under Java package can be.

  • After pasting good is this:

importLoginDemo

Spring configuration relies

This step is particularly important, pull out the wrong warehouse, the warehouse may need to be replaced domestic sources.

  • Add spring dependence in pom.xml in the project root directory, the wait can be pulled completed.
<dependencies>
    <!-- springframework 依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.3.RELEASE</version>
    </dependency>

</dependencies>
  • Also need to add a configuration file, in resource package, the new file, named bean.xml, and which add the following code:

add bean.xml file

  • Then configure bean, a detailed look at the code:
<?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">
    <!--    把对象的创建交给 spring 来管理-->
    <bean id="accountService" class="wiki.laona.service.impl.AccountServiceImpl"/>

    <bean id="accountDao" class="wiki.laona.dao.impl.AccountDaoImpl"/>
</beans>

test

Front completed the preparatory work, the project is now under test build is successful, there needs to change it AccountDemo code.

  • Get Configuration project path through ApplicationContext,
public class AccountDemo {

    /**
     * 获取 spring 的 IOC 的核心容器,并根据 is 获取对象
     * @param args
     */
    public static void main(String[] args) {
//        IAccountService as = new AccountServiceImpl();
        // 获取核心容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        // 根据 id 获取 bean 对象
        IAccountService as = (IAccountService) applicationContext.getBean("accountService");
        IAccountDao ad = applicationContext.getBean("accountDao", IAccountDao.class);
        // 打印内存地址
        System.out.println(as);
        System.out.println(as);
        System.out.println(as);
        System.out.println(ad);
        System.out.println(ad);
        System.out.println(ad);
        // 保存账号
        as.saveAccount();
    }
}
  • Print results are as follows:
wiki.laona.service.impl.AccountServiceImpl@343f4d3d
wiki.laona.service.impl.AccountServiceImpl@343f4d3d
wiki.laona.service.impl.AccountServiceImpl@343f4d3d
wiki.laona.dao.impl.AccountDaoImpl@53b32d7
wiki.laona.dao.impl.AccountDaoImpl@53b32d7
wiki.laona.dao.impl.AccountDaoImpl@53b32d7
已保存账户

Process finished with exit code 0

Well, the hair problems, bin.

summary

In fact, here's project preparation, just before the factory mode implementations replaced by spring ApplicationContext Gets an instance of an object. But the whole project is the most popular spring into the architecture, sounds more tall on.


If a man unknown to concentrate swordsmanship ~!

Guess you like

Origin www.cnblogs.com/huaiangg/p/12508284.html