首先修改pom.xml引入log4j日志依赖:
4.0.0
com.itheima
spring02
1.0-SNAPSHOT
junit
junit
4.11
test
org.springframework
spring-context
5.1.14.RELEASE
org.slf4j
slf4j-log4j12
1.7.25
log4j
log4j
1.2.17
创建log4j的配置文件:
#resources文件夹根目录下
### 配置根
log4j.rootLogger=debug,console
### 日志输出到控制台
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -%m%n
UserServiceImpl类:添加一个userDAO属性,给它赋初值
package com.itheima;
public class UserServiceImpl implements UserService {
//private UserDAO userDAO=new UserDAOImpl();//这里也会出现耦合,需要工厂类解耦合
//使用工厂类解耦合
//private UserDAO userDAO=BeanFactory.getUserDAO();
//通用工厂方法
//private UserDAO userDAO=(UserDAO) BeanFactory.getBean("userDAO");
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public void regist(User user) {
userDAO.save(user);
}
@Override
public void login(String name, String password) {
userDAO.queryUserByNameAndPassword(name,password);
}
}
applicationContext.xml配置文件
SpringTest类:
package com.itheima;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class SpringTest {
//Spring的第一个程序
@Test
public void test1(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Person person =(Person) ctx.getBean("person");
System.out.println("Person="+person);
}
/*
Spring工厂提供的其他方法:
getBean():有多个重载的方法
*/
@Test
public void test2(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象的重载方法2 不清强制类型转换
// Person person = ctx.getBean("person", Person.class);
// System.out.println("Person="+person);
//这个方法,需要Spring工厂配置文件中只能有一个 tels=person.getTels();
for (String tel:tels){
System.out.println("tel="+tel);
}
System.out.println("==================");
//List集合是有序的可以粗重复的元素
List addresses=person.getAddresses();
for (String address:addresses){
System.out.println("address="+address);
}
System.out.println("---------------------");
Map qqs=person.getQqs();
Set keys=qqs.keySet();
for (String key:keys){
System.out.println("key="+key+" value="+qqs.get(key));
}
System.out.println("=================");
Properties p=person.getP();
System.out.println("key is key1"+" value is="+p.getProperty("key1"));
System.out.println("key is key2"+" value is="+p.getProperty("key2"));
}
@Test
public void test8(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
UserService userService = (UserService) ctx.getBean("userService");
userService.regist(new User("suns","123456"));
userService.login("xiaohei","99999");
}
}
test5:结果:
test6:
7.
8.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)