二、IOC容器4

简介: 二、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. 回到顶部↩︎
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
549 18
|
XML Java 数据格式
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
|
XML Java 数据格式
Spring5入门到实战------5、IOC容器-Bean管理(三)
这篇文章深入探讨了Spring5框架中IOC容器的高级Bean管理,包括FactoryBean的使用、Bean作用域的设置、Bean生命周期的详细解释以及Bean后置处理器的实现和应用。
Spring5入门到实战------5、IOC容器-Bean管理(三)
|
XML Java 数据格式
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
这篇文章是Spring5框架的入门教程,详细讲解了IOC容器中Bean的自动装配机制,包括手动装配、`byName`和`byType`两种自动装配方式,并通过XML配置文件和Java代码示例展示了如何在Spring中实现自动装配。
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
|
XML Java 数据格式
Spring5入门到实战------2、IOC容器底层原理
这篇文章深入探讨了Spring5框架中的IOC容器,包括IOC的概念、底层原理、以及BeanFactory接口和ApplicationContext接口的介绍。文章通过图解和实例代码,解释了IOC如何通过工厂模式和反射机制实现对象的创建和管理,以及如何降低代码耦合度,提高开发效率。
Spring5入门到实战------2、IOC容器底层原理
|
XML Java 开发者
经典面试---spring IOC容器的核心实现原理
作为一名拥有十年研发经验的工程师,对Spring框架尤其是其IOC(Inversion of Control,控制反转)容器的核心实现原理有着深入的理解。
1172 3
|
XML Java 数据格式
Spring5入门到实战------8、IOC容器-Bean管理注解方式
这篇文章详细介绍了Spring5框架中使用注解进行Bean管理的方法,包括创建Bean的注解、自动装配和属性注入的注解,以及如何用配置类替代XML配置文件实现完全注解开发。
Spring5入门到实战------8、IOC容器-Bean管理注解方式
|
XML Java 数据格式
Spring IOC容器的深度解析及实战应用
【10月更文挑战第14天】在软件工程中,随着系统规模的扩大,对象间的依赖关系变得越来越复杂,这导致了系统的高耦合度,增加了开发和维护的难度。为解决这一问题,Michael Mattson在1996年提出了IOC(Inversion of Control,控制反转)理论,旨在降低对象间的耦合度,提高系统的灵活性和可维护性。Spring框架正是基于这一理论,通过IOC容器实现了对象间的依赖注入和生命周期管理。
445 0

热门文章

最新文章