Spring——Bean管理-注解方式进行属性注入
创始人
2024-05-28 19:29:24
0

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();}
}

-filtertype="" expression=""/>

属性注入

@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使用方法

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

相关内容

热门资讯

新还珠格格,欣荣和永琪有个孩子... 新还珠格格,欣荣和永琪有个孩子?不是说永琪从来都没碰过她吗?绵忆到底是他和小燕子的还是欣荣的啊求正解...
中级会计怎么备考?今年几月考试... 中级会计怎么备考?今年几月考试?您好,很高兴为您解答中级会计师考试,教材是根本和基础,所有的题目都是...
继兴业、招商、中信后,邮储银行... (来源:现代商业银行杂志)金融资产投资公司(AIC)队伍再添新员。邮储银行近日发布公告称,该行拟以自...
中央巡视组对陕西开展两个半月常... 转自:北京日报客户端日前,中央第十五巡视组进驻陕西省,将开展为期两个半月左右的常规巡视,并会同陕西省...
柳州幻境空间在哪里 柳州幻境空间在哪里柳州幻境空间是位于广西柳州市城中区华联商闷郑城4楼的室内主题乐园,提供了各种游戏和...
中央巡视组进驻山东 联动巡视济... 转自:央视新闻客户端经党中央批准,二十届中央第六轮巡视将对16个省(自治区、直辖市)开展常规巡视,并...
继续发布暴雨蓝色预警!北京等地... 转自:央视新闻客户端中央气象台19日早6时继续发布暴雨蓝色预警。预计,19日早8时至20日早8时,青...
降妖伏魔篇演员有哪些 降妖伏魔篇演员有哪些文章舒淇程小东黄勃
晚上十一点在河边抓鱼听到有人叫... 晚上十一点在河边抓鱼听到有人叫我小名声音跟我一个朋友一样,电筒照却没有发现有人而且我女朋友也听见了不...
属猴的为什么吸引属狗的人 属猴的为什么吸引属狗的人属相狗虽不善甜言蜜语,为人多有情感之被捉,然其铅轮内心却多有向往甜蜜幸福之生...