【Java 从 0 到架构师】Spring - 复杂对象,java 的多线程的底层原理
public class ConnectionFactory {
public Connection getConn1() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("x", "x", "x");
}
}
<bean id="factory" class="com.mj.obj.ConnectionFactory"/>
<bean id="conn1" factory-bean="factory" factory-method="getConn1"/>
public class ConnectionFactory {
public static Connection getConn2() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("x", "x", "x");
}
}
<bean id="conn2" class="com.mj.obj.ConnectionFactory" factory-method="getConn2"/>
public class ConnectionFactoryBean implements FactoryBean<Connection> {
@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"/>
// & 开头代表创建的是 FactoryBean 类型的对象
// 参考: BeanFactory 中的 FACTORY_BEAN_PREFIX
System.out.println(ctx.getBean("&conn"))
db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_mybatis
jdbc.username=root
jdbc.password=root
首先在 applicationContext.xml 根标签中引入标签库:
<?xml version="1.0" encoding="UTF-8"?>
<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"/>
<bean id="conn" class="com.mj.obj.ConnectionFactoryBean">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
参考资料:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions
<bean id="dog" class="com.mj.domain.Dog"/>
<bean id="person" class="com.mj.domain.Person">
<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()}"/>-->
</bean>
参考资料:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes
scope="singleton"
:单例
通过同一个 id 值,在统一 IoC 容器中获取的永远是同一个实例
在 IoC 容器创建的时候,就会创建 bean,可以设置
lazy-init="true"
修改创建时机
scope="prototype"
:非单例;每次 getBaen 时创建一次 bean
============================================================================
Spring 已经内置了基本的类型转换功能,比如:
String 转 int
String 转 Date(支持 yyyy/MM/dd 格式)
<bean id="service" class="com.mj.service.PersonServiceImpl">
<property name="age" value="25"/>
<property name="birthday" value="2011/09/10"/>
</bean>
实现 Converter 接口:
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;
}
}
}
注册 Converter:
注:id 为 conversionService 是固定的
<bean id="person" class="com.mj.domain.Person" p:birthday="09_10_2111"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<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>
</bean>
评论