public class ConnectionFactory { public static Connection getConn2() throws Exception { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection("x", "x", "x"); } }
[](https://codechina.csdn.net/m0_60958482/java-p7)FactroyBean ------------------------------------------------------------------------------
public class ConnectionFactoryBean implements FactoryBean {
@Override public Connection getObject() throws Exception { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection("x", "x", "x"); } @Override public Class<?> getObjectType() { return Connection.class; }
}
<bean id="conn" class="com.mj.obj.ConnectionFactoryBean"/>
//Beginning with & indicates that an object of type FactoryBean is created
//Reference: factory in BeanFactory_ BEAN_ PREFIX
System.out.println(ctx.getBean("&conn"))
[](https://codechina.csdn.net/m0_60958482/java-p7) import external configuration file ---------------------------------------------------------------------------
db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_mybatis
jdbc.username=root
jdbc.password=root
<?xml version="1.0" encoding="UTF-8"?>First in applicationContext.xml Import tag library into root tag: ![Insert picture description here](https://img-blog.csdnimg.cn/6b771e107c714d0e978789284cae0cc5.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNDA5NQ==,size_16,color_FFFFFF,t_70)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="db.properties"/>
<property name="driverClass" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.url}"/> <!-- ${username} The local user name will be obtained fixedly --> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/>
[](https://codechina.csdn.net/m0_60958482/java-p7)SpEL expression -------------------------------------------------------------------------- > reference material:[https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions](https://codechina.csdn.net/m0_60958482/java-p7)
<property name="dog" value="#{dog}"/> <property name="age" value="#{dog.testAge}"/> <property name="name" value="#{dog.testName.bytes.length}"/> <property name="name" value="#{dog.getTestName()}"/>-->
[](https://codechina.csdn.net/m0_60958482/java-p7)scope - controls whether the bean is singleton --------------------------------------------------------------------------------------- > reference material:[https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes](https://codechina.csdn.net/m0_60958482/java-p7) `scope="singleton"`: Single case * Through the same id Value, in unity IoC The same instance is always obtained in the container * stay IoC When a container is created, it is created bean,Can set `lazy-init="true"` Modify creation time `scope="prototype"`: Non single case; every time getBaen Create once when bean [](https://codechina.csdn.net/m0_60958482/java-p7)Converter ============================================================================ > [[Spring5.x]Object lifecycle, profile parameterization, custom type converter, post-processing Bean](https://codechina.csdn.net/m0_60958482/java-p7) [](https://codechina.csdn.net/m0_60958482/java-p7)Spring has built-in basic type conversion functions ------------------------------------------------------------------------------------- Spring Basic type conversion functions have been built in, such as: * String turn int * String turn Date(support **yyyy/MM/dd** (format)
<property name="age" value="25"/> <property name="birthday" value="2011/09/10"/>
[](https://codechina.csdn.net/m0_60958482/java-p7) custom Converter -------------------------------------------------------------------------------- realization Converter Interface:
public class DateConverter implements Converter<String, Date> {
private List<String> formats; public void setFormats(List<String> formats) { this.formats = formats; } @Override public Date convert(String s) { try { return new SimpleDateFormat(format).parse(s); } catch (ParseException e) { e.printStackTrace(); return null; } }
}
register Converter: > Note: id by conversionService It's fixed
<property name="converters"> <set> <bean class="com.mj.converter.DateConverter"> <property name="formats" value="yyyy-MM-dd"/> </bean> <bean class="com.mj.converter.DateConverter"> <property name="formats" value="MM_dd_yyyy"/> </bean> </set> </property>
* * * Advanced example: in xml Middle afferent list Class design for realization Converter Interface:
public class DateConverter implements Converter<String, Date> {
private List<String> formats; public void setFormats(List<String> formats) { this.formats = formats; }
last
Do everything with your heart and pay great attention to details. Seemingly insignificant and cumbersome work will have unexpected value if done thoroughly.
Of course, if you want to become a technical bull, you also need a certain ideological pattern. Your thoughts determine which direction you want to go in the future. It is suggested to read more books on life planning and learn more about the ideological pattern of celebrities. Your road will go further in the future.
I have sorted out more thought maps of technology points, covering 99% of the most popular technology points on the Internet. Here I share this map and a complete set of interview system prepared for golden nine silver ten, from collection to distributed micro services
How to obtain this set of high-quality information?
Doing it thoroughly will have unexpected value.
Of course, if you want to become a technical bull, you also need a certain ideological pattern. Your thoughts determine which direction you want to go in the future. It is suggested to read more books on life planning and learn more about the ideological pattern of celebrities. Your road will go further in the future.
I have sorted out more thought maps of technology points, covering 99% of the most popular technology points on the Internet. Here I share this map and a complete set of interview system prepared for golden nine silver ten, from collection to distributed micro services
[external chain picture transferring... (img-fMHwRKUy-1629250203156)]
[external chain picture transferring... (img-nEAp4UIB-1629250203158)]
[external chain picture transferring... (img-ye7fWWke-1629250203158)]
[external chain picture transferring... (IMG ypbounfb-1629250203160)]
How to obtain this set of high-quality information?