当前位置: 首页 > news >正文

Spring——Bean管理-注解方式进行属性注入

Spring针对Bean管理中创建对象提供的注解有哪些?

@Component:普通

@Service:业务逻辑层

@Controller:controller层

@Repository:dao层

用注解的方式是为什么?

简化xml方式开发,只需要注解就可以完成在配置文件中的配置

如何实现注解开发?

创建对象

组件扫描

package com.atguigu.spring5.ComponentScan;import org.springframework.stereotype.Component;/*** @BelongsProject: 02-Spring* @BelongsPackage: com.atguigu.spring5* @Author: dengLiMei* @CreateTime: 2023-02-04  16:56* @Description: TODO* @Version: 1.0*/
//value可以不写,如果不写默认是类名的首字母小写
@Component(value = "user")
public class User {public void add() {System.out.println("aaaa");}
}
package com.atguigu.spring5.ComponentScan;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @BelongsProject: 02-Spring* @BelongsPackage: com.atguigu.spring5.ComponentScan* @Author: dengLiMei* @CreateTime: 2023-02-10  15:12* @Description: TODO* @Version: 1.0*/
public class Main {public static void main(String[] args) {ApplicationContext content= new ClassPathXmlApplicationContext("beans.xml");User user = content.getBean("user", User.class);user.add();}
}
<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--    开启组件扫描如果扫描多个包,多个包使用逗号隔开扫描包上层目录--><!--   表示: 扫描包中的所有类--><context:component-scan base-package="com.atguigu.spring5"></context:component-scan><!--    use-default-filters=“false”:表示现在不使用默认filter,自己配置filtercontext:include-filter,设置扫描哪些内容--><!--    表示:  只扫描注解带Controller的类--><context:component-scan base-package="com.atguigu.spring5" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--    下面配置扫描包所有内容context:include-filter,设置扫描哪些内容--><!--    表示:  除了Controller其余类都扫描--><context:component-scan base-package="com.atguigu.spring5"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-filtertype="" expression=""/></context:component-scan>
</beans>

属性注入

@Autowired:根据类型注入

@Qualifier:根据名称进行注入,和Autowired

@Resource:可以根据类型或名称注入

@Value:普通属性注入

javax:java扩展包


完全注解开发

Configuration:作为配置类,替代xml配置文件

package com.atguigu.spring5.SpringConfiguration;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;@Configuration
@ComponentScan(basePackages = {"com.atguigu.spring5"})
public class SpringConfigure {}
package com.atguigu.spring5.ScopeTest;import org.springframework.stereotype.Component;/*** @BelongsProject: 02-Spring* @BelongsPackage: com.atguigu.spring5.Book* @Author: dengLiMei* @CreateTime: 2023-02-08  19:28* @Description: TODO* @Version: 1.0*/
@Component
public class Book {private String bname;private String bauthor;public void setBname(String bname) {this.bname = bname;}public void setBauthor(String bauthor) {this.bauthor = bauthor;}public static void main(String[] args) {Book book = new Book();book.setBname("abc");}public void testDemo() {System.out.println(bname + "---" + "aaa");}}
package com.atguigu.spring5.SpringConfiguration;import com.atguigu.spring5.ScopeTest.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @BelongsProject: 02-Spring* @BelongsPackage: com.atguigu.spring5.SpringConfiguration* @Author: dengLiMei* @CreateTime: 2023-02-11  09:38* @Description: TODO* @Version: 1.0*/
public class Main {public static void main(String[] args) {
//        加载配置类ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigure.class);
//        创建实例Book book = context.getBean("book", Book.class);
//        调用方法book.testDemo();}
}

输出结果:


Spring系列文章:

Spring——是什么?作用?内容?用到的设计模式?

Spring——Bean管理-xml方式进行属性注入

Spring——Bean管理-注解方式进行属性注入

Spring——什么是IOC?

Spring——AOP是什么?如何使用?

Spring——什么是事务?传播行为?事务隔离级别有哪些?

Spring——整合junit4、junit5使用方法

如果有想要交流的内容欢迎在评论区进行留言,如果这篇文档受到了您的喜欢那就留下你点赞+收藏脚印支持一下博主~

http://www.lryc.cn/news/28790.html

相关文章:

  • 【设计模式之美 设计原则与思想:设计原则】20 | 理论六:我为何说KISS、YAGNI原则看似简单,却经常被用错?
  • Java代码弱点与修复之——Copy-paste error(复制粘贴错误)
  • Editor.md 的使用方法及图片处理
  • 剑指 Offer II 018. 有效的回文
  • Elasticsearch分析器(Analyzer)
  • P6入门:了解P6 Professional 工具栏及地图分享
  • 习题30 if elif else 语句
  • 32 openEuler使用LVM管理硬盘-管理卷组
  • Jackson CVE-2017-17485 反序列化漏洞
  • 十大排序(C++版)
  • SpringMVC中的常用注解
  • English Learning - L2-3 英音地道语音语调 小元音 [ʌ] [ɒ] [ʊ] [ɪ] [ə] [e] 2023.02.27 周一
  • fastadmin后台登录页修改
  • Java 面向对象(OOP)的三大特性
  • Java:openjdk: error: Student is abstract; cannot be instantiated;java编译环境
  • 28个案例问题分析---019---临时解决方案和最终解决方案--思想
  • 计算机网络的166个概念你知道几个 第四部分
  • Lenovo 联想-IdeaPad-Y530电脑 Hackintosh 黑苹果efi引导文件
  • mac M1 nvm安装教程,避坑
  • 【项目精选】基于网络爬虫技术的网络新闻分析(视频+论文+源码)
  • 【Python - Matplotlib】P2 plot 折线图
  • 【Verilog】——模块,常量,变量
  • 论文投稿指南——中文核心期刊推荐(电影、电视艺术)
  • Pip install 和Conda install 总结
  • 嵌入式系统实验——【玄武F103开发板】实现两个LED小灯闪烁
  • 数组之双指针题
  • 真实需求和梦想实现满足
  • [ant-design-vue] tree 组件功能使用
  • QT父子窗口事件传递与事件过滤器
  • 【2.4 golang中的循环语句for】