3、spring创建第三方bean对象

发布时间 2023-09-23 11:05:39作者: 至尊只有一个

3、spring创建第三方bean对象

在Spring中,很多对象都是单实例的,在日常的开发中,我们经常需要使用某些外部的单实例对象,例如数据库连接池,下面我们来讲解下如何在spring中创建第三方bean实例。

​ 1、导入数据库连接池的pom文件

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
2、编写配置文件
ioc.xml
<?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="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/demo?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="root123456"></property>
</bean>
</beans>
SpringDemoTest.java
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

public class SpringDemoTest {
public static void main(String[] args) throws SQLException {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext3.xml");
DruidDataSource bean = context.getBean(DruidDataSource.class);
System.out.println(bean);
System.out.println(bean.getConnection());
}
}

4、spring引用外部配置文件

在resource中添加dbconfig.properties

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
userName=root
password=root123456
编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--加载外部配置文件
在加载外部依赖文件的时候需要context命名空间
-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="root"></property>
<property name="password" value="root123456"></property>
</bean>
</beans>

5、spring基于xml文件的自动装配

​ 当一个对象中需要引用另外一个对象的时候,在之前的配置中我们都是通过property标签来进行手动配置的,其实在spring中还提供了一个非常强大的功能就是自动装配,可以按照我们指定的规则进行配置,配置的方式有以下几种:

​ default/no:不自动装配

​ byName:按照名字进行装配,以属性名作为id去容器中查找组件,进行赋值,如果找不到则装配null

​ byType:按照类型进行装配,以属性的类型作为查找依据去容器中找到这个组件,如果有多个类型相同的bean对象,那么会报异常,如果找不到则装配null

​ constructor:按照构造器进行装配,先按照有参构造器参数的类型进行装配,没有就直接装配null;如果按照类型找到了多个,那么就使用参数名作为id继续匹配,找到就装配,找不到就装配null

<?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="address" class="com.mashibing.bean.Address">
<property name="province" value="江西省"></property>
<property name="city" value="抚州市"></property>
<property name="town" value="南城县"></property>
</bean>

<bean id="person" class="com.mashibing.bean.Person" autowire="byName"></bean>
<bean id="person1" class="com.mashibing.bean.Person" autowire="byType"></bean>
<bean id="person2" class="com.mashibing.bean.Person" autowire="constructor"></bean>
</beans>

6、SpEL的使用

​ SpEL:Spring Expression Language,spring的表达式语言,支持运行时查询操作对象

​ 使用#{...}作为语法规则,所有的大括号中的字符都认为是SpEL.

<bean id="person3" class="com.mashibing.bean.Person">
<property name="age" value="#{12*2}"></property>
<property name="name" value="#{address.province}"></property>
<property name="address" value="#{address}"></property>
<property name="hobbies" value="#{T(java.util.UUID).randomUUID().toString().substring(0,4)}"></property>
<property name="gender" value="#{address.getCity()}"></property>
</bean>