Spring trial and error notes (1)

Trial and error notes

Article Directory

tiny error

MultipartFile null operation cannot use "==", but should use MultipartFile built-in isEmpty() method, otherwise the purpose of null determination will not be achieved

It is recommended to pass two parameters new File (a1, a2) for File related files. A1 represents the directory where the file is located, and a2 represents the file name. Direct splicing may report an error. The file name will be regarded as the folder name. When judging whether the file exists, exists () will return to not exist, temporarily do not know the reason

Spring boot cannot inject static variables, and @Autowired for static variables does not work

Spring issues with C3P0 database connection pool injection

  1. Need to import 2-3 jar packages
C3P0 required packages

​ mchange-commons-java-0.2.19.jar This is an auxiliary package for the c3p0 database connection pool. If there is no such package, the system will report classnotfoundexception when it starts. This is a package separated after updating the c3p0-0.9.2 version, 0.9. At 1, it was still just a bag.

If this auxiliary package is not imported, no error will be reported when the bean is configured, but an error will be reported when the bean is instantiated by the container;

  1. xml configuration c3p0:
  <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="user" value="root"/>
        <property name="password" value=""/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/study"/>
    </bean>
  1. Test code:
    @Test
    public void testDataSource() throws SQLException {
        ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("ioc3.xml");
        DataSource dataSource = ac.getBean("datasource",DataSource.class);
        System.out.println(dataSource);
    }

Error message:

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasource' defined in class path resource [ioc3.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mchange.v2.c3p0.ComboPooledDataSource]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
    //翻译大概意思就是:创建名为'datasource'的bean时出错,该bean在类路径资源[ioc3.xml]中定义:实例化bean失败,为找到默认构造函数
    //但是ComboPooledDataSource是有无参构造的,所以可能无法找到准确的错误原因

After importing the auxiliary package, the DataSource is normally obtained:

result:

信息: Initializing c3p0-0.9.5.5 [built 11-December-2019 22:07:46 -0800; debug? true; trace: 10]
com.mchange.v2.c3p0.ComboPooledDataSource[ identityToken -> 1b61iobahze2bpj1a5zcgw|7fc229ab, dataSourceName -> 1b61iobahze2bpj1a5zcgw|7fc229ab ]

Process finished with exit code 0

The namespace context that needs to be used when referencing external configuration files

Import the namespace:

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">
Name space

If you only import the first row in the figure, the following error may be reported during runtime:

Line 8 in XML document from class path resource [ioc4.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 70; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明。