目录
配置MyBatis的SqlSessionFactory交由Spring管理
配置非自定义的Bean需要考虑如下两个问题
-
被配置的Bean的实例化方式是什么?有参构造,无参构造,静态工厂方式还是实例化工厂方式
-
被配置的Bean是否需要注入必要属性
配置Druid数据源交由Spring管理
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<?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 id="druidDatas" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///book?useSSL=false&useServerPrepStmts=true"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>
配置Connection交由Spring管理
<bean id="aClass" class="java.lang.Class" factory-method="forName">
<constructor-arg name="className" value="com.mysql.jdbc.Driver"></constructor-arg>
</bean>
<bean id="driverManager" class="java.sql.DriverManager" factory-method="getConnection">
<constructor-arg name="url" value="jdbc:mysql:///book?useSSL=false&useServerPrepStmts=true"></constructor-arg>
<constructor-arg name="user" value="root"></constructor-arg>
<constructor-arg name="password" value="1234"></constructor-arg>
</bean>
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object aClass = applicationContext.getBean("aClass");
Object driverManager = applicationContext.getBean("driverManager");
System.out.println(driverManager);
System.out.println(aClass);
配置日期对象交由Spring管理
<!-- 实例化工厂方法-->
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg name="pattern" value="yyyy-MM-dd HH:mm:ss"></constructor-arg>
</bean>
<bean id="Date" factory-bean="dateFormat" factory-method="parse">
<constructor-arg name="source" value="2023-11-15 21:14:40"></constructor-arg>
</bean>
public static void main(String[] args) throws ParseException {
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
//原始代码
String curreentTimeStr="2023-11-15 21:14:40";
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(curreentTimeStr);
System.out.println(date);
Date date1 = applicationContext.getBean("Date", Date.class);
System.out.println(date1);
}
配置MyBatis的SqlSessionFactory交由Spring管理
<bean id="builder" class="org.apache.ibatis.session.SqlSessionFactoryBuilder"></bean>
<bean id="sqlSessionFactory" factory-bean="builder" factory-method="build">
<constructor-arg name="inputStream" value="mybatis-config.xml"></constructor-arg>
</bean>
<!-- spring 管理sqlsessionfactory的创建-->
<bean id="resources" class="org.apache.ibatis.io.Resources" factory-method="getResourceAsStream">
<constructor-arg name="resource" value="mybatis-config.xml"></constructor-arg>
</bean>
<bean id="builder" class="org.apache.ibatis.session.SqlSessionFactoryBuilder"></bean>
<bean id="sqlSessionFactory" factory-bean="builder" factory-method="build">
<constructor-arg name="inputStream" ref="resources"></constructor-arg>
</bean>
public static void main(String[] args) throws ParseException, IOException {
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
//原始代码
String resource="mybatis-config.xml";
InputStream resourceAsFile=null;
resourceAsFile = Resources.getResourceAsStream(resource);
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsFile);
System.out.println(build);
//
SqlSessionFactory sqlSessionFactory = applicationContext.getBean("sqlSessionFactory", SqlSessionFactory.class);
System.out.println(sqlSessionFactory);
}