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"/>
  1. Test code:
    public void testDataSource() throws SQLException {
        ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("ioc3.xml");
        DataSource dataSource = ac.getBean("datasource",DataSource.class);

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

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


信息: Initializing c3p0- [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:


       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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' 的声明。