add factory bean sample in spring boot source code analysis · laifulin/JavaSpringBootSamples@f3858d3 · GitHub
Skip to content

Commit f3858d3

Browse files
committed
add factory bean sample in spring boot source code analysis
1 parent 294b7f5 commit f3858d3

16 files changed

Lines changed: 162 additions & 223 deletions

File tree

‎springboot-source-code-analysis/(10)XML方式配置Bean实战.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.springboot.source.code.analysis.ioc.ann;
2+
3+
import com.example.springboot.source.code.analysis.ioc.pojo.Animal;
4+
import com.example.springboot.source.code.analysis.ioc.pojo.Dog;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
/**
9+
* Created by ipipman on 2021/10/16.
10+
*
11+
* @version V1.0
12+
* @Package com.example.springboot.source.code.analysis.ioc.ann
13+
* @Description: (通过 @Configuration注解 注入一个Bean)
14+
* @date 2021/10/16 12:04 下午
15+
*/
16+
@Configuration
17+
public class MyBeanConfiguration {
18+
19+
@Bean("dog")
20+
public Animal getDog() {
21+
return new Dog();
22+
}
23+
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example.springboot.source.code.analysis.ioc.ann;
2+
3+
import com.example.springboot.source.code.analysis.ioc.pojo.Animal;
4+
import com.example.springboot.source.code.analysis.ioc.pojo.Cat;
5+
import org.springframework.beans.factory.BeanFactory;
6+
import org.springframework.beans.factory.FactoryBean;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* Created by ipipman on 2021/10/16.
11+
*
12+
* @version V1.0
13+
* @Package com.example.springboot.source.code.analysis.ioc.ann
14+
* @Description: (通过实现 FactoryBean < ? > 接口实现Bean的注入)
15+
* @date 2021/10/16 12:15 下午
16+
*/
17+
@Component
18+
public class MyCatFactoryBean implements FactoryBean<Animal> {
19+
20+
@Override
21+
public Animal getObject() throws Exception {
22+
return new Cat();
23+
}
24+
25+
@Override
26+
public Class<?> getObjectType() {
27+
return Animal.class;
28+
}
29+
30+
@Override
31+
public boolean isSingleton() {
32+
return FactoryBean.super.isSingleton();
33+
}
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.springboot.source.code.analysis.ioc.pojo;
2+
3+
/**
4+
* Created by ipipman on 2021/10/16.
5+
*
6+
* @version V1.0
7+
* @Package com.example.springboot.source.code.analysis.ioc.pojo
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/10/16 12:06 下午
10+
*/
11+
public abstract class Animal {
12+
13+
public abstract String getName();
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.springboot.source.code.analysis.ioc.pojo;
2+
3+
/**
4+
* Created by ipipman on 2021/10/16.
5+
*
6+
* @version V1.0
7+
* @Package com.example.springboot.source.code.analysis.ioc.pojo
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/10/16 12:09 下午
10+
*/
11+
public class Cat extends Animal{
12+
@Override
13+
public String getName() {
14+
return "Cat";
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.springboot.source.code.analysis.ioc.pojo;
2+
3+
/**
4+
* Created by ipipman on 2021/10/16.
5+
*
6+
* @version V1.0
7+
* @Package com.example.springboot.source.code.analysis.ioc.pojo
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/10/16 12:08 下午
10+
*/
11+
public class Dog extends Animal{
12+
@Override
13+
public String getName() {
14+
return "Dog";
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.springboot.source.code.analysis.ioc.pojo;
2+
3+
/**
4+
* Created by ipipman on 2021/10/16.
5+
*
6+
* @version V1.0
7+
* @Package com.example.springboot.source.code.analysis.ioc.pojo
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/10/16 12:25 下午
10+
*/
11+
public class Monkey extends Animal {
12+
@Override
13+
public String getName() {
14+
return "Monkey";
15+
}
16+
}
Lines changed: 33 additions & 0 deletions

‎springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/xml/Animal.java‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/xml/AnimalFactory.java‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)