略
@Component
public class TestInjection {
private String hello = "hello";
public String getHello() {
return hello;
}
}
test
@SpringBootTest
class WebApplicationTests {
@Resource
private TestInjection testInjection;
@Test
void contextLoads() {
System.out.println(testInjection.getHello());
}
}
这种方法多用于业务注入
这种方法多用于初始化配置类Bean,例如kafka客户端、redis客户端等
@Configuration
public class TestInjection {
private String hello = "hello";
public String getHello() {
return hello;
}
@Bean
public TestInjection getTestInjection(){
return new TestInjection();
}
}
测试同上
这种方法多用于框架整合,如mybatis的sqlsessionfactory
public class TestInjection2 implements FactoryBean<TestInjection2> {
private String hello = "hello";
public void setHello(String hello) {
this.hello = hello;
}
public String getHello() {
return hello;
}
@Override
public TestInjection2 getObject() throws Exception {
TestInjection2 testInjection2 = new TestInjection2();
testInjection2.setHello("你好");
return testInjection2;
}
@Override
public Class<?> getObjectType() {
return TestInjection2.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
手动注入并测试
@Test
void test2(){
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
annotationConfigApplicationContext.register(TestInjection2.class);
annotationConfigApplicationContext.refresh();;
System.out.println(annotationConfigApplicationContext.getBean(TestInjection2.class).getHello());
}
@EnableAsync等开关配置实现的主要方案
该注解有三种注入方式
构建对象
package com.ncse.web.controller;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 14:34:00
*/
public class TestInjection3 {
private String hello = "hello";
public String getHello() {
return hello;
}
}
放入配置类
package com.ncse.web.config;
import com.ncse.web.controller.TestInjection3;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 15:25:00
*/
@Component
@Import({TestInjection3.class})
public class InjectConfig {
}
测试
package com.ncse.web;
import com.ncse.web.controller.TestInjection3;
import com.ncse.web.controller.TestInjectionSelector;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 15:10:00
*/
@SpringBootTest
public class MainTest {
@Resource
private TestInjection3 testInjection3;
@Test
void test(){
System.out.println(testInjection3.getHello());
}
}
构建配置对象
package com.ncse.web.controller;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 14:34:00
*/
public class TestInjection4 {
private String hello = "hello";
public String getHello() {
return hello;
}
}
构建配置选择器
package com.ncse.web.controller;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 14:34:00
*/
public class TestInjectionSelector implements ImportSelector {
/**
* 会将该方法返回的全类限定名下的类都注入容器
* @param importingClassMetadata
* @return
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.ncse.web.controller.TestInjection4"};
}
}
将配置选择器注入到配置类中
package com.ncse.web.config;
import com.ncse.web.controller.TestInjection3;
import com.ncse.web.controller.TestInjectionSelector;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 15:25:00
*/
@Component
@Import({TestInjection3.class, TestInjectionSelector.class})
public class InjectConfig {
}
测试
package com.ncse.web;
import com.ncse.web.controller.TestInjection4;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 15:10:00
*/
@SpringBootTest
public class MainTest {
@Resource
private TestInjection4 testInjection4;
@Test
void test4(){
System.out.println(testInjection4.getHello());
}
}
实现配置时动态配置对象信息
创建对象
package com.ncse.web.controller;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 14:34:00
*/
public class TestInjection5 {
private String hello = "hello";
public void setHello(String hello) {
this.hello = hello;
}
public String getHello() {
return hello;
}
}
ImportBeanDefinitionRegistrar实现
package com.ncse.web.controller;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 15:42:00
*/
public class TestInjectionImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(TestInjection5.class)
.addPropertyValue("hello", "hhh")
.getBeanDefinition();
registry.registerBeanDefinition("testInjection5",beanDefinition);
}
}
交配置类处理
package com.ncse.web.config;
import com.ncse.web.controller.TestInjection3;
import com.ncse.web.controller.TestInjectionImportBeanDefinitionRegistrar;
import com.ncse.web.controller.TestInjectionSelector;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 15:25:00
*/
@Component
@Import({TestInjection3.class, TestInjectionSelector.class, TestInjectionImportBeanDefinitionRegistrar.class})
public class InjectConfig {
}
测试
package com.ncse.web;
import com.ncse.web.controller.TestInjection5;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
/**
* @author Xi Peng
* @Description
* @createTime 2022年11月17日 15:10:00
*/
@SpringBootTest
public class MainTest {
@Resource
private TestInjection5 testInjection5;
@Test
void test5(){
System.out.println(testInjection5.getHello());
}
}