1.项目结构
开发工具:IDEA + Maven
2.配置
2.1.Maven配置
Maven配置文件pom.xml,增加Spring和Log依赖
<?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">
<parent>
<artifactId>spring-ioc</artifactId>
<groupId>org.poiuy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-ioc-bean</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>
</project>
2.2.Log配置
Log4j2配置文件log4j2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration status="error">
<!--定义所有的appender-->
<appenders>
<!--控制台输出-->
<Console name="Console" target="SYSTEM_OUT">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<!--日志输出格式-->
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</Console>
<!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用 -->
<!--append为TRUE表示消息增加到指定文件中,false表示消息覆盖指定的文件内容,默认值是true -->
<File name="log" fileName="D:/logs/log4j2.log" append="false">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<!--添加过滤器ThresholdFilter,可以有选择的输出某个级别以上的类别 onMatch="ACCEPT" onMismatch="DENY"意思是匹配就接受,否则直接拒绝 -->
<File name="ERROR" fileName="D:/logs/error.log">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<!--这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
<RollingFile name="RollingFile" fileName="D:/logs/web.log"
filePattern="logs/$${date:yyyy-MM}/web-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<SizeBasedTriggeringPolicy size="2MB"/>
</RollingFile>
</appenders>
<!--然后定义logger,只有定义了logger并引入的appender,appender才会生效 -->
<loggers>
<root level="trace">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
<appender-ref ref="ERROR" />
<appender-ref ref="log"/>
</root>
</loggers>
</configuration>
2.3.Spring配置
Spring配置文件ApplicationContext_idName.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">
<!--id属性是唯一的,xml解析器能够在指定其他元素时做一些额外的校验 -->
<!--name属性可以使用一些不符合id规定的字符,并且可以指定多个name,使用逗号或者分号分隔 -->
<bean id="hello01" class="org.poiuy.spring.ioc.bean.domain.Hello">
<property name="message" value="Hello java" />
</bean>
<bean name="hello02,hello03" class="org.poiuy.spring.ioc.bean.domain.Hello">
<property name="message" value="Hello poi" />
</bean>
</beans>
3.Spring开发
使用Spring开发过程,即不用通过new方法显示的创建类对象,对象是从context中得到的。
-
通过Spring配置文件获得AbstractApplicationContext对象context
-
使用context的getBean方法获取类
-
获取到类后可以使用类的方法
package org.poiuy.spring.ioc.bean.application;
import org.poiuy.spring.ioc.bean.domain.Hello; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
-
@author:llian
-
使用id或者name来获取bean
-
@description:Bean注入
-
@date:11:29 2018-01-16
-
@modified: */ public class BeanIdNameApplication { public static void main(String[] args) { //加载配置文件生成AbstractApplicationContext AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/ApplicationContext_idName.xml"); //从context中获取Bean Hello hello01 = (Hello) context.getBean("hello01"); System.out.println(hello01);
Hello hello02 = (Hello) context.getBean("hello02"); System.out.println(hello02); Hello hello03 = (Hello) context.getBean("hello03"); System.out.println(hello03);} }
-