2026/5/21 19:28:27
网站建设
项目流程
网站关键技术,机械加工网格刀厂家,如何快速做一个网站,dede如何设置网站端口bean的作用域
翻译版英文版重点掌握单例和原型就可以了
单例模式(Spring 默认机制)
所有bean 共享一个实例化对象bean iduser2 classcom.cike4.pojo.User c:nameuser2 c:age20 scopesingleton/测试方法Te…bean的作用域翻译版英文版重点掌握单例和原型就可以了单例模式(Spring 默认机制)所有bean 共享一个实例化对象beaniduser2classcom.cike4.pojo.Userc:nameuser2c:age20scopesingleton/测试方法Testpublicvoidtest2(){ApplicationContextcontextnewClassPathXmlApplicationContext(userbeans.xml);// 这里申明了类型就不需要强壮类型 User 类了Userusercontext.getBean(user2,User.class);Useruser2context.getBean(user2,User.class);System.out.println(useruser2);}bean共享同一个实例化对象所以返回true原型模式每次从容器中get的时候都会产生一个新的对象beaniduser2classcom.cike4.pojo.Userc:nameuser2c:age20scopeprototype/测试方法Testpublicvoidtest2(){ApplicationContextcontextnewClassPathXmlApplicationContext(userbeans.xml);// 这里申明了类型就不需要强壮类型 User 类了Userusercontext.getBean(user2,User.class);Useruser2context.getBean(user2,User.class);System.out.println(user.hashCode());System.out.println(user2.hashCode());System.out.println(useruser2);}两个获取的对象不相等hash也不一样因为实例化不一样其余的request、session、application、这些只能在web开发中使用官方解释https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-factory-scopes https://docs.spring.io/spring-framework/reference/core/beans/factory-scopes.htmlBean的自动装配自动装配是 Spring满足bean以来的一种方式Spring会在上下文中国自动寻找并自动给bean装配属性在Spring中有三种装配的方式在XML中显示的配置在Java中显示配置隐式的自动装配bean 【重要的】测试环境搭建一个人有两个宠物byName自动装配byName会自动在容器上下文中查找和自己对象set方法后面的值对应beanidbeanidcatclasscom.cike5.pojo.Cat/beaniddogclasscom.cike5.pojo.Dog/!-- byName会自动在容器上下文中查找和自己对象set方法后面的值对应beanid --beanclasscom.cike5.pojo.PeopleidpeopleautowirebyNamepropertynamenamevaluecike_y//beanbyType自动装配byType会自动在容器上下文中查找和自己对象属性类型相同的bean弊端类型全局为一才会自动装配beanclasscom.cike5.pojo.Cat/beanclasscom.cike5.pojo.Dog/!-- byType会自动在容器上下文中查找和自己对象属性类型相同的bean 弊端类型全局为一才会自动装配 --beanclasscom.cike5.pojo.PeopleidpeopleautowirebyTypepropertynamenamevaluecike_y//bean小结byName的时候需要保证所有bean的id唯一并且bean需要和自动注入的属性set方法后面的字母一致byTtype的时候需要保证所有的bean的calss唯一并且这个bean需要和自动注入的属性的类型一致官方解释https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-autowired-annotation https://docs.spring.io/spring-framework/reference/core/beans/annotation-config/autowired.html使用注解实现自动装配JDK1.5支持的注解Spring2.5就支持注解了The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.要使用注解须知导入约束。context约束配置注解的支持context:annotation-config/干净并且完整的xml配置?xml version1.0 encodingUTF-8?beansxmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdcontext:annotation-config//beans官方文档https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-annotation-config https://docs.spring.io/spring-framework/reference/core/beans/annotation-config.html https://docs.spring.io/spring-framework/reference/core/beans/annotation-config/autowired.htmlAutowired在类的属性上面进行注释publicclassPeople{AutowiredprivateCatcat;AutowiredprivateDogdog;privateStringname;publicCatgetCat(){returncat;}publicDoggetDog(){returndog;}}不需要set方法了因为是通过反射的原理实现的使用Autowired我们可以不用编写Set方法了前提是你这个自动装配的属性在IoCSpring容器中存在且符合属性类型测试方法Testpublicvoidtest(){// 创建Spring容器对象ApplicationContextcontextnewClassPathXmlApplicationContext(beans.xml);// 获取bean对象Peoplepeoplecontext.getBean(people,People.class);people.getCat().shout();people.getDog().shout();}科普Nullable字段标记了这个注解说明这个字段可以为null可以这样子写让name值为空privateNullableStringname;查看Autowired的注解接口可以看见默认值是truepublicinterfaceAutowired{/** * Declares whether the annotated dependency is required. * pDefaults to {code true}. */booleanrequired()defaulttrue;}也可以这样子写publicclassPeople{// 如果显示定义了Autowired 的 required属性为false说明这个对象可以为null否则不允许为空Autowired(requiredfalse)privateCatcat;Autowired(requiredfalse)privateDogdog;Qualifier可以进行显示定义进行自动装配Qualifier(dog11)privateDogdog;beans.xml 中的容器id为dog11可以进行对应?xml version1.0encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd!--开启注解的支持--context:annotation-config/bean iddog11classcom.cike5.pojo.Dog/bean idcatclasscom.cike5.pojo.Cat/bean idpeopleclasscom.cike5.pojo.People//beansAutowired和Qualifier的区别Autowired是通过byType的方式实现的而且这个对象必须存在Qualifier则是通过byName的方式实现的显式定义这个容器idResource支持隐式自动装配、也支持显示定义相当于Autowored和Quailifier的结合体publicclassPeople{// 隐式定义、自动装配ResourceprivateCatcat;publicclassPeople{// 显示定义指定容器id、自动装配Resource(namecat)privateCatcat;小结Resource和 Autowired的区别都是自动装配的都可以放在属性字段上Autowired通过byType的方式实现而且必须要求这个对象必须存在【常用】Resource 默认通过byName的方式实现如果找不到则会通过byType实现如果两个都找不到的情况下就会报错最后也可以显示定义进行装配【常用】执行的顺序不同Autowired通过byType的方式实现Resource 默认通过byName的方式实现