?
?首先修改pom.xml引入log4j日志依赖:?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>spring02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<!--引入log4j日志坐标-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
创建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配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
id:名字唯一
class属性:类的全限定名
通过写bean标签告诉工厂生产的对象
-->
<!-- <bean id="person" class="com.itheima.Person"/>-->
<!-- <bean id="person1" class="com.itheima.Person"/>-->
<!-- <bean class="com.itheima.Person"/>-->
<!--bean的name属性
通过配置文件为所创建的成员变量赋值-->
<!-- <bean id="person" name="p,p1" class="com.itheima.Person">
<!– 为id属性赋值–>
<!– <property name="id">
<value>10</value>
</property>–>
<!– set注入简化写法–>
<property name="id" value="10"/>
<!– 为name属性赋值–>
<!– <property name="name">
<value>xiaojian</value>
</property>–>
<!– 简化写法–>
<property name="name" value="xiaojian"/>
<!–为数组属性email赋值–>
<property name="emails">
<list>
<value>xiaojian@qq.com</value>
<value>liucy@qq.com</value>
<value>chengyn@qq.com</value>
</list>
</property>
<!–为set集合赋初值–>
<property name="tels">
<set>
<value>13811111</value>
<value>13911111</value>
<value>16611111</value>
<value>16611111</value>
<value>16611111</value>
</set>
</property>
<!–为List集合赋初值–>
<property name="addresses">
<list>
<value>zpark</value>
<value>sahgndi</value>
<value>xierk</value>
<value>xierk</value>
<value>xierk</value>
</list>
</property>
<!–为map集合赋初值–>
<property name="qqs">
<map>
<entry>
<key>
<value>suns</value>
</key>
<value>423333365</value>
</entry>
<entry>
<key>
<value>chenyn</value>
</key>
<value>78642635</value>
</entry>
</map>
</property>
<!–为iproperties集合赋初值–>
<property name="p">
<props>
<prop key="key1">123</prop>
<prop key="key2">456</prop>
<prop key="key3">789</prop>
</props>
</property>
</bean>-->
<!--为UserServiceImpl类UserDAO赋初值
UserDAOImpl只使用一次没有别人调用,可以不指定id
-->
<!-- <bean id="userService" class="com.itheima.UserServiceImpl">-->
<!-- <property name="userDAO">-->
<!-- <bean class="com.itheima.UserDAOImpl"/>-->
<!-- </property>-->
<!-- </bean>-->
<!--解决上述代码文件中bean标签UserDaoImpl对象的重复创建,下面只需首先创建一次
就可以在property里用ref多次引用
-->
<bean id="userDAO" class="com.itheima.UserDAOImpl"></bean>
<!--<bean id="userService" class="com.itheima.UserServiceImpl">
<property name="userDAO" >
<ref bean="userDAO"/>
</property>
</bean>-->
<!--基于属性简化标签写法-->
<!--<bean id="userService" class="com.itheima.UserServiceImpl">
<property name="userDAO" ref="userDAO"/>
</bean>-->
<!-- 基于p标签的简化-->
<bean id="person" class="com.itheima.Person" p:name="xioajian" p:id="100"/>
<!-- 基于p标签自定义类型赋值的简化-->
<bean id="userService" class="com.itheima.UserServiceImpl" p:userDAO-ref="userDAO"/>
</beans>
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工厂配置文件中只能有一个<bean class是Person类型,否则会报异常
// Person bean = ctx.getBean(Person.class);
// System.out.println("person="+bean);
//(2)获得Spring工厂配置文件中,所有的bean标签的id值 person person1
// String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
// for (String beanDefinitionName:beanDefinitionNames){
// System.out.println("beanDefinitionName="+beanDefinitionName);
// }
//(3)获得特定类型获取Spring配置文件中对应的bean的id值
// String[] beanNamesForType = ctx.getBeanNamesForType(Person.class);
// for (String id:beanNamesForType){
// System.out.println("id="+id);
// }
//(4)用于判断是否存在指定id值得bean ,存在放回true,不存在返回false containsBeanDefinition()只能判断id不能判断name
// boolean person = ctx.containsBeanDefinition("person");
// System.out.println(person);
//(5)用于判断是否存在指定id值得bean containsBean():也可以判断name属性
if (ctx.containsBean("a")){
System.out.println("true="+true);
}else{
System.out.println("false="+false);
}
}
//测试Spring配置文件bean标签没有id属性,测试是否含有默认初始化
@Test
public void test3(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
for (String beanDefinitionName:beanDefinitionNames){
System.out.println("beanDefinitionName="+beanDefinitionName);
}
}
//用于测试name属性
@Test
public void test4(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Person person =(Person) ctx.getBean("p1");
System.out.println("Person="+person);
}
//测试注入(手动赋值):
@Test
public void test5(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Person person =(Person) ctx.getBean("person");
//通过代码为成员变量赋值,存在耦合
person.setId(1);//耦合
person.setName("suns");
System.out.println("Person="+person);
}
//通过spring的配置文件进行赋值(注入)
@Test
public void test6(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Person person =(Person) ctx.getBean("person");
System.out.println("Person="+person);
}
//测试为对象数组、集合注入赋值
@Test
public void test7(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Person person =(Person) ctx.getBean("person");
String[] emails=person.getEmails();
for (String email:emails){
System.out.println(email);
}
System.out.println("----------------");
//set集合是无序的,输出的时候,可能不安写入的时候输出
Set<String> tels=person.getTels();
for (String tel:tels){
System.out.println("tel="+tel);
}
System.out.println("==================");
//List集合是有序的可以粗重复的元素
List<String> addresses=person.getAddresses();
for (String address:addresses){
System.out.println("address="+address);
}
System.out.println("---------------------");
Map<String,String> qqs=person.getQqs();
Set<String> 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.
|