Skip to content

Commit 653129f

Browse files
committed
upload
1 parent 9471acd commit 653129f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2562
-0
lines changed

javaframework/spring/第四节课/code/spring_annotation_study/.idea/compiler.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javaframework/spring/第四节课/code/spring_annotation_study/.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javaframework/spring/第四节课/code/spring_annotation_study/.idea/workspace.xml

Lines changed: 787 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.mashibing</groupId>
8+
<artifactId>spring_annotation_study</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
<dependencies>
13+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
14+
<dependency>
15+
<groupId>org.springframework</groupId>
16+
<artifactId>spring-context</artifactId>
17+
<version>5.2.3.RELEASE</version>
18+
</dependency>
19+
<!-- https://mvnrepository.com/artifact/junit/junit -->
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.12</version>
24+
<scope>test</scope>
25+
</dependency>
26+
27+
</dependencies>
28+
</project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.mashibing.bean;
2+
3+
public class Student {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.mashibing.bean;
2+
3+
public class Teacher {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mashibing.controller;
2+
3+
import com.mashibing.dao.PersonDao;
4+
import com.mashibing.service.PersonService;
5+
import com.mashibing.service.PersonService2;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.stereotype.Controller;
9+
10+
import javax.annotation.Resource;
11+
12+
@Controller
13+
//@Scope(value = "prototype")
14+
public class PersonController {
15+
16+
/**
17+
* 通过@AutoWired注解能够完成自动注入的功能
18+
* 是按照什么方式进行自动注入的呢?
19+
* 默认情况下是按照ByType来进行装配的,如果找到直接赋值,找不到报错
20+
* 如果有多个类型一样的bean对象,此时会按照id来进行查找,默认的id是类名首字符小写
21+
* 如果找到了直接注入,如果找不到那么就报错
22+
* <p>
23+
* 如果你想通过名字进行查找,可以自己规定名称,使用注解@Qualifier
24+
* <p>
25+
* 当@AutoWired添加到方法上的时候,此方法在创建对象的时候会默认调用,同时方法中的参数会进行自动装配
26+
*
27+
* @Qualifier注解也可以定义在方法的参数列表中,可以指定当前属性的id名称 使用@Resource可以完成跟@AutoWired相同的功能,但是要注意他们之间的区别
28+
* 1、@Resource是jdk提供的功能,@AutoWired是spring提供的功能
29+
* 2、@Resource可以在其他框架中使用,而@AutoWired只能在spring中使用
30+
* 换句话说:@Resource扩展性好,而@AutoWired支持的框架比较单一
31+
* 3、@Resource是按照名称进行装配的,如果名字找不到,那么就使用类型
32+
* 而@AutoWired是按照类型进行装配,如果类型找不到那么久使用名字进行查找
33+
*/
34+
@Resource
35+
// @Autowired
36+
// @Qualifier("personService")
37+
private PersonService personService;
38+
39+
public void save() {
40+
personService.save();
41+
}
42+
43+
// @Autowired
44+
// public void test(@Qualifier("personDao") PersonDao personDao123) {
45+
// System.out.println("test");
46+
// personDao123.update();
47+
// }
48+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.mashibing.dao;
2+
3+
public abstract class BaseDao<T> {
4+
5+
public abstract void save();
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.mashibing.dao;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
@Repository
6+
public class PersonDao {
7+
8+
public void save(){
9+
System.out.println("保存成功");
10+
}
11+
12+
public void update(){
13+
System.out.println("更新成功");
14+
}
15+
}

0 commit comments

Comments
 (0)