目录
一.依赖注入DI (下)
1.依赖注入类型
(1)注入bean类型
(2)注入基本数据类型
(3)注入List集合
(4)注入Set集合
(6)注入Properties对象
二.注解实现IOC(上)
1.准备工作
2.@Component
3.@Repository、@Service、@Controller
4.@Scope
5.@Autowired
6.@Qualifier
一.依赖注入DI (下)
1.依赖注入类型
DI
支持注入
bean
类型、基本数据类型和字符串、
List
集合、
Set
集合、
Map
集合、
Properties
对象类型等,他们的写法如下:
(1)准备注入属性的类
package com.first.service;
import com.first.dao.StudentDao;
import com.first.domain.Student;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class StudentService {
private StudentDao studentDao; // bean属性
private String name; //字符串类型
private int count; //基本数据类型
private List names; // 字符串类型List集合
private List students1; // 对象类型List集合
private Set students2; // 对象类型Set集合
private Map names2; //字符串类型Map集合
private Map students3;//对象类型Map集合
private Properties properties;//Properties类型
public StudentService() {
}
public StudentService(StudentDao studentDao) {
this.studentDao = studentDao;
}
// 省略getter/setter/toString
}
(2)准备测试方法
为了方便代码可读,再重新创建一个配置文件bean1.xml
@Test
public void t2(){
//创建Spring对象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");
//从容器获取对象
StudentService studentService = (StudentService) ac.getBean("studentService");
System.out.println(studentService);
}
(1)注入bean类型
写法一: 也就是我们之前的写法(手动注入)
写法二:
(2)注入基本数据类型
10
(3)注入List集合
小明
小白
(4)注入Set集合
小明 小白
(4)注入Set集合
方式只是把上面代码的标签换成
小明 小白
(5)注入Map集合
简单数据类型 Map 集合:对象类型 Map 集合:
(6)注入Properties对象
Properties是java提供的一个类,这个类主要用于读取一些配置文件。
值1 值2
二.注解实现IOC(上)
1.准备工作
注解配置和
xml
配置对于
Spring
的
IOC
要实现的功能都是一样的,只是配置的形式不一样。
准备工作
(1)创建一个新的
Spring
项目。
2
(2)编写pojo(也就是domain)
,
dao
,
service
类。
3
前两步和之前创建新的子模块一致,相关文件复制即可,pom.xml的依赖也复制过来引入
StudentService.java:
package com.first.service;
import com.first.dao.StudentDao;
import com.first.domain.Student;
public class StudentService {
private StudentDao studentDao;
public Student findStudentById(int id){
return studentDao.findById(id);
}
}
(3)编写空的配置文件,如果想让该文件支持注解,需要添加新的约束
bean.xml也复制到resources中,并添加约束如下:
内容:
2.@Component
@Component可以代替bean标签
作用:用于创建对象,放入 Spring 容器,相当于- 要在配置文件中配置扫描的包,扫描到该注解才能生效。因为不可能将整个项目都扫描一遍,那样太浪费时间了,所以就指定让扫描哪个包。
- @Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
接口实现类StudentDaoImpl.java:
package com.first.dao;
import com.first.domain.Student;
import org.springframework.stereotype.Component;
//括号里的字符串即@Component 注解中的value属性值
@Component("studentDao")
public class StudentDaoImpl implements StudentDao{
@Override
public Student findById(int id) {
//这里直接返回一个学生对象,模拟从数据库查询
return new Student(1,"断线","北京");
}
}
业务实现类StudentService.java:
没配置id值,id值就默认是首字母小写的类名,即studentService
package com.first.service;
import com.first.dao.StudentDao;
import com.first.domain.Student;
import org.springframework.stereotype.Component;
//@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
@Component
public class StudentService {
private StudentDao studentDao;
public Student findStudentById(int id){
return studentDao.findById(id);
}
}
测试类:
TestIoc.java
package com.first;
import com.first.dao.StudentDao;
import com.first.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIoc {
@Test
public void t1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
System.out.println(studentDao);
//@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
StudentService studentService = (StudentService) ac.getBean("studentService");
System.out.println(studentService);
}
}
输出:
com.first.dao.StudentDaoImpl@79ca92b9 com.first.service.StudentService@1460a8c0
3.@Repository、@Service、@Controller
@Repository、@Service、@Controller和@Component的作用一样。
作用:这三个注解和 @Component 的作用一样,使用它们是为了区分该类属于什么层。 位置:- @Repository用于Dao层
- @Service用于Service层
- @Controller用于Controller层
示例:
StudentDaoImpl.java:
package com.first.dao;
import com.first.domain.Student;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
//括号里的字符串即@Component 注解中的value属性值
//@Component("studentDao")
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao{
@Override
public Student findById(int id) {
//这里直接返回一个学生对象,模拟从数据库查询
return new Student(1,"断线","北京");
}
}
StudentService.java:
package com.first.service;
import com.first.dao.StudentDao;
import com.first.domain.Student;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
//@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
//@Component
@Service
public class StudentService {
private StudentDao studentDao;
public Student findStudentById(int id){
return studentDao.findById(id);
}
}
输出和上面一样!
4.@Scope
@Scope指定bean的创建策略。放了哪个类上面就对哪个类起作用!
作用:指定 bean 的创建策略 位置:类上方 取值: singleton prototype request session globalsessionStudentService.java:
package com.first.service;
import com.first.dao.StudentDao;
import com.first.domain.Student;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
//@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
//@Component
@Service
@Scope("singleton")
public class StudentService {
private StudentDao studentDao;
public Student findStudentById(int id){
return studentDao.findById(id);
}
}
测试函数:
@Test
public void t1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
System.out.println(studentDao);
//@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
StudentService studentService1 = (StudentService) ac.getBean("studentService");
StudentService studentService2 = (StudentService) ac.getBean("studentService");
System.out.println(studentService1);
System.out.println(studentService2);
}
输出:
com.first.dao.StudentDaoImpl@6b4a4e18 com.first.service.StudentService@27c86f2d com.first.service.StudentService@27c86f2d
改成@Scope("prototype")后,输出如下:
com.first.dao.StudentDaoImpl@6b4a4e18 com.first.service.StudentService@27c86f2d com.first.service.StudentService@197d671
5.@Autowired
@Autowired可以完成依赖自动注入。
作用:从容器中查找符合属性类型的对象自动注入属性中。用于代替在StudentService中实现get和set方法
先测试一下:
@Test
public void t2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
StudentService studentService = (StudentService) ac.getBean("studentService");
System.out.println(studentService);
System.out.println(studentService.getStudentDao());
}
输出:
com.first.service.StudentService@6b4a4e18 null
可见studentService的类里的属性studentDao现在为null
在StudentService中添加构造函数
注意:
(1)@Autowired 写在属性上方进行依赖注入时,可以省略setter方法。
属性上方、setter方法上方、构造方法上方(必须是有参构造方法)这三个位置都自己试一下,都可以!
package com.first.service;
import com.first.dao.StudentDao;
import com.first.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
//@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
//@Component
@Service
@Scope("singleton")
public class StudentService {
@Autowired
private StudentDao studentDao;
public StudentService() {
}
public StudentService(StudentDao studentDao) {
this.studentDao = studentDao;
}
public StudentDao getStudentDao() {
return studentDao;
}
public Student findStudentById(int id){
return studentDao.findById(id);
}
}
(2)容器中没有对应类型的对象会报错
即就没生成对象。
// 如果StudentDaoImpl没有放到容器中会报错
//@Component("studentDao")
public class StudentDaoImpl implements StudentDao{
public Student findById(int id) {
// 模拟根据id查询学生
return new Student(1,"笑傲名","长安");
}
}
(3)容器中有多个对象匹配类型时,会找beanId等于属性名的对象,找不到会报错。
虽然StudentDaoImpl1和StudentDaoImpl2类型一致(都是StudentDao的实现类),但对象名studentDao和studentDaoImpl不一致,优先找beanId等于属性名的对象,即studentDao。
// 如果容器中都多个同类型对象,会根据id值等于属性名找对象
@Component("studentDao")
public class StudentDaoImpl1 implements StudentDao{
public Student findById(int id) {
// 模拟根据id查询学生
return new Student(1,"小明","洛阳");
}
}
//不具体指定默认是studentDaoImpl
@Component
public class StudentDaoImpl2 implements StudentDao{
public Student findById(int id) {
// 模拟根据id查询学生
return new Student(1,"小明","洛阳");
}
}
6.@Qualifier
@Qualifier必须和@Autowired-起使用。
作用:在按照类型注入对象的基础上,再按照 bean 的 id 注入。 位置:属性上方 注意: @Qualifier 必须和 @Autowired 一起使用。 比如上面那个(3)的例子,我就是想用StudentDaoImpl2的对象而不用StudentDaoImpl1的对象。那么就在StudentService类的studentDao属性上添加@Qualifier("对象id"),在这里即@Qualifier("studentDaoImpl2"),如下:
@Component
public class StudentService {
@Autowired
@Qualifier("studentDaoImpl2")
private StudentDao studentDao;
public Student findStudentById(int id){
return studentDao.findById(id);
}
}



