bean命名空间和自动装配

1.命名空间

  命名空间分为以下几种。初始阶段学习singleton(默认)和prototype

  

 

   singleton(默认)和prototype的区别:

    使用在定义bena的时候标签里面加上scope,在加上以上6种属性即可:<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

Student student = (Student) context.getBean("student");
Student student1 = (Student) context.getBean("student");

    singleton:通过这个bean建的对象,他们的hashcode是一样的。每次从bean中get的时候都是同一个对象。即student==student1

    prototype:通过这个bean建的对象,他们的hashcode是不一样一样的。每次从bean中get的时候都是新new一个对象。即student不等于student1

      

 

  其余的request,session application websocket在web开发中学习

2.bean的自动装配

  bean有三种装配方式

  1.在xml中显示配置-----前面一直在使用的

  2.在java代码中显示配置

  3.自动装配bean【Autowired】

    配置环境:一个人有两个宠物   people类 dog类 cat类

    

    byName自动装配:会在容器的上下文去查找和自己set方法后面值相同的beanid。即setDog(Dog dog)  bean id=dog

   byTypea自动装配:会在容器的上下文去查找和自己属性相同的beanid。类型必须全局唯一才能装配。多个相同类型装配失败

    

 

 

 

  

 

posted @ 2021-11-20 00:09  qwedfrgh  阅读(102)  评论(0)    收藏  举报