SpringBoot-06-导入Spring配置

导入Spring配置

默认情况下,Spring Boot 中是不包含任何的 Spring 配置文件的,即使我们手动添加 Spring 配置文件到项目中,也不会被识别。那么 Spring Boot 项目中真的就无法导入 Spring 配置吗?答案是否定的。

SpringBoot给我们提供了两种导入Spring配置的方式:

  • 使用@ImportResource注解加载Spring配置

  • 使用全注解的方式加载Spring配置

@ImportResource

在主启动类使用@ImportResource注解可以导入一个或多个Spring配置文件,并将其中的内容生效

实现

  1. 在项目中com.liang.service创建personService的接口,代码如下:
public interface PersonService {
    public Person getPerson();
}

  1. 在项目中com.liang.service创建personService的实现类personServiceImpl,代码如下:
public class PersonServiceImpl implements PersonService{

    @Autowired
    private Person person;
    @Override
    public Person getPerson() {
        return  person;
    }
}

3.在项目中resources下创建application.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="personService" class="com.liang.service.PersonServiceImpl"></bean>
</beans>
  1. 测试单元代码
    //自动装配
    @Autowired
    private Person person;

    //IOC容器
    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService(){
        //校验IOC容器中是否包含组件personService
        boolean flag= ioc.containsBean("personService");
        if(flag){
            System.out.println("personService已经在容器中");
        }else{
            System.out.println("personService不在容器中");
        }
    }
    @Test
    void contextLoads() {
        System.out.println(person);;
    }
  1. 运行
    发现没有达到预想要求,而且实现类识别不了person在这里插入图片描述

  2. 主启动程序类

添加以下注解:

		@ImportResource(locations = 		{"classpath:/beans.xml"})
  1. 再次执行代码
    在这里插入图片描述

全注解方式

Spring Boot 推荐我们使用全注解的方式加载 Spring 配置,其实现方式如下:

  1. 使用 @Configuration 注解定义配置类,替换 Spring 的配置文件;
  2. 配置类内部可以包含有一个或多个被 @Bean 注解的方法,这些方法会被 AnnotationConfigApplicationContext 或 AnnotationConfigWebApplicationContext 类扫描,构建 bean 定义(相当于 Spring 配置文件中的标签),方法的返回值会以组件的形式添加到容器中,组件的 id 就是方法名。

代码部分

import com.liang.service.PersonService;
import com.liang.service.PersonServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @Configuration 注解用于定义一个配置类,相当于 Spring 的配置文件
 * 配置类中包含一个或多个被 @Bean 注解的方法,该方法相当于 Spring 配置文件中的 <bean> 标签定义的组件。
 */
@Configuration
public class MyAppConfig {
    /**
     * 与 <bean id="personService" class="PersonServiceImpl"></bean> 等价
     * 该方法返回值以组件的形式添加到容器中
     * 方法名是组件 id(相当于 <bean> 标签的属性 id)
     */
    @Bean
    public PersonService personService() {
        System.out.println("在容器中添加了一个组件:peronService");
        return new PersonServiceImpl();
    }
}
Spring Boot与MyBatis的结合可以帮助我们快速开发基于数据库的应用程序。下面是使用Spring Boot和MyBatis的一般步骤: 1. 导入依赖:在Maven或Gradle项目中,添加Spring Boot和MyBatis的依赖。例如,在Maven项目中可以添加以下依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- 数据库驱动 --> <!-- 根据自己使用的数据库选择对应的驱动依赖 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` 2. 配置数据源:在application.properties或application.yml文件中配置数据库连接信息,包括数据库URL、用户名和密码等。 3. 创建实体类和Mapper接口:创建与数据库表对应的实体类,并创建Mapper接口用于定义数据库操作方法。 4. 创建Mapper映射文件:创建与Mapper接口对应的XML映射文件,编写SQL语句以及结果映射配置。 5. 注册Mapper接口:在Spring Boot配置类或者使用@MapperScan注解,将Mapper接口注册到Spring容器中。 6. 编写业务逻辑:在Service层编写业务逻辑,调用Mapper接口中的方法完成数据操作。 7. 运行应用程序:使用Spring Boot的启动类来运行应用程序,启动内嵌的Servlet容器。 以上是使用Spring Boot和MyBatis的一般步骤,当然在实际项目中还可能涉及其他配置和组件的使用。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值