Spring:xml方式实现spring和配置log4j.properties
程序员文章站
2022-05-23 18:31:06
...
xml方式实现spring和配置log4j.properties
配置log4j.properties
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.category.org.springframework.beans.factory=DEBUG
package com.wuzl;
/**
* 消息服务,打印输出
*/
public class MessageServices {
public MessageServices() {
super();
System.out.println("MessageService...");
}
/**
* 执行打印功能
* @return 返回打印字符
*/
public String getMessage(){
return "hello word!";
}
}
package com.wuzl;
/**
* 打印机
*/
public class MessagePrint {
public MessagePrint() {
super();
System.out.println("MessagePrint...");
}
/**
* 建立和MessageService的关联关系
*/
private MessageServices service;
/**
* 设置service的值
* @param service
*/
public void setService(MessageServices service) {
this.service = service;
}
public void printMessage(){
System.out.println(this.service.getMessage());
}
}
package com.wuzl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 使用spring创建对象
*/
public class ApplicationSpring {
public static void main(String[] args) {
System.out.println("Spring打印");
//初始化spring容器,使用xml的方式
ApplicationContext context=new ClassPathXmlApplicationContext( "applicationContext.xml");
//从容器中获取MessagePrint对象
MessagePrint print=context.getBean(MessagePrint.class);
print.printMessage();
}
}
xml方式实现spring
配置文件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">
<!--
bean元素,描述当前的对象需要由spring管理
id属性:标识对象,未来在应用程序中可以根据id获取对象
class:被管理对象的类全名
-->
<bean id="service" class="com.wuzl.MessageServices"></bean>
<!--
使用bean关联关系
-->
<bean id="print" class="com.wuzl.MessagePrint">
<property name="service" ref="service"></property>
</bean>
</beans>
上一篇: Mysql高性能学习笔记-01
推荐阅读