配置

本文介绍了Spring框架的基本配置方法,包括两种不同的XML配置方式,并演示了如何通过BeanFactory和ApplicationContext来获取Bean实例。

 一 使用架包
spring.jar
commons-logging-1.0.4
aspectjrt.jar
aspectweaver.jar
cglib-node-2.1.3.jar
commons-dbcp.jar
commons-pool.jar
mysql-connector-java-5.0.0-beta-bin.jar

二 完整配置
1.方式一
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
                  "
http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="entity" class="entity.Entity">
        <property name="message" value="Hello" />
        <property name="name" value="Rod Jonhson"/>
    </bean>
</beans>

2.方式二
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <bean id="shopwaiterProxy" class="aopaspectjauto.ShopWaiter"></bean>
    <bean class="aopaspectjauto.TestAspect"></bean>
</beans>

三 说明
<bean
id="theAction"     //唯一标识
class="...Action"  //Java Bean
singlet="true"     //指定使用单例 在BeanFactory范围内
init-method="init"  //初始化方法 一般初始资源
destroy-method="cleanup"  //销毁方法 一般销毁资源
<property name="message">
<value>Hello</value>
<property name="dataSource">
<ref local="dataSource"/>
</property>
</property>
>

1.例子
<bean id="/doLogin,/doRegist" class="....">
        <property name="userDao">
                <ref bean="userdaoProxy"/>
        </property>
</bean>

四 一般使用
获得Bean方法
方法一:
ClassPathResource resource = new ClassPathResource("springtest/test.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
EntityInterface entity = (Entity) beanFactory.getBean("entity");
方法二:
ApplicationContext ctx =
               new ClassPathXmlApplicationContext("springtest2/test.xml");
Entity entity = (Entity)ctx.getBean("test");

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值