/*
commons-logging-1.0.4.jar
spring.jar
*/
//-----------------------------------------------------------
// 1/6
//D:\Indigo_workspace2\ReviewSpring\src\applicationContext.xml
<?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-2.0.xsd">
<!-- dao 实例 -->
<bean id="dao" class="com.helloweenvsfei.spring.example.DaoImpl">
</bean>
<!-- service 实例 -->
<bean id="serviceImpl"
class="com.helloweenvsfei.spring.example.ServiceImpl">
<property name="dao">
<bean class="com.helloweenvsfei.spring.example.DaoImpl"></bean>
</property>
</bean>
<bean id="service"
class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 拦截器
<property name="interceptorNames" value="theAdvisor"></property>
-->
<!-- 被拦截的对象 -->
<property name="target">
<ref local="serviceImpl" />
</property>
</bean>
<bean id="call_dao"
class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 拦截器
<property name="interceptorNames" value="theAdvisor"></property>
-->
<!-- 被拦截的对象 -->
<property name="target">
<ref local="dao" />
</property>
</bean>
</beans>
//-------------------------------------------------------------
// 2/6
package com.helloweenvsfei.spring.example;
//D:\Indigo_workspace2\ReviewSpring\src\com\helloweenvsfei\spring\example\DaoImpl.java
import java.util.Calendar;
public class DaoImpl implements IDao {
public String sayHello(String name) {
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if (hour < 6)
return "凌晨早, " + name;
if (hour < 12)
return "早上好, " + name;
if (hour < 13)
return "中午好, " + name;
if (hour < 18)
return "下午好, " + name;
return "晚上好, " + name;
}
}
//-------------------------------------------------------------
// 3/6
package com.helloweenvsfei.spring.example;
//D:\Indigo_workspace2\ReviewSpring\src\com\helloweenvsfei\spring\example\IDao.java
public interface IDao {
public String sayHello(String name);
}
//-------------------------------------------------------------
// 4/6
package com.helloweenvsfei.spring.example;
//D:\Indigo_workspace2\ReviewSpring\src\com\helloweenvsfei\spring\example\ServiceImpl.java
public class ServiceImpl implements IService {
private IDao dao;
public void service(String name) {
System.out.println("ServiceImpl~Just print out:" + dao.sayHello(name));
}
public IDao getDao() {
return dao;
}
public void setDao(IDao dao) {
this.dao = dao;
}
}
//-------------------------------------------------------------
// 5/6
package com.helloweenvsfei.spring.example;
public interface IService {
public void service(String name);
}
//-------------------------------------------------------------
// 6/6
//D:\Indigo_workspace2\ReviewSpring\src\com\helloweenvsfei\spring\example\SpringTest.java
package com.helloweenvsfei.spring.example;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class SpringTest {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
"applicationContext.xml"));
// 从配置文件中获取对象
IService hello = (IService) factory.getBean("service");
hello.service("Helloween");
IDao dao = (IDao) factory.getBean("call_dao");
String strHello=dao.sayHello("Jim");
System.out.println(strHello);
factory.destroySingletons();
}
}
//-------------------------------------------------------------