对于Eclipse要求:
EclipseEE
新件项目:需要建成:dynamic web project
在WEB-INF/lib下需要的jar包:
整个项目的目录结构:
源码:
IDao.java
package com.yy.action;
public interface IDao {
public String sayHello(String name);
}
IService.java
package com.yy.service;
public interface IService {
public void service(String name);
}
DaoImpl.java
package com.yy.impl;
import java.util.Calendar;
import com.yy.action.IDao;
public class DaoImpl implements IDao{
@Override
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;
}
}
ServiceImpl.java
package com.yy.impl;
import com.yy.action.IDao;
import com.yy.service.IService;
public class ServiceImpl implements IService{
private IDao dao;
@Override
public void service(String name) {
System.out.println(dao.sayHello(name));
}
public void setDao(IDao dao) {
this.dao = dao;
}
public IDao getDao() {
return dao;
}
}
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">
<bean id="daoImpl" class="com.yy.impl.DaoImpl" />
<bean id="service" class="com.yy.impl.ServiceImpl">
<property name="dao" ref="daoImpl"></property>
</bean>
</beans>
log4j.properties
log4j.rootLogger=DEBUG,stdout,R
log4j.logger.org=ERROR, A1
log4j.logger.com.gc.action=DEBUG,A2
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=org.log
log4j.appender.A1.MaxFileSize=500KB
log4j.appender.A1.MaxBackupIndex=50
log4j.appender.A1.Append=true
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ISO8601} - [%p] [%C{1}] - %m%n
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=gc.log
log4j.appender.A2.MaxFileSize=500KB
log4j.appender.A2.MaxBackupIndex=50
log4j.appender.A2.Append=true
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d{ISO8601} - [%p] [%C{1}] - %m%n
#--------------------stdout--------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %c - %m%n
#--------------------R--------------------------------
#log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
#this log file will be stored in web server's /bin directory,modify to your path which want to store.
log4j.appender.R.File=gf.log
#log4j.appender.R.datePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.R.datePattern='.'yyyy-MM-dd
log4j.appender.R.append=true
## Keep one backup file
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %c - %m%n
#[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
注意事项:
1. applicationContext.xml 和 log4j.properties是需要放在WEB-INF/classes目录下的。
2. 运行项目的时候,选择Java Application运行。
至此,运行,可看到结果:
晚上好, 汪扬