Spring管理第三方依赖

ops/2024/10/19 1:25:01/

在开发中,我们常需要根据业务需求导入我们需要的第三方依赖包,本文主要以导入druid数据库来连接池为案例讲解有关spring管理第三方依赖

目录

纯注解文件注入

1.在pom.xml中导入依赖

2.在com.lcyy包下建立一个config包用于配置类的实现

3.在config包下建立一个JdbcConfig类

4.创建测试类

5.运行结果如下:


以下我将通过纯注解方式实现

纯注解文件注入

1.在pom.xml中导入依赖

需要导入的有springspring-context,spring-jdbc,druid

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.31</version></dependency>
 <dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.31</version></dependency>
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.18</version></dependency>

2.在com.lcyy包下建立一个config包用于配置类的实现

在resource下建立一个jdbc的配置文件后缀为

java">jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/db
jdbc.username=root
jdbc.password=zhien0516

在包下建一个SpringConfig类

java">package com.lcyy.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;@Configuration
@ComponentScan("com.lcyy")
@PropertySource("classpath:jdbc.properties")
public class SpringConfig {
}

 其中@Configuration 表明该类是一个配置类

@ComponentScan("com.lcyy")  表示包的扫描,表示在com.lcyy下的所有包都会被spring ioc容器管理

@PropertySource 表示加载jdbc的配置文件

3.在config包下建立一个JdbcConfig类

java">package com.lcyy.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;@Configuration
public class JdbcConfig {@Value("druid")private String age;@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;@Bean("dataSource")public DataSource dataSource(){DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(username);ds.setPassword(password);System.out.println("我是德鲁伊"+age);//测试德鲁伊return ds;}
}

 注意:

@Configuration 可以在SpringConfig类上用@Import代替,如下

java">package com.lcyy.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;@Configuration
@ComponentScan("com.lcyy")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class})
public class SpringConfig {}

4.创建测试类

java">import com.lcyy.config.SpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class DataSource {public static void main(String[] args) {//获取ioc容器AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);//根据类型获取bean,并强制转换为DataSource类型Object ctxBean =  ctx.getBean("dataSource");System.out.println(ctxBean);}
}

注意:此时这里用的是纯注解方式实现第三方技术管理依赖,若用xml文件实现,则获取容器就不是用的 AnnotationConfigApplicationContext 而是用的 ClassPathXmlApplicationContext

ClassPathXmlApplicationContext里填写的是用SpringContext.xml,而AnnotationConfigApplicationContext 里填写的是SpringConfig.class类

5.运行结果如下:


http://www.ppmy.cn/ops/24409.html

相关文章

互连网络的负载平衡路由算法 (UGAL, Universal Globally Adaptive Load-Balancing 通用全局自适应负载平衡)

Universal Globally Adaptive Load-Balancing 通用全局自适应负载平衡 1. Motivation 动机2. 任意对称拓扑上的 UGAL3. 总结 Universal Globally Adaptive Load-Balancing 通用全局自适应负载平衡 之前的工作都是基于 torus 网络的负载平衡路由&#xff0c;而这篇文章的内容…

【百度Apollo】探索自动驾驶:Apollo 新版本 Beta 全新的Dreamview+,便捷灵活更丰富

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《linux深造日志》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 文章目录 引入一、Dreamview介绍二、Dreamview 新特性2.1、基于模式的多场景——流程更简洁地图视角调节&#xff1a;调试流…

docker打包容器为镜像

要使用Docker将容器打包成镜像&#xff0c;你需要执行以下步骤&#xff1a; 创建一个Dockerfile&#xff0c;定义如何构建你的镜像。 使用docker build命令来创建镜像。 以下是一个简单的示例&#xff1a; 首先&#xff0c;创建一个名为Dockerfile的文件&#xff0c;内容如…

抽象代理模式2.0版本

前言&#xff1a; 1.0版本的核心 代理的定义 A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource t…

【Proteus】LED呼吸灯 直流电机调速

1.LED呼吸灯 #include <REGX51.H> sbit LEDP2^0; void delay(unsigned int t) {while(t--); } void main() {unsigned char time,i;while(1){for(time0;time<100;time){for(i0;i<20;i){LED0;delay(time);LED1;delay(100-time);}}for(time100;time>0;time--){fo…

【算法基础实验】图论-UnionFind连通性检测之quick-union

Union-Find连通性检测之quick-union 理论基础 在图论和计算机科学中&#xff0c;Union-Find 或并查集是一种用于处理一组元素分成的多个不相交集合&#xff08;即连通分量&#xff09;的情况&#xff0c;并能快速回答这组元素中任意两个元素是否在同一集合中的问题。Union-Fi…

mybatis - XxxMapper.java接口中方法的参数 和 返回值类型,怎样在 XxxMapper.xml 中配置的问题

这个例子中的mybatis-config.xml文件&#xff0c;引用这个文件即可 实体类src/main/java/com.atguigu.pojo/Employee.java package com.atguigu.pojo;public class Employee {private Integer id;private String name;private String plone;public Integer getId() {return i…

ElasticSearch面试题

目录 1、ElasticSearch基本概念&#xff1a; 2、正向索引和倒排索引&#xff1a; 正向索引&#xff1a; 倒排索引&#xff1a; 3、Mysql和Elasticsearch对比 4、ik_smart和 ik_max_word区别、优缺点 5、ElasticSearch介绍/理解 6、ELK 1、ElasticSearch基本概念&#x…