Java Spring 配置非自定义Bean
0.介绍
实际开发中会使用第三方jar包中的功能类,这些Bean要想让Spring进行管理,也需要对其进行配置
两个问题:
- 被配置的Bean实例化方式是什么
- 被配置的Bean是否需要注入必要属性
1.配置Druid数据源交由Spring管理
- maven配置文件导入相应jar包
pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
beans.xml
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/java"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
主函数调用
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Object dataSource = applicationContext.getBean("dataSource");
System.out.println(dataSource);
}
运行结果
{
CreateTime:"2023-02-26 17:23:51",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
2.配置Connection交由Spring管理
Connection的产生是通过DriverManager的静态方法getConnection获取的,所以我们要用静态工厂的方式配置
beans.xml(getConnection报红不用管)
<bean id="connection" class="java.sql.DriverManager" factory-method="getConnection" scope="prototype">
<constructor-arg name="url" value="jdbc:mysql://localhost:3306/java"/>
<constructor-arg name="user" value="root"/>
<constructor-arg name="password" value="root"/>
</bean>
主函数调用
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Object connection = applicationContext.getBean("connection");
System.out.println(connection);;
}
执行结果(报错不用管,看到有类字节码地址输出即可)
com.mysql.jdbc.JDBC4Connection@42a15bdc
3.配置日期对象交由Spring管理
原生使用日期对象
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse("2023-08-27 12:00:00");
System.out.println(date);
结果输出
Sun Aug 27 12:00:00 CST 2023
配置日期对象交由Spring管理
beans.xml
<bean id="simpleDateFormat" class="java.text.SimpleDateFormat">
<constructor-arg name="pattern" value="yyyy-MM-dd HH:mm:ss"/>
</bean>
<bean id="date" factory-bean="simpleDateFormat" factory-method="parse">
<constructor-arg name="source" value="2023-08-27 12:00:00"/>
</bean>
主函数执行
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Object date1 = applicationContext.getBean("date");
System.out.println(date1);
结果输出
Sun Aug 27 12:00:00 CST 2023
4.配置MyBatis的SqlSessionFactory交由Spring管理
导入MyBatis的相关坐标
<!-- mybatis框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
4.1 原生实现
// 静态工厂方法方式
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
// 无参构造实例化
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 实例工厂方法
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
System.out.println(sqlSessionFactory);
执行结果
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@79efed2d
4.2 Bean实现
resources目录下创建mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/java"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>
beans.xml
<!-- 静态工厂方法 -->
<bean id="resources" class="org.apache.ibatis.io.Resources" factory-method="getResourceAsStream">
<constructor-arg name="resource" value="mybatis-config.xml"/>
</bean>
<!-- 无参构造实例化 -->
<bean id="sqlSessionFactoryBuilder" class="org.apache.ibatis.session.SqlSessionFactoryBuilder"/>
<!-- 实例工厂方法 -->
<bean id="sqlSessionFactory" factory-bean="sqlSessionFactoryBuilder" factory-method="build">
<constructor-arg name="inputStream" ref="resources"/>
</bean>
主函数调用
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Object sqlSessionFactory = applicationContext.getBean("sqlSessionFactory");
System.out.println(sqlSessionFactory);
执行结果
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@5026735c
评论 (0)