spring的依赖注入 注解版
来源:互联网 发布:电脑防蓝光软件 编辑:程序博客网 时间:2024/05/29 18:19
要说的都在注解里面了
创建Person和Student类
public class Person {/** * */@Resource(name="student")//@Autowired//按照类型匹配,这是spring框架提供的一种注解方式//@Qualifier("student") //按照spring容器中bean的id进行匹配,下面两个注解的作用等于上面的注解private Student student;public void setStudent(Student student) {this.student = student;}public Student getStudent() {return student;}}
public class Student{public void say(){System.out.println("hello");}}
首先更改applicationContext.xml中的头信息
<?xml version="1.0" encoding="UTF-8"?><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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="person" class="com.mo.entity.Person"></bean> <bean id="student" class="com.mo.entity.Student"></bean> <!-- 启动注解的驱动 --> <context:annotation-config/> <!-- 原理: 1.当加载spring的配置文件时,加载两个bean到spring容器中去 2.因为启动了注解驱动,spring会在容器中寻找bean的属性 上面有@Resource注解的,并将其属性的属性名与spring容器中bean的id对照, 一致的话就进行依赖注入,不一致的话就按照属性的类型与spring容器bean的属性的类型进行匹配,类型一致就进行注入,若spring容器中不存在这种类型,那么将报错 如果注解是这种形式@Resource(name="xxx"),就按照name中的值与spring容器中bean的id值进行匹配,一致进行注入,若不存在这个id就报错 注意:注解只适应于引用类型 --></beans>
测试
@Testpublic void test4(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Person person = (Person)context.getBean("person");person.getStudent().say();}输出就是hello
spring的类扫描
首先applicationContext.xml的头文件要更改
<?xml version="1.0" encoding="UTF-8"?><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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 类扫描启动,在base-package指定的包及子包下扫描所有带有特定注解的类 --> <context:component-scan base-package="com.mo.entity"/> </beans>
/* * @Component注解会将Person类名 翻译为<bean id="person" class=".."/> * 就是将类名的第一个字母小写,变成spring容器中的一个bean * * 如果是这样写@Component("per") 就会翻译为<bean id="per" class=""/> */@Componentpublic class Person {
@Componentpublic class Student{
当加载applicationContext.xml配置文件时,这两个类就会被将加载到spring的容器中
0 0
- spring的依赖注入 注解版
- spring依赖注入的注解
- Spring依赖注入:注解注入
- Spring各种依赖注入注解的区别
- Spring各种依赖注入注解的区别
- spring-依赖注入的注解配置
- Spring各种依赖注入注解的区别
- Spring:注解版依赖注入+注入collection值演示
- spring annotation注解 依赖注入
- spring依赖注入(2)-注解
- Spring使用注解依赖注入
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- 由小到大排序(含负数、小数)
- CodeForces 644 B.Processing Queries(队列)
- ubuntu16.04 搜狗输入法无法输入中文
- Spring AOP 详解
- 求100以内的素数,全部打印出来
- spring的依赖注入 注解版
- 题目1475:IP数据包解析
- 算法时间复杂度(二)
- SQL中LIKE '%' 与 PreparedStatement的占位符 ? 使用方法
- Java 直接运行jar里的某个类
- POJ1611 The Suspects
- 邻接表存储无向图
- 根据内容获取web高度(动态获取web高度)
- 【Java学习20170427】Servlet过滤器和监听器