[Spring]IOC&DI&AOP的XML配置方法

本文详细介绍Spring框架的基础配置过程,包括环境搭建所需的jar包、约束文件的配置、如何将类托管给Spring、依赖注入的不同方式及简化配置等。此外,还介绍了AOP的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.环境准备

1.jar包:

可以在官方文档index.html里面看到:


所以我们就导下面几个jar包:

spring-beans-4.2.9.RELEASE.jar

spring-core-4.2.9.RELEASE.jar

spring-context-4.2.9.RELEASE.jar

spring-expression-4.2.9.RELEASE.jar

2.约束:

可在官方文档index.html中搜索关键字schema:


就能看到基本的demo:



<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:oxm="http://www.springframework.org/schema/oxm" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd                                              http://www.springframework.org/schema/oxm                                                                  http://www.springframework.org/schema/oxm/spring-oxm.xsd">


本地的约束文件在schema目录下,自己可以导

二.将类托管到Spring中(配置xml)

1.托管类(bean标签)

bean标签:将指定的类托管给spring

<bean id="us" class="com.robin.service.impl.UserServiceImpl" scope="prototype"
        init-method="init" destroy-method="destroy">
</bean>
<!--
    id:Bean标识符,可通过该id,向Spring工厂申请到实例
    class:全限定名
    scope:指定生成的实例是单例/多例/其他...默认为单例singleton,多例为prototype
        在Spring的html文档beans.html中有详细说明
    init-method:生成实例前调用的方法
    destory-method:销毁实例前调用的方法
-->


2.依赖注入(Dependency Injection)

依赖注入(DI):比如说需要创建某个类(TestBean2)的实例,我们刚才已经将其托管给了Spring,里面有个成员变量,我们希望Spring创建该类实例时,能够将里面的成员变量也注入进去:

public class TestBean2 {
    private String name2;//希望Spring帮我们实例化TestBean2时,也设置好这个成员变量name2
    public TestBean2() {
    }
    public TestBean2(String name2) {
        System.out.println("TestBean2:有参构造赋值 name2 = "+name2);
        this.name2 = name2;
    }
    public String getName2() {
        return name2;
    }
    public void setName2(String name2) {
        System.out.println("TestBean2:Set赋值 name2 = "+name2);
        this.name2 = name2;
    }
    @Override
    public String toString() {
        return "TestBean2 [name2=" + name2 + "]";
    }

2.1注入普通对象:

注入普通对象,也就是可以直接写值的,如数值类型,字符串类型...,就直接写值就可以了:

通过构造函数(普通对象)
<bean id="test2" class="com.robin.bean.TestBean2"  scope="prototype">
        <constructor-arg name="name2" value="Test2"/>
</bean>
<!--
    name:注入的属性名
    value:注入值
-->

通过set方法(普通对象)
<bean id="test3" class="com.robin.bean.TestBean3"  scope="prototype">
        <property name="name3" value="Test3"/>
</bean>
<!--
    name:注入的属性名
    value:注入值
-->


2.2注入特殊对象

注入特殊对象,就是不能直接写值的,如自己定义的各种对象,则1.这个特殊对象也要托管给Spring,并且要求注入的对象,还要2.提供被注入对象的set方法:


public void setUser(User user) {
        System.out.println("TestBean4:set赋值 name4 = "+user);
        this.user = user;//1.User也要被托管给Spring  2.提供set方法
}

通过构造函数(特殊对象:没办法直接写值的)
<bean id="test4" class="com.robin.bean.TestBean4"  scope="prototype">
        <constructor-arg name="user" ref="user2"/>
</bean>
<!--
    name:注入的属性名
    ref:注入的对象的类的id
-->

通过set方法(特殊对象:没办法直接写值的)
<bean id="test4" class="com.robin.bean.TestBean4"  scope="prototype">
        <property name="user" ref="user2"/>
</bean>
<!--
    name:注入的属性名
    ref:注入的对象的类的id
-->

2.3注入类型(数组/list/map):

同样有普通类和特殊类的区别,一般用ref/value来区分:

<!--ref:代表里面成员是特殊类,需要指明托管类id-->

<!--value:代表里面成员是普通类,直接写值即可-->

数组:

<bean id="test8" class="com.robin.bean.TestBean8"  scope="prototype">
        <property name="users">
            <array>
                <ref bean="user2"/> <!--ref:代表里面成员是特殊类,需要指明托管类id--> 
                <ref bean="user2"/> <!--value:代表里面成员是普通类,直接写值即可-->  
                <ref bean="user2"/>         
            </array>
        </property>
</bean>

list:

<bean id="test5" class="com.robin.bean.TestBean5"  scope="prototype">
        <property name="list">
            <list>
                <value>元素1</value>
                <value>元素2</value>
                <value>元素3里面有个逗号</value>
            </list>
        </property>
</bean>

map:

map比较特殊,每个成员是entry,每个entry是key-value对存在

<bean id="test7" class="com.robin.bean.TestBean7"  scope="prototype">
        <property name="map">
            <map>
                <entry key="1" value-ref="user2"/>
                <entry key="2" value-ref="user2"/>
                <entry key="3" value-ref="user2"/>
            </map>
        </property>
</bean>

2.4 p名称空间与c名称空间:

依赖注入的方式已经固定化了,就是 有参构造 | set方法。 但是spring担心程序员在xml的配置要写的代码很多,然后程序员就不喜欢用了。所以spring针对xml的注入,提出了两种简化写法 : p 名称空间 | c名称空间

p 名称空间

针对的是这个<property name="" /> , 也就表明了代码里面声明注入的方式是 set方式

<bean id="us" class="com.itheima.service.impl.UserServiceImpl" p:address="北京">

c名称空间


针对的是这个<constructor-arg/>, 也就要求我们的代码声明注入的方式必须是有参构造

<bean id="us" class="com.itheima.service.impl.UserServiceImpl" c:address="北京">

三.获取容器(上下文),从容器中获取对象

由于这个跟bean相关,所以可以找官方文档beans.html,向下翻一点就能找到demo.


// create and configure beans
ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});//后面接xml的路径,获取context
​
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);//使用getBean方法获取bean
​
// use configured instance
List<String> userList = service.getUsernameList();//后面就可以使用这个对象了
​
​
// 3.关闭context
((AbstractApplicationContext) context).close();//关闭context(AbstractApplicationContext是ClassPathXmlApplicationContext的父类,也是ApplicationContext接口的实现类)

