配置
来源:互联网 发布:洲际酒店 知乎 编辑:程序博客网 时间:2024/09/21 09:01
一 使用架包
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");