ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot学习笔记(三)——SpringBoot中的配置Bean

2022-09-13 21:02:26  阅读:278  来源: 互联网

标签:xml SpringBoot args 笔记 public Bean MyApplication 注解 class


在Spring中,我们可以使⽤XML的⽅式来对Spring进⾏配置,也可以通过Java Config(也就是类+注解)的⽅式进⾏配置,在Spring Boot中也是⼀样的。

方法一:使用xml的方式(xml文件+@importResource+@Autowired)

我们可以通过@ImportResource注解来导⼊⼀个XML⽂件作为Spring的配置⽂件. 示例如下: 让我们来重构之前创建出来的项目。 pom.xml中代码不变,依然如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <!-- 父类包,引入这个依赖以后,下边的依赖就不需要写版本号了   -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!--web应用的相关包,其实就是springmvc和spring的相关包  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

修改com.example.MyApplication.java代码如下:

@EnableAutoConfiguration
@ImportResource("spring.xml")
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

在com.example.controller包下创建UserController,代码如下:

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    String home() {
        return userService.test();
    }
}

在com.example.service包下创建UserService,代码如下:

public class UserService {
    public String test() {
        return "hello world, nice";
    }
}

在resources包下创建spring.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
   https://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="userService" class="com.example.service.UserService" />
 <bean id="userController" class="com.example.controller.UserController"
/>
</beans>

然后就可以看到程序跑通了。

方法二:使用类+注解的方式(推荐使用)

我们也可以通过@Import+@Configuration+@Bean来进⾏等价替换掉XML的形式。 在方法一的基础上,pom.xml文件不用动。

在com.example这个文件夹下新建AppConfig.java文件,这个文件相当于之前的xml配置文件,代码如下:

@Configuration
public class AppConfig {

    @Bean
    public UserService userService(){
        return new UserService();
    }

    @Bean
    public UserController userController(){
        return new UserController();
    }
}

修改MyApplication.java代码如下:

@EnableAutoConfiguration
@Import(AppConfig.class)
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

UserService和UserController中的代码不用变。

方法三:自动扫描—+xml的方式实现配置

使用@Service和@Autowired注解+配置自动扫描就可以,使用@Service注解的话,就不用在xml文件中声明bean了,会自动将当前类注入到spring容器之中。

在方法一的基础上,在service类前加上@Service注解。

然后修改spring.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
   https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.example" />
    
</beans>

方法四:自动扫描+注解的方式完成配置

配置类的作⽤除开可以通过@Bean来定义Bean之外,也可以配置扫描路径,这样我们就直接不用AppConfig这个类了,因为@Controller和@Service(如果想让其它层的注册为bean,可以用@Component这个注解)这种注解是可以被@ComponentScan(“扫描包”)这个注解扫描到。 直接删除AppConfig这个类,然后修改MyApplication这个类如下:
@EnableAutoConfiguration
@ComponentScan("com.example")
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
这样就不需要⽤@Bean了,但是得在类上加上@Component注解来定义Bean。 扫描过程中,除开可以扫描到@Component、@Service、@Controller、@RestController等注解之外,也能扫描到@Configuration,也就是我们可以在扫描路径下定义其他的配置类。 另外,由于MyApplication类所在包就是com.example,所以我们可以直接这么写:
@EnableAutoConfiguration
@ComponentScan()
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

同时,我们可以让MyApplication这个类本身作为配置类,也就是加上一个@Configuration这个注解。

代码变成了:

@EnableAutoConfiguration
@Configuration
@ComponentScan
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
   }
}
此时MyApplication就存在三个注解: @EnableAutoConfiguration @Configuration @ComponentScan 在Spring Boot中,提供了⼀个注解来替代这三个注解,这个注解就是@SpringBootApplication。 所以代码就可以改成下列经典写法。
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
   }
}

标签:xml,SpringBoot,args,笔记,public,Bean,MyApplication,注解,class
来源: https://www.cnblogs.com/worthmove/p/16690815.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有