1、注解的方式获取对象

(1)导包:

(2)书写配置文件(要保证已经导入了约束):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="pers.zhb.domain"></context:component-scan>
</beans>

配置文件的核心就一句话,它的作用是:扫描pers.zhb.domain包下的所有类的注解。

(3)创建Student对象,并在里面添加注解:

import org.springframework.stereotype.Component;
@Component("student")
public class Student {
private String snum;
private String sname;
private String sex;
private Course course;
public Student(String snum, String sname, String sex, Course course) {
this.snum = snum;
this.sname = sname;
this.sex = sex;
this.course = course;
}
public Course getCourse() {
return course;
} public void setCourse(Course course) {
this.course = course;
} public Student(){
System.out.println("Student对象创建了!");
}
public String getSnum() {
return snum;
} public void setSnum(String snum) {
this.snum = snum;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"snum='" + snum + '\'' +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
", course=" + course +
'}';
}
}

该注解中的参数student相当于<bean>里面的name,通过student可以获取到Student对象。

(4)创建测试类:

public class Test {
public void test1(){
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
Student student =(Student)applicationContext.getBean("student");
student.setSname("zhai");
System.out.println(student);
}
public static void main(String[] args){
Test test=new Test();
test.test1();
}
}

(5)注解的四种方式:

第二个可以用于Service层对象的创建,第三个web层,第四个dao层。这四个注解只是名字不同而已,功能是相同的。

2、对象的单例 / 多例

(1)单例对象

测试:

Student student1  =(Student)applicationContext.getBean("student");
Student student2 =(Student)applicationContext.getBean("student");
System.out.println(student1==student2);

返回结果为true,说明创建的是同一个对象。在spring容器中只存在一个bean的实例,bean以单里的形式存在

(2)多例对象

测试:

Student student1  =(Student)applicationContext.getBean("student");
Student student2 =(Student)applicationContext.getBean("student");
System.out.println(student1==student2);

返回的结果为false,创建的是两个不同的对象。每次调用getBean()的时候都会返回一个新的实例

3、值的注入

(1)在属性处赋值:

此种方式破坏了封装性,不推荐,因此set方式更优。

(2)在set方法处赋值:

(3)注入引用数据类型的数据:

方式一:

创建Course对象,并将对象引入到容器:

但是,这种方式有一个弊端,就是在有多个对象的情况下,将无法选择具体选择哪一个。

方式二:

指定注入哪一个对象。

4、引入配置文件创建Spring容器

用此方法需要导入jar包:

ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象

创建容器的代码可以用注解代替:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")

采用这种方式只需要在类中写一次,因此,不需要在每一个方法中都写获取容器的代码,可以减少代码量。

最新文章

  1. 打造AngularJs2.0开发环境
  2. python 安装模块
  3. ImageView
  4. 替罪羊树模板(BZOJ1056/1862)
  5. Python关键字yield的解释(stackoverflow)
  6. jdbc插入修改clob类型的两种方式
  7. php 对问卷结果进行统计
  8. firefox无法使用yslow的解决方案
  9. C#中string和byte[]相互转换问题解决
  10. Activex、OLE、COM、OCX、DLL之间有什么区别?
  11. idea中classpath的问题
  12. 磁盘缓存--YYCache 设计思路
  13. Web版记账本开发记录(一)代码和功能展示
  14. PHP之基本操作
  15. 关于maven的CoreException: Could not get the value for parameter compilerId for plugin 。。的错误
  16. linux下软件安装知识整理
  17. Feign 的简单使用(2)
  18. css中position:fixed实现div居中
  19. 向SQL Server 现有表中添加新列并添加描述.
  20. [Java算法分析与设计]--链式堆栈的设计

热门文章

  1. Federated Learning with Matched Averaging
  2. 学习seo赚钱一定要有超前的思路和眼光
  3. 单元测试框架 python
  4. HDU - 3499 -(Dijkstra变形+枚举边)
  5. Left Mouse Button (bfs)
  6. 深入了解Netty【五】线程模型
  7. Zabbix如何监控Linux防火墙服务
  8. redis基础数据结构及编码方式
  9. Nginx在mvvm模式中的使用
  10. leetcode刷题-95/96/98