Spring的入门
一、Spring的jar包
链接:https://pan.baidu.com/s/1vnFkEAGHey06edMXeyNyZw
提取码:1dvm
二、java代码
UserDAO.java:
package com.lwc.dao;
/**
* 用户管理DAO层接口
* @author xinbai
*
*/
public interface UserDAO {
public void save();
}
UserDAOImpl .java:
package com.lwc.DaoImpl;
import com.lwc.dao.UserDAO;
/**
* 用户管理DAO层实现类
* @author xinbai
*
*/
public class UserDAOImpl implements UserDAO {
@Override
public void save() {
// TODO 自动生成的方法存根
System.out.println("UserDaoImpl执行了。");
}
}
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.xsd">
<!-- spring的入门标签 -->
<bean id="userDAO" class="com.lwc.DaoImpl">
</bean>
</beans>
测试类 demo.java:
package com.lwc.test;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lwc.DaoImpl.UserDAOImpl;
import com.lwc.dao.UserDAO;
public class demo {
@Test
public void demo2() {
UserDAO userDAO=new UserDAOImpl();
userDAO.save();
}
@Test
/**
* spring的方式的调用
*/
public void demo1() {
//创建Spring工厂
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserDAO userDAO=(UserDAO)applicationContext.getBean("userDAO");
userDAO.save();
}
}
三、 新建log4j
log4j.properties:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
# error warn info debug trace
log4j.rootLogger= info, stdout