首页 技术 正文
技术 2022年11月23日
0 收藏 526 点赞 2,636 浏览 2964 个字

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")

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,361
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853