基于XML管理bean

一,例子 

①创建 Maven Module
②引入依赖
<dependencies>
        <!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>
        <!-- junit测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
③创建类 HelloWorld
package com.atguigu.spring.pojo;

public class HelloWorld {
    public void sayHello(){
        System.out.println("hello spring!");
    }
}
④创建 Spring 的配置文件

⑤在Spring的配置文件中配置bean

​
<!-- 
配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理 
通过bean标签配置IOC容器所管理的bean 
属性: 
id:设置bean的唯一标识 
class:设置bean所对应类型的全类名 
--> 
<bean id="helloworld" class="com.atguigu.spring.bean.HelloWorld"></bean> 
​

 配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理 
通过bean标签配置IOC容器所管理的bean 
属性: 
id:设置bean的唯一标识 
class:设置bean所对应类型的全类名 

⑥创建测试类测试
package com.atguigu.spring.test;

import com.atguigu.spring.pojo.HelloWorld;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldTest {
    @Test
    public  void  test(){
        //获取IOC容器
        ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取ioc容器中的bean
        HelloWorld helloworld = (HelloWorld) ioc.getBean("helloworld");
        helloworld.sayHello();

    }
}

二,获取bean

①方式一:根据 id 获取
由于 id 属性指定了 bean 的唯一标识,所以根据 bean 标签的 id 属性可以精确获取到一个组件对象。
上个实验中我们使用的就是这种方式。
②方式二:根据类型获取
@Test 
public void testHelloWorld(){ 
ApplicationContext ac = new 
ClassPathXmlApplicationContext("applicationContext.xml"); 
HelloWorld bean = ac.getBean(HelloWorld.class); 
bean.sayHello(); 
} 

③方式三:根据 id 和类型
@Test 
public void testHelloWorld(){ 
ApplicationContext ac = new 
ClassPathXmlApplicationContext("applicationContext.xml"); 
HelloWorld bean = ac.getBean("helloworld", HelloWorld.class); 
bean.sayHello(); 
} 
④注意
当根据类型获取 bean 时,要求 IOC 容器中指定类型的 bean 有且只能有一个
IOC 容器中一共配置了两个:
<bean id="helloworldOne" class="com.atguigu.spring.bean.HelloWorld"></bean>
<bean id="helloworldTwo" class="com.atguigu.spring.bean.HelloWorld"></bean>
根据类型获取时会抛出异常:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean
of type 'com.atguigu.spring.bean.HelloWorld' available: expected single matching bean but
found 2: helloworldOne,helloworldTwo

三,依赖注入之setter注入

①创建学生类 Student
public class Student { 
private Integer id; 
private String name; 
private Integer age; 
private String sex; 
public Student() { 
} 
public Integer getId() { 
return id; 
} 
public void setId(Integer id) { 
this.id = id; 
} 
public String getName() { 
return name; 
} 
public void setName(String name) { 
this.name = name; 
} 
public Integer getAge() { 
return age; 
} 
public void setAge(Integer age) { 
this.age = age; 
} 
public String getSex() { 
return sex; 
} 
public void setSex(String sex) { 
this.sex = sex; 
} 
@Override 
public String toString() { 
return "Student{" + 
"id=" + id + 
", name='" + name + '\'' + 
", age=" + age + 
", sex='" + sex + '\'' + 
'}'; 
} 
}
②配置 bean 时为属性赋值
<bean id="studentOne" class="com.atguigu.spring.bean.Student"> 
<!-- property标签:通过组件类的setXxx()方法给组件对象设置属性 --> 
<!-- name属性:指定属性名(这个属性名是getXxx()、setXxx()方法定义的,和成员变量无关) 
--> 
<!-- value属性:指定属性值 --> 
<property name="id" value="1001"></property> 
<property name="name" value="张三"></property> 
<property name="age" value="23"></property> 
<property name="sex" value="男"></property> 
</bean>

③测试

@Test 
public void testDIBySet(){ 
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-di.xml"); 
Student studentOne = ac.getBean("studentOne", Student.class); 
System.out.println(studentOne); 
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架是一种轻量级的Java开发框架,主要使用依赖注入和面向切面编程等技术来完成应用程序的开发和管理。在Spring框架中,注入Bean是非常重要的一个概念。在XML配置文件中,可以使用以下几种方式来注入Bean: 1. Constructor注入 使用Constructor注入是指在Bean实例化时,通过构造函数(Constructor)注入需要的依赖。在配置文件中使用 <constructor-arg> 标签来配置参数,例如: <bean id="foo" class="com.example.Foo"> <constructor-arg index="0" value="fooValue"/> <constructor-arg index="1" value="barValue"/> </bean> 2. Setter注入 使用Setter注入是指在Bean实例化后,通过Setter方法注入需要的依赖。在配置文件中使用 <property> 标签来配置参数,例如: <bean id="foo" class="com.example.Foo"> <property name="foo" value="fooValue" /> <property name="bar" value="barValue" /> </bean> 3. 静态工厂方法注入 使用静态工厂方法注入是指通过静态工厂方法(static factory method)来实例化Bean,并注入需要的依赖。在配置文件中使用 <bean> 标签的 factory-method 属性来指定工厂方法名称,例如: <bean id="foo" class="com.example.Foo" factory-method="createFoo"> <constructor-arg index="0" value="fooValue"/> <constructor-arg index="1" value="barValue"/> </bean> 4. 实例化工厂方法注入 使用实例化工厂方法注入是指在Bean实例化前,通过实例化工厂方法(factory method)来实例化Bean,并注入需要的依赖。在配置文件中使用 <bean> 标签的 factory-bean 和 factory-method 属性来指定工厂Bean和工厂方法名称,例如: <bean id="fooFactory" class="com.example.FooFactory"/> <bean id="foo" factory-bean="fooFactory" factory-method="createFoo"> <constructor-arg index="0" value="fooValue"/> <constructor-arg index="1" value="barValue"/> </bean>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值