Spring完全注解开发

举报
牛哄哄的柯南 发表于 2021/05/26 15:23:26 2021/05/26
【摘要】 完全注解开发的意思就是不使用xml配置文件,之前我们可以用使用注解方式替代xml配置文件方式完成很多事情,但是还是要在配置文件中开启组件扫描,现在我们创建一个配置类来完全代替配置文件,在配置类中完成开启组件扫描的操作。 创建配置类,替代xml配置文件 @Configuration: 把当前类作为配置类,替代xml配置文件 SpringConfi...

完全注解开发的意思就是不使用xml配置文件,之前我们可以用使用注解方式替代xml配置文件方式完成很多事情,但是还是要在配置文件中开启组件扫描,现在我们创建一个配置类来完全代替配置文件,在配置类中完成开启组件扫描的操作。

创建配置类,替代xml配置文件

@Configuration: 把当前类作为配置类,替代xml配置文件

SpringConfig类:

package com.Keafmd.spring5.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Keafmd
 *
 * @ClassName: SpringConfig
 * @Description: Spring配置类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-17 17:04
 */

@Configuration //把当前类作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.Keafmd"}) //  替代配置文件中的<context:component-scan base-package="com.Keafmd"></context:component-scan>
public class SpringConfig {
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

测试类

测试类:

package com.Keafmd.spring5.testdemo;

import com.Keafmd.spring5.config.SpringConfig;
import com.Keafmd.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestSpring5Demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-17 13:03
 */
public class TestSpring5Demo1 { @Test public void testService(){ //使用配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); UserService userService = context.getBean("userService",UserService.class); System.out.println(userService); userService.add(); } @Test public void testService2(){ // 加载配置类 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); UserService userService = context.getBean("userService",UserService.class); System.out.println(userService); userService.add(); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

两种测试输出结果相同:

com.Keafmd.spring5.service.UserService@7ce3cb8e
name:Keafmd
service add......
dao add ...

Process finished with exit code 0

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这样我们就通过实用配置类的方式完全替代了配置文件,从而达到完全注解开发。

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]

在这里插入图片描述

加油!

共同努力!

Keafmd

文章来源: keafmd.blog.csdn.net,作者:牛哄哄的柯南,版权归原作者所有,如需转载,请联系作者。

原文链接:keafmd.blog.csdn.net/article/details/112753665

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。