-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
50 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,12 @@ | ||
package springframework; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* @Author:吉口吉 | ||
* @Date:Created in 14:17 2021/12/5 | ||
* @Description: 容器,用HashMap托管bean | ||
* @Description: | ||
*/ | ||
|
||
public class BeanFactory { | ||
|
||
private Map<String, Object> beanMap = new ConcurrentHashMap<>(); | ||
|
||
public Object getBean(String name) {//bean的获取 | ||
return beanMap.get(name); | ||
} | ||
|
||
public void registerBean(String name, Object bean) {//托管bean | ||
beanMap.put(name, bean); | ||
} | ||
|
||
public interface BeanFactory {//容器 | ||
public Object getBean(String name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
import pojo.User; | ||
import springframework.BeanFactory; | ||
import springframework.config.BeanDefinition; | ||
import springframework.support.DefaultListableBeanFactory; | ||
|
||
public class Test { | ||
public static void main(String[] args){ | ||
//1.初始化容器BeanFactory | ||
BeanFactory beanFactory=new BeanFactory(); | ||
//2.托管bean | ||
beanFactory.registerBean("user",new User()); | ||
//1.初始化容器 | ||
DefaultListableBeanFactory factory=new DefaultListableBeanFactory(); | ||
//2.注册bean的定义信息(class对象) | ||
BeanDefinition beanDefinition=new BeanDefinition(User.class); | ||
factory.registerBeanDefinition("user",beanDefinition); | ||
//3.获取bean | ||
User u=(User)beanFactory.getBean("user"); | ||
u.UserSpeak(); | ||
User u1=(User)factory.getBean("user"); | ||
u1.userSpeak(); | ||
//4.再次获取bean | ||
User u2=(User)factory.getBean("user"); | ||
u2.userSpeak(); | ||
|
||
System.out.println(u1); | ||
System.out.println(u2); | ||
if (u1==u2) System.out.println("两者内存地址相同"); | ||
else System.out.println("两者内存地址不同"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package pojo; | ||
|
||
public class User { | ||
public void UserSpeak(){System.out.println("你好");} | ||
public void userSpeak(){System.out.println("你好");} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.