spring注入你真搞懂了吗?不会一直都是这样错误理解吧?( 二 )

  1. 创建一个spring项目
  2. 创建Appconfig类以及test类
  3. 创建几个service类
Appconfig.java
package services;import org.springframework.beans.factory.annotation.Configurable;import org.springframework.context.annotation.ComponentScan;@Configurable@ComponentScan("com.shadow")public class Appconfig {Test.java
package 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.java
package 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
spring注入你真搞懂了吗?不会一直都是这样错误理解吧?

文章插图
 
想要在GoudanService中配置GouService,我们采用xml配置方式的no注入模型 。虽然我们没有配置注入模型参数但是xml默认是no 。故所以我们运行Test.java得出结果是配置成功的 。
spring注入你真搞懂了吗?不会一直都是这样错误理解吧?

文章插图
 
我把xml这一段注释掉呢?
根据no注入模型的官网解释,大家应该知道这是无法自动配置的,也就是null 。
spring注入你真搞懂了吗?不会一直都是这样错误理解吧?

文章插图
 
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
spring注入你真搞懂了吗?不会一直都是这样错误理解吧?

文章插图
 
<bean id="goudanBean" class="services.GoudanService"></bean><bean id="gouBean" class="services.GouService"></bean>Test.java
package 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
spring注入你真搞懂了吗?不会一直都是这样错误理解吧?

文章插图
 
我们发现bytype模型注入,与no方式注入想比不需要了bean里配置property的ref,spring容器会自动根据GouService的类型去找bean,结果找到了类型为:services.GouService的bean,所以也是可以配置成功的 。
重点:bytype就是如果属性类型与bean的class类型相同那么可以自动配置,否则配置错误 。看下面图

spring注入你真搞懂了吗?不会一直都是这样错误理解吧?

文章插图
 

spring注入你真搞懂了吗?不会一直都是这样错误理解吧?

文章插图
 
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;}}


推荐阅读