feat: 添加spring四种自动装配模式

添加spring四种自动装配模式
This commit is contained in:
zc 2022-02-19 21:56:41 +08:00
parent 6f47dc89b0
commit 98192db775
10 changed files with 188 additions and 4 deletions

View File

@ -11,4 +11,12 @@ import org.springframework.stereotype.Component;
@Component
public class Bean {
public String user;
@Override
public String toString() {
return "Bean{" +
"user='" + user + '\'' +
'}';
}
}

View File

@ -0,0 +1,24 @@
package com.youlai.laboratory.spring.DI;
import com.youlai.laboratory.spring.Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 通过构造器装配
*
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2022/2/18 0018 22:30
*/
public class ByConstructorService {
private Bean bean;
public ByConstructorService(Bean bean) {
this.bean = bean;
}
public Bean getBean() {
return bean;
}
}

View File

@ -0,0 +1,24 @@
package com.youlai.laboratory.spring.DI;
import com.youlai.laboratory.spring.Bean;
import org.springframework.stereotype.Component;
/**
*
* 演示默认装配no
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2022/2/19 0019 17:50
*/
public class ByDefaultService {
public Bean bean;
@Override
public String toString() {
return "ByDefaultService{" +
"bean=" + bean +
'}';
}
}

View File

@ -0,0 +1,26 @@
package com.youlai.laboratory.spring.DI;
import com.youlai.laboratory.spring.Bean;
/**
*
*通过属性名称装配需要提供set方法且符合命名规范
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2022/2/19 0019 17:50
*/
public class ByNameService {
public Bean bean;
@Override
public String toString() {
return "ByDefaultService{" +
"bean=" + bean +
'}';
}
//需要提供set方法且命名为setXxx
public void setBean(Bean bean) {
this.bean = bean;
}
}

View File

@ -0,0 +1,22 @@
package com.youlai.laboratory.spring.DI;
import com.youlai.laboratory.spring.Bean;
/**
*
* 通过属性类型装配
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2022/2/19 0019 17:50
*/
public class ByTypeService {
public Bean bean;
@Override
public String toString() {
return "ByDefaultService{" +
"bean=" + bean +
'}';
}
}

View File

@ -26,4 +26,7 @@ public class ConstructorService {
System.out.println(bean);
}
public Bean getBean() {
return bean;
}
}

View File

@ -1,9 +1,8 @@
package com.youlai.laboratory.spring.create;
import com.youlai.laboratory.spring.Test;
/**
* 说明描述
* 使用无参构造器实例化bean
*
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2022/2/18 0018 22:52

View File

@ -0,0 +1,23 @@
<?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">
<bean id="no"
class="com.youlai.laboratory.spring.DI.ByDefaultService"/>
<bean id="bean" name="bean"
class="com.youlai.laboratory.spring.Bean"
/>
<bean id="byName"
class="com.youlai.laboratory.spring.DI.ByNameService" autowire="byName"/>
<bean id="byType"
class="com.youlai.laboratory.spring.DI.ByTypeService" autowire="byType"/>
<bean id="constructor"
class="com.youlai.laboratory.spring.DI.ByConstructorService" autowire="constructor"/>
</beans>

View File

@ -0,0 +1,55 @@
package com.youlai.laboratory.spring;
import com.youlai.laboratory.spring.DI.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Optional;
/**
* 测试4种自动装配模型默认no,(no,byName,byName,constructor)
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2022/2/19 0019 18:00
*/
@Slf4j
@SpringBootTest
public class DIModeTest {
@Test
void noTest(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/DI/DIModel.xml");
applicationContext.refresh();
ByDefaultService no = applicationContext.getBean("no", ByDefaultService.class);
log.info("装配成功?{}", no.bean==null);
}
@Test
void byName(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/DI/DIModel.xml");
applicationContext.refresh();
ByNameService byName = applicationContext.getBean("byName", ByNameService.class);
log.info("装配成功?{}",byName.bean != null);
}
@Test
void byType(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/DI/DIModel.xml");
applicationContext.refresh();
ByTypeService byType = applicationContext.getBean("byType", ByTypeService.class);
log.info("装配成功?{}",byType.bean != null);
}
@Test
void constructor(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/DI/DIModel.xml");
applicationContext.refresh();
ByConstructorService constructor = applicationContext.getBean("constructor", ByConstructorService.class);
log.info("装配成功?{}",constructor.getBean() != null);
}
}

View File

@ -8,13 +8,13 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* 说明描述
* 测试两种注入方式(构造器和setter)
*
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2022/2/18 0018 21:04
*/
@SpringBootTest
public class DITests {
public class DITypeTests {
@Test