6.使用@Autowired注解
2016-10-12 00:05
357 查看
第一步:创建javaweb项目
第二步:导入必要的IOC包,这儿有五个
“commons-logging.jar”
“spring-beans-3.2.8.RELEASE.jar”
“spring-context-3.2.8.RELEASE.jar”
“spring-core-3.2.8.RELEASE.jar”
“spring-expression-3.2.8.RELEASE.jar”
第三步:导入spring配置文件,我这儿是applicationContext.xml,
第四步:创建并且声明bean
创建Programmer类,并使用@Component注解将其声明到容器中,然后使用@Autowired注解在构造器上将Computer注入进来,代码如下:
构造器方式注入bean
setter方式注入bean
属性加注解注入bean
第五步:写测试代码(注意这个就需要导入Junit 4的包)对应的测试代码
第六步:测试结果:
com.zxy.bean.Programer@72e8a021
com.zxy.bean.Computer@6691177
这儿详细说明注解的意义:
@Autowired是概述Spring要将数据注入进来
@Qualifier是告诉Spring要注入的bean的ID,如果要注入的bean是单例的,@Qualifier可以省略,届时Spring会类型自动找到这个单例的bean,大部分的bean是单例的,所以@Qualifier往往是省略的
第二步:导入必要的IOC包,这儿有五个
“commons-logging.jar”
“spring-beans-3.2.8.RELEASE.jar”
“spring-context-3.2.8.RELEASE.jar”
“spring-core-3.2.8.RELEASE.jar”
“spring-expression-3.2.8.RELEASE.jar”
第三步:导入spring配置文件,我这儿是applicationContext.xml,
<!-- 开启IOC组件扫描 --> <context:component-scan base-package="com.zxy"/>
第四步:创建并且声明bean
@Component("computer") public class Computer implements Serializable{ /** * */ private static final long serialVersionUID = 1L; }
创建Programmer类,并使用@Component注解将其声明到容器中,然后使用@Autowired注解在构造器上将Computer注入进来,代码如下:
构造器方式注入bean
@Component public class Programer implements Serializable{ private Computer computer; @Autowired public Programer(@Qualifier("computer")Computer computer){ this.computer = computer; } public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; } }
setter方式注入bean
@Component public class Teacher implements Serializable { private Computer computer; @Autowired public void setComputer(@Qualifier("computer") Computer computer) { this.computer = computer; System.out.println("Computer"); } public Computer getComputer() { return computer; } }
属性加注解注入bean
@Component public class Student implements Serializable { @Autowired @Qualifier("computer") private Computer computer; public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; System.out.println("Computer"); } }
第五步:写测试代码(注意这个就需要导入Junit 4的包)对应的测试代码
@Test public void test4() { String conf = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(conf); Programer prog = ctx.getBean("programer", Programer.class); System.out.println(prog); System.out.println(prog.getComputer()); }
@Test public void test5() { String conf = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(conf); Teacher teacher = ctx.getBean("teacher", Teacher.class); System.out.println(teacher); System.out.println(teacher.getComputer()); }
@Test public void test6() { String conf = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(conf); Student student = ctx.getBean("student", Student.class); System.out.println(student); System.out.println(student.getComputer()); }
第六步:测试结果:
com.zxy.bean.Programer@72e8a021
com.zxy.bean.Computer@6691177
这儿详细说明注解的意义:
@Autowired是概述Spring要将数据注入进来
@Qualifier是告诉Spring要注入的bean的ID,如果要注入的bean是单例的,@Qualifier可以省略,届时Spring会类型自动找到这个单例的bean,大部分的bean是单例的,所以@Qualifier往往是省略的
相关文章推荐
- Spring3.x--使用@Autowired注解
- 使用@Autowired注解
- 工作中问题记录--servlet中使用@Autowired注解
- 注解 @Resource与@Autowired与@Component的使用
- @Autowired注解的使用
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- 4、spring使用@Autowired注解实现自动装配
- 工作中问题记录--servlet中使用@Autowired注解
- Spring使用@Autowired注解自动装配
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- Spring、使用注解方式装配对象(@Resource、@Autowired)
- Spring中@Resource、@Autowired、@Qualifier注解的使用和区别
- Spring、使用注解方式装配对象(@Resource、@Autowired)
- Autowired注解的使用
- Spring框架使用-注解注释-@Autowired
- Spring、使用注解方式装配对象(@Resource、@Autowired)
- Spring MVC 自定义标签如何使用@Autowired自动装配注解
- @Autowired注解的使用
- 静态方法使用@Autowired注解报null问题
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。