- 创建一个spring项目
- 创建Appconfig类以及test类
- 创建几个service类
package services;import org.springframework.beans.factory.annotation.Configurable;import org.springframework.context.annotation.ComponentScan;@Configurable@ComponentScan("com.shadow")public class Appconfig {
Test.javapackage test;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {//AnnotationConfigApplicationContext ac =//new AnnotationConfigApplicationContext(Appconfig.class);ClassPathXmlApplicationContext cc = new ClassPathXmlApplicationContext("web.xml");}}
其余都为空基于 XML 的 bean 注入方式no方式配置GoudanService.java
package services;public class GoudanService {GouService gouService;public void setGouService(GouService gouService) {this.gouService = gouService;}public GouService getGouService() {return gouService;}}
web.xml<bean id="goudanBean" class="services.GoudanService"><property name="gouService"><ref bean="gouBean"></ref></property></bean><bean id="gouBean" class="services.GouService"></bean>
Test.javapackage test;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;import org.springframework.context.support.ClassPathXmlApplicationContext;import services.GoudanService;public class Test {public static void main(String[] args) {//AnnotationConfigApplicationContext ac =//new AnnotationConfigApplicationContext(Appconfig.class);ClassPathXmlApplicationContext cc = new ClassPathXmlApplicationContext("web.xml");System.out.println(cc.getBean(GoudanService.class).getGouService());}}
运行Test.java的result文章插图
想要在GoudanService中配置GouService,我们采用xml配置方式的no注入模型 。虽然我们没有配置注入模型参数但是xml默认是no 。故所以我们运行Test.java得出结果是配置成功的 。
文章插图
我把xml这一段注释掉呢?
根据no注入模型的官网解释,大家应该知道这是无法自动配置的,也就是null 。
文章插图
bytype方式配置GoudanService.java
package services;public class GoudanService {GouService gouService;public void setGouService(GouService gouService) {this.gouService = gouService;}public GouService getGouService() {return gouService;}}
web.xml文章插图
<bean id="goudanBean" class="services.GoudanService"></bean><bean id="gouBean" class="services.GouService"></bean>
Test.javapackage test;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;import org.springframework.context.support.ClassPathXmlApplicationContext;import services.GoudanService;public class Test {public static void main(String[] args) {//AnnotationConfigApplicationContext ac =//new AnnotationConfigApplicationContext(Appconfig.class);ClassPathXmlApplicationContext cc = new ClassPathXmlApplicationContext("web.xml");System.out.println(cc.getBean(GoudanService.class).getGouService());}}
运行Test.java的result文章插图
我们发现bytype模型注入,与no方式注入想比不需要了bean里配置property的ref,spring容器会自动根据GouService的类型去找bean,结果找到了类型为:services.GouService的bean,所以也是可以配置成功的 。
重点:bytype就是如果属性类型与bean的class类型相同那么可以自动配置,否则配置错误 。看下面图
文章插图
文章插图
byname方式配置GoudanService.java
package services;public class GoudanService {GouService gouService;GouService gouBean;public GouService getGouBean() {return gouBean;}public void setGouBean(GouService gouBean) {this.gouBean = gouBean;}public void setGouService(GouService gouService) {this.gouService = gouService;}public GouService getGouService() {return gouService;}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 抖音小店无货源店群大揭秘,这些你真的清楚吗?
- 如何给WINDOWS 7注入USB和NVME驱动?
- 玩转SpringBoot之整合 shiro 权限框架
- Spring Boot 12 国际化
- 什么是计时攻击?Spring Boot 中该如何防御?
- SpringBoot-登录验证码实现
- 跟常用框架SpringSecurity比 微服务到底胜在哪?
- 大牛教大家如何用SpringBoot技术快速实现天气预报系统
- 欧拉|比男人更懂女人 欧拉注入“她力量”:最爱女性的汽车倾力打造“她生态”
- 如何按照条件向Spring容器中注册bean?这次我懂了