Spring XML标签

本文详细介绍了Spring框架中bean、context、task、p、c、aop、import及util等核心标签的使用,包括bean的配置、依赖注入、作用域、初始化与销毁方法,以及context的组件扫描、属性占位符,task的定时任务配置,p和c标签简化属性赋值,aop的切面编程,import导入外部配置,以及util标签的集合和属性定义。这些内容对于理解和配置Spring框架至关重要。
摘要由CSDN通过智能技术生成

bean标签

<?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">
 <beans default-lazy-init="true">//在容器级别控制延迟初始化
<bean id="person"  //bean的id
           class="com.msb.test.Person" //对应的类
           scope="singleton" //bean的作用范围:(默认)singleton、prototype、request、session、application、websocket
           destroy-method="destory" //销毁时执行方法
           init-method="init" //初始化执行方法,在设置属性完成后执行
           factory-bean="personFactory" //通过该工厂来创建该bean
           factory-method="createPerson" //创建该bean的工厂方法
           lazy-init="true">  //是否为懒加载

    <bean id="person" scope="singleton" class="com.msb.test.Person0101"
          destroy-method="destory" init-method="init"
          lazy-init="true">
        <property name="name" value="小红"></property> //设置属性
        <property name="age" value="10"></property>
        <property name="cat" ref="cat"></property> //设置引用bean
        <property name="catList">
            <list value-type="com.msb.test.Cat"> //设置list bean
                <ref bean="cat"></ref>
                <ref bean="cat"></ref>
                <ref bean="cat"></ref>
            </list>
        </property>
        <property name="cats">
            <array> //设置array bean
                <ref bean="cat"></ref>
                <ref bean="cat"></ref>
                <ref bean="cat"></ref>
            </array>
        </property>
        <property name="catSet">
            <set value-type="com.msb.test.Cat"> //设置set bean
                <ref bean="cat"></ref>
            </set>
        </property>
        <property name="adminEmails"> //设置props(java.util.Properties)
            <props>
                <prop key="administrator">[emailprotected]</prop>
                <prop key="support">[emailprotected]</prop>
                <prop key="development">[emailprotected]</prop>
            </props>
        </property>
        <property name="someMap"> //设置map
            <map>
                <entry key="an entry" value="just some string"/>
                <entry key ="a ref" value-ref="myDataSource"/>
            </map>
        </property>
        空字符串 和 <null/>
    </bean>

    <bean id="personFactory" class="com.msb.test.PersonFactory" //设置工厂bean
        init-method="init" destroy-method="destory">
    </bean>
    <bean id="cat" class="com.msb.test.Cat" scope="prototype">
        <constructor-arg name="name" value="咪咪"></constructor-arg> //设置构造函数参数
        <property name="age" value="2"></property>
        <property name="color" value="白色"></property>
    </bean>
                                            依赖创建manager
    <bean id="beanOne" class="ExampleBean" depends-on="manager"/>
    <bean id="manager" class="ManagerBean">
        替换指定方法:子类要继承并实现或重写父类的方法
        <lookup-method name="createCommand" bean="myCommand"/> //如果方法是abstract,则动态生成的子类将实现该方法。否则,动态生成的子类将覆盖原始类中定义的具体方法。
        或者 @Lookup("myCommand")

        任意方法替换
        实现 MethodReplacer 接口,重写reimplement方法
        配置如下:
        <replaced-method name="computeValue" replacer="replacementComputeValue">
            <arg-type>String</arg-type>
        </replaced-method>
    </bean>

context标签

<?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"
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsdd">
  扫描指定包下面的bean
  <context:component-scan base-package="com.msb"></context:component-scan>
加载配置文件
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>

task标签(定时任务)

<?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:task="http://www.springframework.org/schema/task"
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task.xsd">

  <bean class="com.msb.job.MyJob" id="myJob"></bean>
    <task:annotation-driven></task:annotation-driven>
    <task:scheduled-tasks>
        <task:scheduled ref="myJob" method="job" cron="* * * * * ?"/>
    </task:scheduled-tasks>

p标签

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean name="john-modern"
        class="com.example.Person01"
        p:name="John Doe"/>

c标签

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsdd">
       <bean id="thingOne" class="x.y.ThingOne" c:thingTwo-ref="thingTwo" c:thingThree-ref="thingThree" c:email="[emailprotected]"/>
       <bean id="thingOne" class="x.y.ThingOne" c:_0-ref="thingTwo" c:_1-ref="thingThree"/>

aop标签

<?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:aop="http://www.springframework.org/schema/aop"
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">


    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean id="myAdvice" class="com.msb.test.MyAdvice"></bean>
    <aop:config>
        <aop:pointcut id="myPoint" expression="execution(* com.msb.test.LifeManage.myadvice(String)) and args(name)"></aop:pointcut>
        <aop:aspect id="myAdvice01" ref="myAdvice">
            <aop:before method="before" pointcut-ref="myPoint"></aop:before>
        </aop:aspect>
    </aop:config>

import标签

  <!--引入其他配置文件-->
    <import resource="file1.xml"></import>

util标签

<?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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util-4.1.xsd">

定义Properties
<util:properties id="utilProperties">
    <prop key="a">a</prop>
    <prop key="b">b</prop>
</util:properties>

<util:properties id="utilProperties" 
location="classpath:util.properties,classpath:application.properties" 
local-override="true">
    <prop key="a">a</prop>
    <prop key="b">b</prop>
</util:properties>

定义java.util.List
<util:list id="utilList">
    <value>1</value>
    <value>2</value>
</util:list>

定义java.util.Set
<util:set id="utilSet" value-type="java.lang.Integer">
    <value>1</value>
    <value>2</value>
</util:set>

定义java.util.Map
<util:map id="utilMap">
    <entry key="a" value="1"/>
    <entry key-ref="utilList" value="2" value-type="java.lang.Integer"/>
    <entry key="utilSet" value-ref="utilSet"/>
    <entry>
        <key>
            <value>b</value>
        </key>
        <value>2</value>
    </entry>
    <entry>
        <key>
            <ref bean="utilSet"/>
        </key>
        <ref bean="utilList"/>
    </entry>
</util:map>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值