Spring 三种注入配置方式

Spring通过DI(依赖注入)实现IOC(控制反转),常用的注入配置有三种:XML配置、注解配置、JAVA配置

XML配置

  • Entity类
public class PersonEntity {

    private String name;
    
    private Integer age;
    
    private WorkEntity workEntity;
}    

public class WorkEntity {

    private String name;
}    
  • XML配置

<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="wordEntity" class="com.moon.entity.WorkEntity">
		<property name="namne" value="work" />
	bean>
	
	<bean id="personEntity" class="com.moon.entity.PersonEntity">
		
		<property name="name" value="zhangsan"/>
		
		<property name="age" value="19" />
		
		<property name="workEntity" ref="wordEntity" />
	bean>
beans>
  • Test测试
public class XmlTest {
    public static void main(String[] args) {
        //获取上下文对象
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //获取bean
        Object entity = context.getBean("personEntity");
        //输出结果
        System.out.println(entity);
        //PersonEntity{name='zhangsan', age=19, workEntity=WorkEntity{namne='work'}}
    }
}

注解配置

  • Entity类
@Component//将对象注入到spring
public class PersonEntity {

    @Value("lisi")//属性注入
    private String name;

    @Value("23")//属性注入
    private Integer age;

    @Autowired//依赖注入
    private WorkEntity workEntity; 
}

@Component//将对象注入到spring
public class WorkEntity {

    @Value("workXXX")//属性注入
    private String name;    
}    
  • Config配置类
@Configuration//申明这是一个配置类
@ComponentScan("com.moon.entity")//指定注解扫描的包
public class AnnotationConfig {
}
  • Test测试
public class AnnotationTest {
   public static void main(String[] args) {
       //获取上下文对象、
       //AnnotationConfig.class 配置类作为参数
       AnnotationConfigApplicationContext context =
               new AnnotationConfigApplicationContext(AnnotationConfig.class);
       //获取bean
       Object entity = context.getBean("personEntity");
       //输出
       System.out.println(entity);
       //PersonEntity{name='lisi', age=23, workEntity=WorkEntity{namne='workXXX'}}
   }
}

JAVA配置

  • Entity类
public class PersonEntity {

    private String name;
    
    private Integer age;
    
    private WorkEntity workEntity;
}    

public class WorkEntity {

    private String name;
}    
  • Config配置类
@Configuration//申明这是一个配置类
public class AnnotationConfig {

    @Bean("workEntity")//将返回值作为bean注入,指定bean名字
    public WorkEntity workEntity(){
        WorkEntity entity = new WorkEntity();
        //属性注入
        entity.setName("workName");
        return entity;
    }

    @Bean//将返回值作为bean注入,不指定bean名字,bean名字默认为方法名
    public PersonEntity personEntity(WorkEntity workEntity){
        PersonEntity personEntity = new PersonEntity();
        //属性注入
        personEntity.setAge(40);
        //属性注入
        personEntity.setName("wangwu");
        //依赖注入
        personEntity.setWorkEntity(workEntity);
        return personEntity;
    }
} 
  • Test测试
public class JavaTest {
   public static void main(String[] args) {
       //获取上下文对象、
       //AnnotationConfig.class 配置类作为参数
       AnnotationConfigApplicationContext context =
               new AnnotationConfigApplicationContext(AnnotationConfig.class);
       //获取bean
       Object entity = context.getBean("personEntity");
       //输出
       System.out.println(entity);
       //PersonEntity{name='wangwu', age=40, workEntity=WorkEntity{namne='workName'}}
   }
}

你可能感兴趣的:(spring)