Spring 学习记录 4 扫描包
2014-10-22 13:01
405 查看
上篇介绍了基于注解的自动装配。
这篇将介绍基于注解的声明 bean。
将 noname.xml 稍作修改
好了,记住 <context:component-scan> 这个标签吧,这个上次用到的标签的爸爸。
不仅启用了注解,还设定了要扫描的包。
这意味着 Spring 容器会去 ivo 包下,扫描所有类,
当类拥有 @Component (不只是) 注解时,就被认为是需要容器管理的 bean。
这篇将介绍基于注解的声明 bean。
将 noname.xml 稍作修改
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="ivo"/> </beans>
好了,记住 <context:component-scan> 这个标签吧,这个上次用到的标签的爸爸。
不仅启用了注解,还设定了要扫描的包。
这意味着 Spring 容器会去 ivo 包下,扫描所有类,
当类拥有 @Component (不只是) 注解时,就被认为是需要容器管理的 bean。
@Component public class BowAndArrow implements Weapon{ public void killEnemy(){ System.out.println("Kill enemy with bow and arrow"); } }
@Component public class Person { @Autowired private Weapon weapon; public void killEnemy() { weapon.killEnemy(); } }
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("noname.xml"); Person person = (Person)applicationContext.getBean("person"); person.killEnemy(); }
相关文章推荐
- jqGrid分页技术学习(三)spring jdbc 通用DAO——添加一条记录
- jqGrid分页技术学习(五)spring jdbc 通用DAO——修改一条记录
- Spring 学习记录 2 Spring的依赖注入
- Spring学习记录一(SpringIOC容器初级)
- kali学习记录之端口扫描
- ITCAST视频-Spring学习笔记(让Spring自动扫描和管理Bean)
- 学习Spring源码记录(一)
- spring4.1+hibernate4.3整合学习 问题记录1
- Spring mvc ,spring ,ibatis 学习记录(2)
- 学习记录--spring中的RowMapper
- jqGrid分页技术学习(六)spring jdbc 通用DAO——查询所有记录
- Spring学习记录
- 页面每次添加都显示最后一次访问记录spring scope="prototype" 学习笔记
- Spring 学习记录 冷兵器时代的故事
- maven学习记录2.3:spring-mybatis.xml配置
- jqGrid分页技术学习(四)spring jdbc 通用DAO——删除一条记录
- spring security3学习记录
- 记录Spring.net学习中遇到的各种问题
- Spring 学习记录 5
- spring AOP学习记录1