前言
注解配置和xml配置对于Spring的IOC要实现的功能都是一样的,只是配置的形式不一样。
准备工作:
- 创建一个新的Spring项目。
- 编写pojo,dao,service类。
- 编写空的配置文件,如果想让该文件支持注解,需要在bean.xml添加新的约束:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> < context:component-scan base-package = "com.example" /> < context:property-placeholder location = "db.properties" /> </ beans > |
一、@Component
@Component可以代替bean标签
- 作用:用于创建对象,放入Spring容器,相当于 <bean id="" class="">
- 位置:类上方
- 注意:@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// 此时bean的id为studentDaoImpl @Component public class StudentDaoImpl implements StudentDao{ public Student findById( int id) { // 模拟根据id查询学生 return new Student( 1 , "程序员" , "北京" ); } // 此时bean的id为studentDao @Component ( "studentDao" ) public class StudentDaoImpl implements StudentDao{ public Student findById( int id) { // 模拟根据id查询学生 return new Student( 1 , "程序员" , "北京" ); } } |
二、@Repository、@Service、@Controller
作用:这三个注解和@Component的作用一样,使用它们是为了区分该类属于什么层。
位置:
- @Repository用于Dao层
- @Service用于Service层
- @Controller用于Controller层
1
2
3
4
|
@Repository public class StudentDaoImpl implements StudentDao{} @Service public class StudentService {} |
三、@Scope
作用:指定bean的创建策略
位置:类上方
取值:singleton prototype request session globalsession
1
2
3
|
@Service @Scope ( "singleton" ) public class StudentService {} |
测试一下:
1
2
3
4
5
6
7
8
9
10
|
@Test public void t1(){ ApplicationContext ac = new ClassPathXmlApplicationContext( "bean.xml" ); StudentDao studentDao = (StudentDao) ac.getBean( "studentDao" ); System.out.println(studentDao); StudentService service1 = (StudentService) ac.getBean( "studentService" ); System.out.println(service1.hashCode()); StudentService service2 = ac.getBean( "studentService" ,StudentService. class ); System.out.println(service2.hashCode()); } |
OK,确实可以
四、@Autowired
作用:从容器中查找符合属性类型的对象自动注入属性中。用于代替 <bean> 中的依赖注入配置。
位置:属性上方、setter方法上方、构造方法上方。
注意:@Autowired 写在属性上方进行依赖注入时,可以省略setter方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Component public class StudentService { @Autowired private StudentDao studentDao; public Student findStudentById( int id){ return studentDao.findById(id); } } // 测试方法 @Test public void t2(){ ApplicationContext ac = new ClassPathXmlApplicationContext( "bean.xml" ); StudentService studentService = (StudentService) ac.getBean( "studentService" ); System.out.println(studentService.findStudentById( 1 )); } |
测试结果:
OK,也是可以的
五、@Qualifier
作用:在按照类型注入对象的基础上,再按照bean的id注入。
位置:属性上方
注意:@Qualifier必须和@Autowired一起使用。
如下
1
2
3
4
5
6
7
8
9
|
@Component public class StudentService { @Autowired @Qualifier ( "studentDaoImpl2" ) private StudentDao studentDao; public Student findStudentById( int id){ return studentDao.findById(id); } } |
六、@Value
作用:注入String类型和基本数据类型的属性值。
位置:属性上方以下说明一下用法:
1. 直接设置固定的属性值
1
2
3
4
5
|
@Value ( "1" ) private int count; @Value ( "hello" ) private String str; |
2. 获取配置文件中的属性值
编写配置文件db.properties
1
2
|
jdbc.username=root jdbc.password=123456 |
spring核心配置文件(bean.xml)扫描配置文件
1
|
< context:property-placeholder location = "db.properties" /> |
注入配置文件中的属性值
1
2
3
4
5
|
@Value ( "${jdbc.username}" ) private String username; @Value ( "${jdbc.password}" ) private String password; |
3. 测试结果
测试方法
1
2
3
4
5
6
7
|
// 测试注解Value @Test public void t3(){ ApplicationContext ac = new ClassPathXmlApplicationContext( "bean.xml" ); StudentService service = ac.getBean( "studentService" ,StudentService. class ); System.out.println(service); } |
运行结果
OK,应该和上面设置的值一样,说明可以使用,本篇就介绍到这几个注解了,下篇会介绍完接下来的注解。
以上就是Spring IOC相关注解运用(上篇)的详细内容,更多关于Spring IOC注解的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/qq_53317005/article/details/129875785