spring学习-IOC-基于XML的Bean创建
Posted 懒佯佯大哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring学习-IOC-基于XML的Bean创建相关的知识,希望对你有一定的参考价值。
示例1
-
创建一个maven工程,目录如下:
-
引入maven依赖:
<dependencies>
<!-- Spring aop依赖 核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- AspectJ是一个面向切面的框架:定义了AOP的语法,有一个专门的字节码生成器
来生成遵守java规范的class文件: https://www.jianshu.com/p/c42005d62620~-->
<!-- Spring 整合AspectJ框架的依赖包 -->
<!-- spring-aspects包依赖了aspectjweaver包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--spring-context:加载的上下文环境包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
</dependencies>
- 创建测试类:
package ioc1;
public interface HelloWorld
void sayHelloWorld();
class HelloWorldImpl implements HelloWorld
@Override
public void sayHelloWorld()
System.out.println("Hello World!!!!!!");
- resources目录:创建spring的ioc1.xml配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!--1. 通过全限定类名声明一个bean-->
<bean class="ioc1.HelloWorldImpl"/>
</beans>
- 创建测试类:
package ioc1;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class Main
public static void main(String[] args)
BeanFactory beanFactory = new ClassPathXmlApplicationContext("ioc1.xml");
System.out.println("========== 1. 通过全限定类名声明一个bean ==========");
// 类名查找时,需要将ioc1.xml中的配置中的其它5个注释掉,否则这里会找到多个
// bean是唯一的,多次获取一个bean的时候,是同一个实例
HelloWorld helloWorld1 = beanFactory.getBean(HelloWorldImpl.class);
HelloWorld helloWorld12 = beanFactory.getBean(HelloWorldImpl.class);
helloWorld1.sayHelloWorld();
System.out.println("地址为:" + helloWorld1);
helloWorld12.sayHelloWorld();
System.out.println("地址为:" + helloWorld12);
- 执行输出结果:
========== 1. 通过全限定类名声明一个bean ==========
Hello World!!!!!!
地址为:ioc1.HelloWorldImpl@7fad8c79
Hello World!!!!!!
地址为:ioc1.HelloWorldImpl@7fad8c79
- 结果说明:
- spring正常的生成了bean的实例,并且可以调用
- 生成的bean实例是唯一的(地址相同)
示例2
- 上述在xml中使用了类的全限定名,初步实现了HelloWorld的bean实例创建、获取、调用,下面再看其它的bean生成方式:
- 编辑ioc1.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!--1. 通过全限定类名声明一个bean-->
<bean class="ioc1.HelloWorldImpl"/>
<!--2. 通过id声明一个bean-->
<bean id="helloWorldById" class="ioc1.HelloWorldImpl"/>
<!--3. 通过全限定类名声明一个bean-->
<bean name="helloWorldByName" class="ioc1.HelloWorldImpl"/>
<!--4. 同时指定id和name-->
<bean id="helloWorldByIdAndName1" name="helloWorldByIdAndName2" class="ioc1.HelloWorldImpl"/>
<!--5. 同时指定多个name-->
<bean name="helloWorldByMultiName1;helloWorldByMultiName2;helloWorldByMultiName3" class="ioc1.HelloWorldImpl"/>
<!--6. 通过别名声明id-->
<alias name="helloWorldById" alias="helloWorldAlias"/>
</beans>
- 修改测试代码:
class Main
public static void main(String[] args)
BeanFactory beanFactory = new ClassPathXmlApplicationContext("ioc1.xml");
//System.out.println("========== 1. 通过全限定类名声明一个bean ==========");
// 类名查找时,需要将ioc1.xml中的配置中的其它5个注释掉,否则这里会找到多个
// bean是唯一的,多次获取一个bean的时候,是同一个实例
//HelloWorld helloWorld1 = beanFactory.getBean(HelloWorldImpl.class);
//HelloWorld helloWorld12 = beanFactory.getBean(HelloWorldImpl.class);
//helloWorld1.sayHelloWorld();
//System.out.println("地址为:" + helloWorld1);
//helloWorld12.sayHelloWorld();
//System.out.println("地址为:" + helloWorld12);
System.out.println("========== 2. 通过id声明一个bean ==========");
HelloWorld helloWorld2 = beanFactory.getBean("helloWorldById", HelloWorld.class);
helloWorld2.sayHelloWorld();
System.out.println("========== 3. 通过name声明一个bean ==========");
HelloWorld helloWorld3 = beanFactory.getBean("helloWorldByName", HelloWorld.class);
helloWorld3.sayHelloWorld();
System.out.println("========== 4. 同时指定id和name ==========");
HelloWorld helloWorld41 = beanFactory.getBean("helloWorldByIdAndName1", HelloWorld.class);
helloWorld41.sayHelloWorld();
HelloWorld helloWorld42 = beanFactory.getBean("helloWorldByIdAndName2", HelloWorld.class);
helloWorld42.sayHelloWorld();
System.out.println("========== 5. 同时指定多个name ==========");
HelloWorld helloWorld51 = beanFactory.getBean("helloWorldByMultiName1", HelloWorld.class);
HelloWorld helloWorld52 = beanFactory.getBean("helloWorldByMultiName2", HelloWorld.class);
helloWorld51.sayHelloWorld();
System.out.println("========== 6. 通过别名声明id ==========");
HelloWorld helloWorld6 = beanFactory.getBean("helloWorldAlias", HelloWorld.class);
helloWorld6.sayHelloWorld();
System.out.println("========== 验证:bean实例是否相等 ==========");
System.out.println("2和3为不同的id和name,为不同实例,故实例不相等:" + (helloWorld2 == helloWorld3));
System.out.println("4.同时指定id和name场景下,实例只有一个:" + (helloWorld41 == helloWorld42));
System.out.println("5.同时指定多个name时,实例只有一个:" + (helloWorld51 == helloWorld52));
- 输出结果:
========== 2. 通过id声明一个bean ==========
Hello World!!!!!!
========== 3. 通过name声明一个bean ==========
Hello World!!!!!!
========== 4. 同时指定id和name ==========
Hello World!!!!!!
Hello World!!!!!!
========== 5. 同时指定多个name ==========
Hello World!!!!!!
========== 6. 通过别名声明id ==========
Hello World!!!!!!
========== 验证:bean实例是否相等 ==========
2和3为不同的id和name,为不同实例,故实例不相等:false
4.同时指定id和name场景下,实例只有一个:true
5.同时指定多个name时,实例只有一个:true
参考
- https://blog.csdn.net/qq_22583741/article/details/79589910
- 莫羽清-spring讲解
以上是关于spring学习-IOC-基于XML的Bean创建的主要内容,如果未能解决你的问题,请参考以下文章
Spring5学习笔记 — “IOC操作Bean管理(基于xml)”