本文只关注属性注入与构造函数注入,其他注入方式不涉及。
相关类代码:
//测试Bean对象
package com.example.demo;
public class BeanVO {
private Integer beanField;
public Integer getBeanField() {
return beanField;
}
public void setBeanField(Integer beanField) {
this.beanField = beanField;
}
}
//在系统中配置bean
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
@Bean
public BeanVO testBean() {
BeanVO testBean = new BeanVO();
testBean.setBeanField(5);
return testBean;
}
}
//构造函数注入
package com.example.demo.injection;
import com.example.demo.BeanVO;
import org.springframework.stereotype.Component;
@Component
public class ConstructInjectBean {
//字段注入已不推荐
private BeanVO beanVO;
//只有一个参数时构造函数中可以加autowire注解,不加也可以
public ConstructInjectBean(BeanVO beanVO) {
this.beanVO = beanVO;
}
public BeanVO getBeanVO() {
return beanVO;
}
}
//属性注入
package com.example.demo.injection;
import com.example.demo.BeanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SetInjectBean {
private BeanVO beanVO;
//与方法名无关,这里特意未用setBeanVO方法名
@Autowired
public void set111BeanVO(BeanVO beanVO) {
this.beanVO = beanVO;
}
public BeanVO getBeanVO() {
return beanVO;
}
}
测试代码如下:
//测试两种注入方式
package com.example.demo;
import com.example.demo.injection.ConstructInjectBean;
import com.example.demo.injection.SetInjectBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
//test类中可以使用字段注入或属性注入,但不支持构造函数注入(测试类不允许重载构造函数)
@Autowired
private ConstructInjectBean constructInjectBean;
@Autowired
private SetInjectBean setInjectBean;
@Test
public void testConstructInject() {
assertEquals((long) constructInjectBean.getBeanVO().getBeanField(), 5);
}
@Test
public void testPropertyInject() {
assertEquals((long) setInjectBean.getBeanVO().getBeanField(), 5);
}
}
测试结果:
已上两种注入方式均已生效且结果正确。
已上两种方式均正确的前提是ConstructInjectBean类与SetInjectBean类均被@Component注解修饰,如果这两个类本身不是bean的话,bean注入无法实现。
针对上面这段话,看以下例子:
//去掉component注解修饰的构造函数注入示例
package com.example.demo.inherit;
import com.example.demo.BeanVO;
public class ParentConstructInjectBean {
private BeanVO beanVO;
public ParentConstructInjectBean(BeanVO beanVO) {
this.beanVO = beanVO;
}
public BeanVO getBeanVO() {
return beanVO;
}
}
//一个正常的bean继承上面的非bean类
package com.example.demo.inherit;
import com.example.demo.BeanVO;
import org.springframework.stereotype.Component;
@Component
public class NormalBean extends ParentConstructInjectBean {
private BeanVO beanVO;
public NormalBean(BeanVO beanVO) {
super(beanVO);
this.beanVO = beanVO;
}
//重写get方法
public BeanVO getBeanVO() {
beanVO = new BeanVO();
beanVO.setBeanField(10);
return beanVO;
}
}
测试代码:
package com.example.demo;
import com.example.demo.inherit.ParentConstructInjectBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private ParentConstructInjectBean parentConstructInjectBean;
@Test
public void testInheritInject() {
assertEquals((long) parentConstructInjectBean.getBeanVO().getBeanField(), 10);
}
}
测试通过,但此时 ParentConstructInjectBean类依然不是一个bean,测试类中注入的其实是ParentConstructInjectBean类的子类NormalBean,这里可以参考接口注入时其实注入的是接口实现类原理,测试方法testInheritInject中值等于10证明了一点。
以上论述说明非bean类无法通过继承而成为bean类,也无法为非bean类注入。
上段结论对于属性注入同样成立(测试代码及结果不变),其他相关代码如下:
//不带comonent注解的属性注入示例
package com.example.demo.inherit;
import com.example.demo.BeanVO;
import org.springframework.beans.factory.annotation.Autowired;
public class ParentSetInjectBean {
private BeanVO beanVO;
//这里的autowire能成功不是因为这个类是bean,而是这个方法会被子类使用,而子类是bean
//父类接口方法的注解会被继承类继承
@Autowired
public void set111BeanVO(BeanVO beanVO) {
this.beanVO = beanVO;
}
public BeanVO getBeanVO() {
return beanVO;
}
}
package com.example.demo.inherit;
import com.example.demo.BeanVO;
import org.springframework.stereotype.Component;
@Component
public class NormalBean extends ParentSetInjectBean {
//重写get方法
public BeanVO getBeanVO() {
BeanVO beanVO = super.getBeanVO(); //这里bean的值是5,注入成功
beanVO.setBeanField(10);
return beanVO;
}
}