AOP XML开发

一.环境准备

1.jar包:

需要导入4个包:

aop联盟接口包

aspect接口包

spring aop联盟实现包

spring aspect实现包

2.约束

找到官方文档index.html,搜索关键字AOP,能够找到:


跳转过去,搜索关键字<?xml version="1.0" encoding="UTF-8"?>能够找到它的约束的写法:

 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
​
    <!-- a service object; we will be profiling its methods -->
    <bean id="entitlementCalculationService"
            class="foo.StubEntitlementCalculationService"/>
​
    <!-- this switches on the load-time weaving -->
    <context:load-time-weaver/>
</beans>

其实这里还不全,还需要一个这样的名称空间:

 
xmlns:aop="http://www.springframework.org/schema/aop"
​
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"


3.AOP

再上下翻一下,能够看到A first example,有介绍些快速入门:


1.将pointcut和目标类托管到Spring

2.设置pointcut(切入点),以及作用范围

3.指明pointcut的类/方法

<bean id="log" class="com.robin.log.LogDemo"></bean>                             <!--增强类-->
<bean id="testService" class="com.robin.service.impl.TestServiceImpl"></bean>    <!--被增强类-->

 
<aop:config>
        <!--
        1.id:指明这个切入点id为power
        2.execution:指明这个切入点作用的类/方法(符合这个表达式的所有方法,都会被这个pointcut影响):
              第一个*:表示所有返回值
              第二个*:表示所有类
              第三个*:表示所有方法
             ..:表示参数     
        -->
        <aop:pointcut id="power" expression="execution(* com.robin.service.impl.*.*(..))"/>
        <!--
        1.ref:增强类的id:log 
        2.method:增强类的方法名 
        3.pointcut-ref:pointcut id:power
        -->
        <aop:aspect ref="log"> 
            <aop:before method="log" pointcut-ref="power"/>
        </aop:aspect>
    </aop:config> 
​
<!--
流程:
1.先将  a.增强类(你要插入的方法)和  b.被代理类(被增强的方法) 托管给spring(配置bean)
2.定义好pointcut的id,以及作用范围expression(配置aop:pointcut)
3.配置增强类id及其方法名method,并指定pointcut的id(配置aop:aspect及其子标签) 
-->


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值