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

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

相关内容

热门资讯

夜经济有霓虹闪烁,也有数字赋能... 转自:上观新闻助力城市夜经济释放消费市场新活力,高德地图日前启动代驾暑促活动。今年以来,多地出台相关...
优碧欧维生素DK镁为冠军赋能!...   2025年6月底,全国现代五项锦标赛圆满落幕,UBIO品牌大使罗帅凭借卓越的体能与出色的技能,一...
孟美岐和杨超越谁更火? 孟美岐和杨超越谁更火? 孟美岐和杨超越两个人我都很喜欢,她们两个都很优秀,但是孟美岐应该更火一些。火...
江阴银行:副行长王凯因工作调动... 7月7日,江阴银行发布公告称,近日该行董事会收到副行长王凯的书面辞职报告。王凯因工作调动,辞去副行长...
问仙的介绍 问仙的介绍问仙,是上海部分农村地区的一种愚昧的陋俗。旧社会,农村缺医少药,有些颤明知家庭见病人久病不...
过眼云眼皆是尘怎么接下一句? 过眼云眼皆是尘怎么接下一句?上一句是,过眼云烟皆是尘,下一句是,缠腰财富俱为幻。如是对联,出句尾字是...
量化交易新规正式实施,对高频策... 转自:第一财经量化交易新规7日正式实施。今年4月,沪深北交易所发布《程序化交易管理实施细则》(下称《...
大提琴主奏的著名乐曲有哪几个? 大提琴主奏的著名乐曲有哪几个?《辛德勒名单》,Luka sulic(克罗地亚组合的成员之一),这首是...
新加坡数据中心 NTT DC ...   新加坡数据中心房地产投资信托基金 NTT DC REIT 周一表示,计划通过在本国交易所进行首次...
美云商城的东西是真的吗? 美云商城的东西是真的吗?这家的所有商品从源头杜绝假货,商品均经过严格甄选,保证商品品质刚才说的话也没...