二、IOC容器4

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 二、IOC容器4

IOC 操作 Bean 管理(外部属性文件)

1、直接配置数据库信息

(1)配置德鲁伊连接池

(2)引入德鲁伊连接池依赖jar包

JDBC中包含官网下载

src下/新建bean6.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 直接配置数据库连接池   -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Drive"></property><!-- 为8.0之后  前为 com.mysql.jdbc.Drive   -->
        <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

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

(1)创建外部属性文件,properties格式文件,数据库信息

src下/新建 jdbc.properties
prop.driverClass=com.mysql.cj.jdbc.Drive
prop.url=jdbc:mysql://localhost:3306/userDb
prop.username=root
prop.password=root

(2)把外部properties属性文件引入到spring配置文件中

  • 引入context名称空间
  • 在spring配置文件使用标签引入外部属性文件
bean6.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       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/util http://www.springframework.org/schema/util/spring-util.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.cj.jdbc.Drive"></property>&lt;!&ndash; 为8.0之后  前为 com.mysql.jdbc.Drive   &ndash;&gt;-->
    <!--        <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>-->
    <!--        <property name="username" value="root"></property>-->
    <!--        <property name="password" value="root"></property>-->
    <!--    </bean>-->
    <!-- 引入外部属性文件   -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!-- 直接配置数据库连接池   -->
    <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、什么是注解

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

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

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

2、Spring针对Bean管理中创建对象提供注解

(1)@Component

(2)@Service

(3)@Controller

(4)@Repository

  • 上面的四个注解功能是一样的,都可以用来创建bean实例

3、基于注解方式实现对象创建

第一步 引入依赖

spring-aop-5.3.10.jar

第二步 开启组件扫描

新建一个模块(项目)spring5_demo3/src下/新建bean1.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"
       xmlns:p="http://www.springframework.org/schema/p"
       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.atguigu"></context:component-scan>
</beans>

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

spring5/新建service/新建UserService
package com.atguigu.spring5.service;
import org.springframework.stereotype.Component;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService")//<bean id="userService" class",,"/>
public class UserService {
    public void add(){
        System.out.println("service add.........");
    }
}
TestSpring5Demo1
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5Demo1 {
    @Test
    public void testService(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        UserService uservice = context.getBean("userService", UserService.class);
        System.out.println(uservice);
        uservice.add();
    }
}

4、开启组件扫描细节配置

bean1.xml
<!-- 实例1
        use-default-filters="false"  表示现在不使用默认filter,自己配置filter
        context:include-filter ,设置扫描哪些内容
     -->
    <context:component-scan base-package="com.atguigu" use-default-filters="false">
        <context:include-filter type="annotation"
                            expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 实例2     
        下面配置扫描所有内容
        context:exclude-filter ,设置哪些内容 不 进行扫描
     -->
    <context:component-scan base-package="com.atguigu">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

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

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

第一步 把service和dao对象创建,在service和dao类添加创建对象注解
spring5/新建dao/新建UserDao接口
package com.atguigu.spring5.dao;
public interface UserDao {
    public void add();
}

dao/新建UserDaoImpl

package com.atguigu.spring5.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao{
    @Override
    public void add() {
        System.out.println("dao add.....");
    }
}
第二部在service注入dao对象,在service类中添加dao类型属性,在属性上面使用注解
UserService
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService -- userService
//@Component(value = "userService")//<bean id="userService" class",,"/>
//@Controller
//@Repository
@Service
public class UserService {
    //定义dao类型属性
    //不需要添加set方法
    //添加注入属性注解
    @Autowired
    private UserDao userDao;
    public void add(){
        System.out.println("service add.........");
        userDao.add();
    }
}

执行TestSpring5Demo1

com.atguigu.spring5.service.UserService@73e22a3d
service add.........
dao add.....
Process finished with exit code 0

(2)@Qualifier:根据属性名称进行自动注入

这个@Qualifier注解的使用,和上面@Autowired一起使用

package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService -- userService
//@Component(value = "userService")//<bean id="userService" class",,"/>
//@Controller
//@Repository
@Service
public class UserService {
    //定义dao类型属性
    //不需要添加set方法
    //添加注入属性注解
    @Autowired//根据类型进行注入
    @Qualifier(value = "userDaoImpl")//根据名称进行注入,当UserDao有多个实现类时,可以精确找到
    private UserDao userDao;
    public void add(){
        System.out.println("service add.........");
        userDao.add();
    }
}

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

jdk11之后开始移除了,spring官方不建议用因为在javax.annotation.Resource

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

UserService
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService -- userService
//@Component(value = "userService")//<bean id="userService" class",,"/>
//@Controller
//@Repository
@Service
public class UserService {
    @Value(value = "abc")
    private String name;
    //定义dao类型属性
    //不需要添加set方法
    //添加注入属性注解
    @Autowired//根据类型进行注入
    @Qualifier(value = "userDaoImpl")//根据名称进行注入,当UserDao有多个实现类时,可以精确找到
    private UserDao userDao;
    public void add(){
        System.out.println("service add........."+name);
        userDao.add();
    }
}

6、完全注解开发

(1)创建配置类,替代xml配置文件

spring5下/新建config包/新建SpringConfig
package com.atguigu.spring5.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.atguigu"})
public class SpringConfig {
}

(2)编写测试类

TestSpring5Demo1
 @Test
    public void testService2(){
        //加载配置类
        ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService uservice = context.getBean("userService", UserService.class);
        System.out.println(uservice);
        uservice.add();
    }
  1. 回到顶部↩︎
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2天前
|
Java 测试技术 开发者
IoC容器有什么作用?
【4月更文挑战第30天】IoC容器有什么作用?
33 0
|
2天前
|
Java 测试技术 开发工具
ApplicationArguments读取应用程序参数并注入到IOC容器
ApplicationArguments读取应用程序参数并注入到IOC容器
ApplicationArguments读取应用程序参数并注入到IOC容器
|
2天前
|
XML Java 数据格式
Spring IoC容器初始化过程(xml形式)
Spring IoC容器初始化过程(xml形式)
52 0
|
2天前
|
Java 测试技术 开发者
Spring IoC容器通过依赖注入机制实现控制反转
【4月更文挑战第30天】Spring IoC容器通过依赖注入机制实现控制反转
22 0
|
2天前
|
Java 开发者 容器
IoC容器如何实现依赖注入?
【4月更文挑战第30天】IoC容器如何实现依赖注入?
21 0
|
2天前
|
XML Java 数据格式
如何配置IoC容器?
【4月更文挑战第30天】如何配置IoC容器?
20 0
|
2天前
|
XML Java 程序员
什么是Spring的IoC容器?
【4月更文挑战第30天】什么是Spring的IoC容器?
20 0
|
2天前
|
存储 前端开发 Java
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
|
2天前
|
XML Java 数据格式
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (下)
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界
|
2天前
|
XML Java 数据格式
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (上)
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (上)

热门文章

最新文章