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

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

相关内容

热门资讯

缅北白家致中国公民6人死亡 【#缅北白家致中国公民6人死亡#】#缅北白家涉赌资金超180亿元#公安部部署开展打击缅北涉我犯罪专项...
2025年GEO排名优化公司榜... 数字化浪潮席卷全球的当下,AI搜索已然成为企业提升品牌影响力、抢占市场先机的战略要地。当用户在Dee...
大项目、大订单陆续交付 博隆技... 7月11日,博隆技术发布业绩预告。经公司财务部初步测算,2025年上半年,公司预计实现归属于母公司所...
如何理解特定股票代码开头的含义... 在股票投资领域,理解特定股票代码开头的含义至关重要,它不仅是识别股票的关键标识,还能为股票分析提供有...
东方财富成交额逾百亿! 格隆汇7月11日|东方财富涨超4%,成交额逾100亿元。
港股异动丨药明康德业绩亮眼,刺... 格隆汇7月11日|港股市场CRO概念股集体走强,其中,凯莱英涨超15%,药明康德涨12%,昭衍新药、...
【光明论坛】奋力书写挺膺担当的... 来源:国际在线近日,中华全国青年联合会第十四届委员会全体会议、中华全国学生联合会第二十八次代表大会在...
盛天网络投资成立互娱科技公司 人民财讯7月11日电,企查查APP显示,近日,武汉市带带互娱科技有限公司成立,法定代表人为杨影,注册...
金融科技板块活跃 安硕信息涨幅...   07月11日消息,截止10:45,金融科技板块活跃,瑞达期货涨停,安硕信息、恒宝股份(维权)、国...
石文先委员:构建符合时代要求的... 转自:人民政协报企业是国家经济核心构成和根本支撑。日前,中共中央办公厅、国务院办公厅发布了《关于完善...