先来一个spring.xml的基本应用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>
<!-- default-autowire="byType" -->
<bean id = "javaService" class = "com.springcource01.service.JAVAService">
<!--
我们在一开始学习spring的时候,那个时候肯定用的都是xml进行配置,
比如说,我要在JAVAService中应用WDTService,怎么办呢?
但是,这样肯定是有弊端的,比如说,我们要在JAVAService 注入100个
那,我要写100个吗?这也太烦了吧
-->
<!--name 就是我们在JAVAService中写的setWdtService 这个setName 这个Name-->
<property name="wdtService">
<!-- 这个bean就是下面的 id -->
<ref bean = "wdtService"/>
</property>
</bean>
<bean id = "wdtService" class = "com.springcource01.service.WDTService">
</bean>
</beans>
public class JAVAService {
private WDTService wdtService;
public WDTService getWdtService() {
return wdtService;
}
public void setWdtService(WDTService wdtService) {
this.wdtService = wdtService;
}
}
public class WDTService {
}
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext cc =
new ClassPathXmlApplicationContext("classpath:spring.xml");
//这个就是我们在JAVAService.class中拿到 WdtService
System.out.println(cc.getBean(JAVAService.class).getWdtService());
}
}
Spring自动注入模型
自动注入模型,这个仅仅是针对于采用xml这样方式的说法。
自动注入模型4中方式:
1、no
2、byName 这个根据名字找到bean 然后把这个bean通过set方法注入到对象上去的
3、byType 这个是通过类型去找。 setXXX XXX可以随便写
4、constructor 推断构造方法
byType、byName案例
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byType">
<bean id = "javaService" class = "com.springcource01.service.JAVAService">
</bean>
<bean id = "wdtService" class = "com.springcource01.service.WDTService">
</bean>
</beans>
public class JAVAService {
private WDTService wdtService;
public WDTService getWdtService() {
return wdtService;
}
//用的是byType setWdtService 改成 setXXX
//也是可以成功的把WDTService注入到JAVAService中的
//因为,我采用的是byType,不会受名字的影响的
//如果,把配置文件中的 default-autowire="byType" 改成 byName
//setXXX 这样就注入不进去了,因为byName是通过name找bean的
public void setWdtService(WDTService wdtService) {
this.wdtService = wdtService;
}
}
public class WDTService {
}
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext cc =
new ClassPathXmlApplicationContext("classpath:spring.xml");
System.out.println(cc.getBean(JAVAService.class).getWdtService());
}
}
通过注解进行bean的注入
@Configurable
@ComponentScan("com.springcource01")
public class AppConfig {
}
@Component
public class JAVAService {
@Autowired
private WDTService wdtService;
public void wdt() {
System.out.println(wdtService);
}
}
@Component
public class WDTService {
}
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac =
new AnnotationConfigApplicationContext(AppConfig.class);
ac.getBean(JAVAService.class).wdt();
}
}
总结
如果有面试官,问,spring的注入方式之类的问题的话,你要这么回答:
spring有两种注入方式:
1、通过set方法
2、通过构造方法
spring有4种注入模型:(仅仅针对于xml的这种方式)
1、no
2、byName
3、byType
4、constructor
我们经常用的注解 @Autowired 这个注解,这个是先通过 type,再通过name 这个是spring jar包提供的
这个type和name和上面的注入模型 没有一毛钱的关系,千万不要把概念给整混乱了。
spring源码来验证@Autowired和byType、byName无关
@Autowired: Field field = (Field) this.member 可以理解为 通过反射拿到对象的。
//byType byName
if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) {
MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
// Add property values based on autowire by name if applicable.
if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME) {
autowireByName(beanName, mbd, bw, newPvs);
}
// Add property values based on autowire by type if applicable.
if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) {
autowireByType(beanName, mbd, bw, newPvs);
}
pvs = newPvs;
}
@Autowired @resource
这两个注解的区别:
Autowired 这个是先通过 type,再通过name 这个是spring jar包提供的
resource 这个是先通过name,再通过type的 这个是JDK jar包提供的