Spring configuration

Spring configuration process

guide package

导入基本的核心包
- spring-beans-4.3.8.RELEASE.jar
- spring-context-4.3.8.RELEASE.jar
- spring-core-4.3.8.RELEASE.jar
- spring-expression-4.3.8.RELEASE.jar
- commons-logging-1.2.jar
- log4j-1.2.17.jar

Import constraints

Two ways:
1. Import the constraint file
2. Go to the official document to copy

First, import the constraint file

  1. Import constraint file in tool configuration

image

  1. Import constraints into desgin mode

image

The second, go to the official document to copy

  1. Open the xsd-configuration.html file from the downloaded tarball

    ..\spring-framework-4.3.8.RELEASE-dist\spring-framework-4.3.8.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
  2. Search for xmlns to see the constraints for the example, then copy
<?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 definitions here -->

</beans>

Create JavaBeans

  • Interface
/**
 * ICustomer接口
 */
public interface ICustomerDao {

    /**
     * 保存客户
     */
    void saveCustomer();
}
  • Impl
/**
 * ICustomerDao的实现类
 */
public class CustomerDaoImpl implements ICustomerDao {

    /* (non-Javadoc)
     * @see com.dao.ICustomerDao#saveCustomer()
     */
    @Override
    public void saveCustomer() {
        System.out.println("save Customer Success!");
    }

}

write configuration file

  • The name of the configuration file can be written at will, and you can choose the standard applicationContext.xml name
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入schema 
    约束的位置在:
        ..\spring-framework-4.3.8.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
    文件中。

    注意:
        导入schema约束,并且建议将spring-beans.xsd,增加使用的大版本信息:spring-beans-4.0.xsd
-->
<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-4.0.xsd">

        <!-- 配置客户dao实现类对象,说明:
        bean标签:配置javaBean对象
        id属性:指定对象的唯一标识名称
        class属性:指定类的全限定类名称(包名称+类名称) -->
        <bean id="customerDao" class="com.dao.impl.CustomerDaoImpl"></bean>

</beans>

test class

  • "classpath:" means that it is an identifier under the root path of the class, plus it is a usage habit. It can also be omitted.
public class Client {

    /**
     * ApplicationContext接口:
     *      1.它就相当于一个大工厂,提供了根据配置文件中的id属性值,获取对象
     * 
     *      2.实现类:
     *          ClassPathXmlApplication类,从类的根路径下加载配置文件(企业项目开发中,使用较多)
     *          FileSystemXmlApplication类,从文件系统(磁盘)上加载配置文件
     * @param args
     */
    public static void main(String[] args) {
        // 1.创建ClassPathXmlApplication对象
        // 注意这里的"classpath:"表示在类的根路径下,是一个标识符,加上是使用习惯。也可以不加。
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:appliactionContext.xml");

        // 2.从context中,获取客户dao对象
        // getBean方法:获取bean对象
        // name参数:配置文件中id属性值
        CustomerDao customerDao = (CustomerDao) context.getBean("customerDao");

        // 3.调用方法执行
        customerDao.saveCustomer();
    }
}
  • result output
save Customer Success!

Guess you like

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