SpringIOC操作Bean管理--最终章

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: SpringIOC操作Bean管理--最终章


外部属性文件

在我们编写配置文件的时候,经常要去在Bean注入一些对接数据库、nacos等一些系统部署信息的细节信息,如果直接把这些东西写到配置文件里面操作起来非常的不方便,为了优化这种问题,就出现了外部属性文件


如何直接配置数据库信息?

第一步:引入德鲁伊连接池依赖jar包


第二步:配置德鲁伊连接池

<?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  http://www.springframework.org/schema/context/spring-context.xsd" >
   <!--直接配置连接池-->
    <bean id="dataSource"  class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
        <property name="username" value="root"></property>
        <property name="password" value="2022"></property>
    </bean>
 </beans>



引入外部属性文件配置数据库连接池

第一步:创建外部属性文件,properties格式文件,写数据库信息

prop.driverClass:com.mysql.jdbc.Driver
prop.url:jdbc:mysql://localhost:3306/userDb
prop.userName:root
prop.password:2022

第二步:把外部properties属性文件引入到spring配置文件

*引入context名称空间

*在spring配置文件使用标签引入外部属性文件

<?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  http://www.springframework.org/schema/context/spring-context.xsd" >
    <!--引入外部属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置连接池-->
        <bean id="dataSource"  class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${prop.driverClass}"></property>
            <property name="url" value="${prop.url}"></property>
            <property name="username" value="${prop.userName}"></property>
            <property name="password" value="${prop.password}"></property>
        </bean>
</beans>

IOC操作Bean管理(基于注解方式)

什么是注解?

(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值)

(2)使用注解,注解作用在类上面,方法上面,属性上面

(3)使用注解的目的,简化xml配置


利用注解创建对象

可以实现创建对象的注解有:@Component、@Service、@Controller、@Repository 上面四个注解功能是一样的,都可以用来创建bean实例
第一步 引入依赖


第二步 开启组件扫描

<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 http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启组件扫描
    1.如果扫描多个包,多个包逗号隔开
    2.扫描上层目录-->
    <context:component-scan base-package="com.ioc.spring5"></context:component-scan>
</beans>

第三步 创建类,在类上面添加创建对象注解

//注解里面value属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService") //<bean id="userService" class=".."/>
public class UserService {
    public void add(){
        System.out.println("service add......");
    }
}

基于注解方式实现属性注入

(1)@AutoWired:根据属性类型进行自动装配

第一步:把service和dao对象创建,在service和dao类添加创建对象注解
第二步:在service注入dao对象,在service类添加dao类属性,在属性上面使用注解

(2)@Qualifer:根据属性名称进行注入

当一个接口有多个实现类,根据类型用不知道用那个,就要派它上场,和@AutoWired一起来使用了

(3)@Resource:可以根据类型注入,可以根据名称注入

(4)@Value:注入普通类型属性

代码示例:
service类:

//注解里面value属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService") //<bean id="userService" class=".."/>
public class UserService {
    //定义dao类型属性
    //不需要添加set方法
    //添加注入属性注解
//    @Autowired
//    @Qualifier(value = "userDaoImpl")
//    private UserDao userDao;
//    @Resource //根据类型注入
    @Resource(name = "userDaoImpl")
    private UserDao userDao;
    @Value(value = "abc")
    private String name;
    public void add(){
        System.out.println("service add......"+name);
        userDao.add();
    }
}

dao类:

@Repository
public class UserDaoImpl implements UserDao{
    @Override
    public void add() {
        System.out.println("dao add");
    }
}

dao接口:

public interface UserDao {
    public void add();
}

完全注解实现

创建配置类,替代xml配置文件
代码示例:

@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.ioc"})
public class SpringConfig {
}
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3月前
|
Java Spring
SpringMVC中bean的加载控制
SpringMVC中bean的加载控制
30 0
|
10月前
|
XML Java 数据库连接
“Spring管理JavaBean的过程及Bean的生命周期“
“Spring管理JavaBean的过程及Bean的生命周期“
73 0
|
10月前
|
Java Spring
创建名为 'authFilterRegistration' 的bean时,该bean依赖于一个未满足的依赖关系
创建名为 'authFilterRegistration' 的bean时,该bean依赖于一个未满足的依赖关系
52 1
|
3月前
|
Java Spring 容器
Spring注解开发定义bean及纯注解开发模式
Spring注解开发定义bean及纯注解开发模式
48 0
|
2月前
|
缓存 Java Spring
Bean 的创建和管理
Bean 的创建和管理
22 3
|
3月前
|
存储 XML Java
Spring框架学习 -- 读取和存储Bean对象
Spring框架学习 -- 读取和存储Bean对象
35 0
Spring框架学习 -- 读取和存储Bean对象
|
3月前
|
XML Java Maven
Spring框架学习 -- 创建与使用
Spring框架学习 -- 创建与使用
29 0
|
Java 容器 Spring
Bean的加载方式
Bean的加载方式 1.XML方式声明bean 2.XML+注解方式声明bean 3.注解方式声明配置类 扩展1——FactoryBean 扩展2——配置类中导入原始的配置文件(系统迁移) 扩展3——proxyBeanMethods 4.使用@Import导入要注入的bean 扩展4——使用@Import注解还可以导入配置类 5.使用上下文对象在容器初始化完毕后注入bean 6.导入实现了ImportSelector接口的类,实现对导入源的编程式处理 bean的加载方式(七) bean的加载方式(八)
159 1
|
XML Java 数据格式
【Spring 从0开始】IOC容器的Bean管理 - 基于XML,注入外部bean、内部bean和级联赋值
【Spring 从0开始】IOC容器的Bean管理 - 基于XML,注入外部bean、内部bean和级联赋值
【Spring 从0开始】IOC容器的Bean管理 - 基于XML,注入外部bean、内部bean和级联赋值
|
Java 应用服务中间件 Maven