debug、info、warn、error是log4j的重要数据 优先级,从高到低:error>warn>info>debug 如果配置文件中定义了上一级的,那么低一级的信息将不会被打印出来
slf4j simple log facade for java (java日志门面) slf4j的实现原理 假设日志系统A的info日志输出方法如下 A.message(); 日志系统B的info日志输出方法如下 B.show(); log4j的info日志输出方法如下: logger.getInfo() slf4j的实现就是: slf4j slf4j = new slf4j (A); log.info(); 这样我们用的就是日志系统A的方法, 如果使用lo4j来打印日志,仅仅需要配置新的日志源即可 slf4j slf4j = new slf4j(log4j); log.info(); 而不需要更改其他代码所以它两者的区别是: log4j是真正实现日志功能的产品,同类产品很多 slf4j 是一个适配器,当我们的系统换了一个日志源后,不需要更改代码
spring是分层的 Java SE/EE 应用 service、dao、web 轻量级开源框架 分为IOC(控制反转)和AOP(面向切面) 特点: 提供了展现层 SpringMVC 持久层 Spring JDBC 能整合开源第三方框架和类库 业务层事务管理 方便解耦,简化开发 IOC
spring框架体系 Test :用于测试使用 Core container:核心容器,就是用于装Java Bean对象 AOP:切面编程 Aspects:提供了与AspectJ的集成 Data access:数据访问。用于访问操作我们的数据库。支持持久层的操作。jdbcTemplate mybatis Web:用于支持数据展示层,支持http请求 Transactions:用于支持事物处理。用于解决业务层的事物处理问题。 编程式事务管理和声明式事务管理.控制反转 把原来new对象的这种方式转换成了spring通过反射创建对象的方式 spring创建完的对象放到一个容器中,谁需要就给谁注入进去 通俗来说,就是把创建对象和管理对象的权利交给spring
添加spring依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.9.RELEASE</version> </dependency>导入后包含四个模块 创建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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">Test.java
public class Test { @Test public void test01(){ ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) context.getBean("person"); System.out.println(person); } } public void test01(){ Person person= (Person) context.getBean("person1"); log.debug(person.toString()); }applicationContext.xml 赋值:
<bean id="person" class="com.xxx.xxxx.Person" scope="prototype"/> <bean id="person1" class="com.xxx.entity.Person" > <property name="id" value="10"/> <property name="name" value="tom"/> <property name="age" value="20"/> <property name="birthday" ref="date"/> </bean>标签id:bean标签的识别ID,理论上可以随便写 标签class:Spring给你创建的类的对象,需要写上该类的全路径名
还有一种赋值方法:
public void test02(){ Person person1=new Person(); person1.setId(1); log.debug(person1.toString()); Person person2=new Person(1,"tom",20,new Date()); log.debug(toString()); }构造方法创建对象
<bean id="person2" class="com.xxx.xxxx.Person"> <constructor-arg name="id" value="10"/> <constructor-arg name="name" value="tom"/> <constructor-arg name="age" value="20"/> <constructor-arg name="birthday" ref="date"/> </bean>1.value可以赋上基本类型数据与String,但是其他对象,要使用ref 2.在一个容器中查找已经存在的对象
通过调用静态方法获取bean对象
public static Person getBean() { return new Person(); } <bean class="com.xxx.xxx.PersonFactory" factory-method="getBean" id="person3"/>通过工厂对象调用成员方法获得bean对象 xxxFactory factory = new XxxFactory(); //实例化工厂对象 factory .xxxx(); //获得对象 标签factory-bean: 创建工厂对象 标签factory-method: 调用方法获得bean对象
<bean factory-bean="factory1" factory-method="getBean" id="person4"/>单例是内存中只有一个对象,每次获取到该对象的地址值一样。 多例是内存中的每个对象都是一个新的对象,他们的地址值都不同。 spring默认的情况下创建的对象都是单例的。 单例:
<bean id="person" class="com.xxx.xxxx.Person" scope="singleton"/>多例:
<bean id="person" class="com.xxx.xxxx.Person" scope="prototype"/>单例:singleton 多例:prototype
创建方法init 普通方法service 销毁方法destory 标签init-method: 当该对象初始化的时候该方法会自动执行 标签destroy-method: 当该对象即将销毁的时候会自动调用该方法
<bean id="person5" class="com.xxx.xxxx.Person" init-method="init" destroy-method="destory" />给对象的属性设置值 set方法给对象设置值 构造方法给对象初始化的时候设置值 当spring调set方法,其类中必须有set方法
private PersonDao personDao; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } 复杂属性依赖注入 private String[] arr; public void setArr(String[] arr) { this.arr = arr; } public String[] getArr() { return arr; } //List private List<String> list; public void setList(List<String> list) { this.list = list; } //set private Set<String> set; public void setSet(Set<String> set) { this.set = set; } //map private Map<String,String> map; public void setMap(Map<String, String> map) { this.map = map; } //properties private Properties properties; public void setProperties(Properties properties) { this.properties = properties; } <bean id="person6" class="com.xxx.xxxx.Person"> <property name="name" value="tom"/> <property name="arr"> <array> <value >rose</value> <value >rose</value> <value >rose</value> </array> </property> <property name="list"> <list> <value >rose1</value> <value >rose2</value> <value >rose3</value> </list> </property> <property name="set"> <set> <value >rose</value> <value >rose</value> <value >rose3</value> </set> </property> <property name="map"> <map> <entry key="10010" value="rose1"/> <entry key="10086" value="rose2"/> <entry key="10000" value="rose3"/> </map> </property> <property name="properties"> <props> <prop key="10010">rose1</prop> <prop key="10086">rose2</prop> <prop key="10000">rose3</prop> </props> </property> </bean> xml方式注入Dao Test @Test public void test09(){ PersonService personService = (PersonService) context.getBean("personService"); Person p = new Person(); p.setUsername("tom"); p.setPassword("123456"); boolean flag =personService.login(p); log.debug(flag+""); }PersonService
public class PersonService { private static final Logger log= LoggerFactory.getLogger(PersonService.class); private PersonDao personDao ; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public boolean login(Person p) { log.debug(p+" login"); Person person = personDao.find(p); if(person==null) { return false; }else{ return true; } } }PersonDao
public class PersonDao { public Person find(Person p){ if ("tom".equals(p.getUsername())&&"123456".equals(p.getPassword())){ return p; }else{ return null; } } }applicationContext.xml
<bean id="personService" class="com.xxx.xxxx.PersonService"> <property name="personDao" ref="personDao"/> </bean> <bean id="personDao" class="com.xxx.xxxx.PersonDao"> </bean> 注解注入 查找之后,进行赋值第一种:@Autowired 要么 @Autowired与 @Qualifier(“personDao”)同时存在 第二种:@Value("#{bean的id}") 第三种:@Resource(name=“bean的id值”)
