从头认识Spring-2.7 自己主动检測Bean(1)-@Component @Repository @Service @Controller
2024-09-05 14:35:53
这一章节我们来讨论一下自己主动检測Bean。
1.domain
厨师类:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class Chief { @Value("jack")
private String name = ""; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
除了上面的默认配置。我们还能够自己加上bean的id
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("jack")
public class Chief { @Value("jack")
private String name = ""; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
厨师类这里我们使用@Component来标注这个Bean,默认id是chief
事实上还有其它标签能够作为bean的标注
@Component-作为构件来配置bean
@Repository-标识数据仓库
@Service-标识服务
@Controller-标识MVC的控制器
可是上面的几个标签的作用大同小异,都是标注有这么一个bean
2.測试类:
(1)以下是通过.class来提取bean
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_19/ApplicationContext-test.xml" })
public class ChiefTest { @Autowired
private ApplicationContext applicationContext; @Test
public void testChief() {
Chief jack = (Chief) applicationContext.getBean(Chief.class);
System.out.println(jack.getName());
}
}
(2)因为上面的bean没有标注id,因此,它的默认id是chief,我们也能够通过id来提取
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_19/ApplicationContext-test.xml" })
public class ChiefTest { @Autowired
private ApplicationContext applicationContext; @Test
public void testChief() {
Chief jack = (Chief) applicationContext.getBean("chief");
System.out.println(jack.getName());
}
}
3.配置文件(最简单)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan
base-package="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19" />
</beans>
測试输出:
jack
总结:这一章节我们主要介绍自己主动检測Bean。
文件夹:http://blog.csdn.net/raylee2007/article/details/50611627
最新文章
- html常用标签介绍
- HDU 5964 平行四边形
- windows 下的定时任务
- Oracle学习整理
- ArrayAdapter适配器的用法,模拟QQ发消息界面。
- [置顶] 学习鸟哥的Linux私房菜笔记(6)——过滤器、输入输出及管道
- iOS 使用SBJSON创建和解析JSON
- bjective-C 中核心处理字符串的类是 NSString 与 NSMutableString
- 两年JAVA程序员的面试总结
- (二十六)静态单元格(Cell)
- windows安装oracle遇INS 30131 错误
- Android切换横竖屏不销毁前台Activity,也不影响后台Activity
- SQLyog 字体设置
- 简述 Spring Cloud 是什么2
- mysql自定义函数收集
- ACM java写法入门
- 【算法笔记】B1004 成绩排名
- sqoop学习3(数据导入乱码问题)
- iOS开发中的系统版本比较
- Spring+SpringMVC+Mybatis+Shiro环境搭建之IDEA下搭建Maven项目
热门文章
- Ambari架构及安装
- [转]发布基于T4模板引擎的代码生成器[Kalman Studio]
- 新认知之WinForm窗体程序
- (转)在 vue-cli 脚手架中引用 jQuery、bootstrap 以及使用 sass、less 编写 css [vue-cli配置入门]
- HTML CSS, JavaScript 计算器
- CSS——padding
- 重绘DataGridView的DataGridViewCheckBoxCell控件
- linux下vim命令汇总
- 11、scala函数式编程
- 2015.12.20-2015.12.25 大论文迭代 A