栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring 注解创建对象

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring 注解创建对象

1.通过注解方法创建对象

1.1.1 @Component 创建对象 value 对象名称(spring 唯一)

@Component(value = "myStudent")

等同于

1.1.2 省略value

@Component("myStudent")

1.1.3 直接省略

@Component

容器获得时直接类名小写

Student s= (Student) ac.getBean("student");

1.2 声明扫描组件 base-package 为Component创建的对象文件目录

1.3 创建容器并获得对象

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Student s= (Student) ac.getBean("myStudent");
System.out.println(s);

spring 中同类型的还有

@Repository 
@Service 
@Controller 

用法同Component一致,作用:给项目分层

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340574.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号