Skip to content

Commit e50ddc5

Browse files
committed
fix:Spring相关面试题
1 parent ad6c62c commit e50ddc5

12 files changed

Lines changed: 197 additions & 53 deletions

File tree

design-pattern/src/main/java/com/github/guang19/designpattern/singleton/Eager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ public static Eager getInstance()
2121
{
2222
return eager;
2323
}
24+
2425
}
Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.guang19.designpattern.singleton;
22

33
import java.util.TreeMap;
4+
import java.util.UUID;
45

56
/**
67
* @author yangguang
@@ -11,6 +12,15 @@ public class Lazy
1112
{
1213
private static volatile Lazy lazy = null;
1314

15+
public static final String s = "a";
16+
17+
public static final String s2 = UUID.randomUUID().toString();
18+
19+
static
20+
{
21+
System.out.println("init");
22+
}
23+
1424
private Lazy(){}
1525

1626
//懒汉式只在第一次获取的时候创建单例
@@ -28,30 +38,4 @@ public static Lazy getLazy()
2838
}
2939
return lazy;
3040
}
31-
32-
static class T{
33-
private String name;
34-
35-
public T()
36-
{ }
37-
38-
public String getName()
39-
{
40-
return name;
41-
}
42-
43-
public void setName(String name)
44-
{
45-
this.name = name;
46-
}
47-
}
48-
49-
public static void main(String[] args)
50-
{
51-
TreeMap<T,Object> treeMap = new TreeMap<>();
52-
treeMap.put(new T(),"a");
53-
treeMap.put(new T(),"b");
54-
treeMap.put(new T(),"c");
55-
System.out.println(treeMap);
56-
}
5741
}

spring-learning/Spring.md

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,63 @@
1-
## Spring复习
1+
## Spring常见面试题
22

3-
对于Spring的源码我不想一段一段的贴,毕竟Spring的代码体量是相当多了,不过我不贴不代表不看,相反必须看,因为Spring加载容器时的拓展点很多,有必要搞清楚这些拓展点的执行顺序.
4-
然后我整理下关于Spring的常见问题,这些问题的回答有我个人的理解,也有参考网上一些大神的文章.
5-
---
6-
7-
#### 什么是Spring Framework?
8-
Spring是一个轻量级,开源的Java应用开发框架.它使开发Java应用程序变得更加简单,并且使用它开发可以降低应用架构之间的耦合性.
3+
#### 什么是Spring Framework
4+
Spring是一个轻量级的,开源的Java应用程序开发框架。它提供的IOC和AOP等核心功能,能够使开发者很方便的开发出松耦合的应用。
95

10-
#### Spring Framework的好处?
11-
1. 应用程序模块之间的解耦
12-
2. 组件的自由度(Spring提供了各种不同类型的组件(aop,tx,dao,web等等)用户可以自由挑选需要组件与Spring无缝集成)
13-
3. 开源免费
6+
#### Spring的优缺点
7+
##### Spring的优点:
8+
1. 方便解耦,简化开发
9+
将系统的类之间的复杂关系,交由Spring IOC工厂容器管理。
10+
11+
2. AOP支持
12+
Spring 提供 AOP模块,能够很方便的编写出AOP程序.
13+
14+
3. 声明式事务
15+
只需要通过配置或注解就可以完成对事务的支持,而不需要手动的编写事务代码
16+
17+
4. 第三方框架无缝集成
18+
Spring可以很方便的将第三方框架继承到系统中,很灵活。
19+
20+
...
21+
##### Spring的缺点:
22+
1. 复杂
23+
Spring发展到现在,确认有些复杂了,但是对于它解决的问题来说,复杂已经不算什么了。
24+
25+
2. 效率
26+
Spring内部依赖反射,而反射的效率正是它的缺点。
1427

15-
...
16-
#### Spring Framework提供了哪些模块?
17-
1. IOC模块: core,bean,context,spel,aop...
18-
2. DAO模块: jdbc,orm,oxm,transaction,jms,tx...
19-
3. WEB模块: web,mvc,webflux,websocket...
20-
4. AOP模块: aop,aspectj....
21-
5. 测试模块: test
28+
29+
#### Spring 主要提供了哪些模块
30+
1. core模块提供IOC和DI等核心功能
31+
32+
2. aop模块提供面向切面编程的实现
33+
34+
3. web模块提供对web应用的支持
35+
36+
4. dao模块提供数据库方面的支持
37+
38+
5. test模块提供测试方面的支持
39+
40+
#### Spring主要使用了哪些设计模式
41+
```text
42+
Spring使用的设计模式有很多,此处只列举几个常见的
43+
```
44+
1. 工厂模式
45+
BeanFactory 就是简单工厂的实现,用来创建和获取Bean
46+
47+
2. 单例模式
48+
Spring容器的Bean默认是单例的
49+
50+
3. 代理模式
51+
aop使用的就是代理模式
52+
53+
4. 模板方法模式
54+
jdbcTemplate等就使用模板方法模式
55+
56+
5. 观察者模式
57+
当有事件触发时,该事件的监听者(观察者)就会做出相应的动作。
58+
...
2259

23-
#### Spring IOC容器的配置方式有哪些?
60+
#### Spring IOC容器的配置方式有哪些?
2461
1. xml (不再推荐使用)
2562
2. 注解(推荐使用)
2663
3. Java API(和注解一起使用)

spring-learning/pom.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@
2626
<groupId>org.springframework</groupId>
2727
<artifactId>spring-beans</artifactId>
2828
</dependency>
29-
<dependency>
30-
<groupId>org.springframework</groupId>
31-
<artifactId>spring-aop</artifactId>
32-
</dependency>
33-
<dependency>
34-
<groupId>org.springframework</groupId>
35-
<artifactId>spring-tx</artifactId>
36-
</dependency>
3729
<!--lombok-->
3830
<dependency>
3931
<groupId>org.projectlombok</groupId>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.guang19.spring;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
/**
6+
* @author yangguang
7+
* @date 2020/3/7
8+
* @description <p></p>
9+
*/
10+
public class ApplicationContextTest
11+
{
12+
public static void main(String[] args)
13+
{
14+
AnnotationConfigApplicationContext context =
15+
new AnnotationConfigApplicationContext("com.github.guang19.spring.config");
16+
System.out.println(context.getBean("person"));
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.guang19.spring;
2+
3+
import lombok.*;
4+
5+
/**
6+
* @author yangguang
7+
* @date 2020/3/7
8+
* @description <p>测试bean</p>
9+
*/
10+
@Getter
11+
@Setter
12+
@ToString
13+
@NoArgsConstructor
14+
public class Person
15+
{
16+
private Long personId;
17+
18+
private String personName;
19+
20+
private Integer age;
21+
22+
public Person(Long personId, String personName, Integer age)
23+
{
24+
System.out.println("init bean type of person");
25+
this.personId = personId;
26+
this.personName = personName;
27+
this.age = age;
28+
}
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.guang19.spring.config;
2+
3+
/**
4+
* @author yangguang
5+
* @date 2020/3/7
6+
* @description <p></p>
7+
*/
8+
public class AwareT
9+
{
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.guang19.spring.config;
2+
3+
/**
4+
* @author yangguang
5+
* @date 2020/3/7
6+
* @description <p></p>
7+
*/
8+
public class BeanFactoryPostProcessorT
9+
{
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.guang19.spring.config;
2+
3+
/**
4+
* @author yangguang
5+
* @date 2020/3/7
6+
* @description <p></p>
7+
*/
8+
public class BeanPostProcessorT
9+
{
10+
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.guang19.spring.config;
2+
3+
import com.github.guang19.spring.Person;
4+
import org.springframework.beans.BeansException;
5+
import org.springframework.beans.factory.*;
6+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
7+
import org.springframework.beans.factory.config.BeanPostProcessor;
8+
import org.springframework.context.ApplicationContextAware;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
/**
13+
* @author yangguang
14+
* @date 2020/3/7
15+
* @description <p></p>
16+
*/
17+
@Configuration
18+
public class ConfigurationContext
19+
{
20+
21+
@Bean
22+
public Person person()
23+
{
24+
return new Person(1L,"yxg1",19);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)