Spring-HelloWorld搭建

步骤:

1.首先编写JAVABEAN

2.配置CONFIG.XML

3.编写测试程序


LOG4J的配置

log4j.rootLogger=DEBUG\uFF0Cstdout
log4j.logger.org=ERROR,A1
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=org.log
log4j.appender.A1.MaxFileSize=500KB
log4j.appender.A1.MaxBackupIndex=50
log4j.appender.A1.Append=true
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ISO8601}-[%p][%c{1}]-%m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.File=gf.log
log4j.appender.stdout.MaxFileSize=500KB
log4j.appender.stdout.MaxBackupIndex=50
log4j.appender.stdout.Append=true
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.CoversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ss}%c-%m%n


编写存放HELLOWORLD的JAVA文件HELLOWORLD.JAVA

这个JAVABEAN用来存储将要展示的HELLOWORLD,并提供GET和SET方法


package com.gc.action;

public class HelloWorld {

	public String msg=null;//该变量用来存储字符串
	
	//设定变量msg的set方法
	public void setMsg(String msg) {
		this.msg=msg;
	}
	
	//获取变量msg的get方法
	public String getMsg() {
		return this.msg;
	}
	
}


配置SPRING的XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--定义一个Bean-->
    <bean id="HelloWorld" class="com.gc.action.HelloWorld">
    <!--将其变量msg通过依赖注入-->
    <property name="msg">
    <value>opzoonzhuzhengke</value>
    </property>
    </bean>
</beans>

 id="HelloWorld"用来唯一表示该BEAN
 class="com.gc.action.HelloWorld"
 name="msg"和对应变量对应的<value>opzoonzhuzhengke</value>,希望向JAVABEAN中注入字符串“opzoonzhuzhengke”



测试程序:

package com.gc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.gc.action.HelloWorld;

public class TestHelloWorld {
    public static void main(String[] args)
    {
    	//通过ApplicationContext来获取Spring文件的配置
    	ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
    
        //通过Bean的id来获取Bean
    	HelloWorld HelloWorld=(HelloWorld)actx.getBean("HelloWorld");
    	
    	//打印输出
    	System.out.println(HelloWorld.getMsg());
    }
}

测试程序利用ApplicationContext来获取config.xml,从而完成了JAVABEAN与XML之间关系的建立,接着就可以用getBean()方法获取在XML中定义的内容了


SPRING可以通过XML定义任意字符串,IOC,面向切面。


还有修改其来源(如下):


1.首先编写接口文件hello.java

2.通过接口,重写接口方法,GET和SET方法实现,完成EnHello.java和ChHello.java

3.修改CONFIG.XML

4.测试程序



1.接口

package com.gc.impl;

public interface Hello {
    //该方法用来输出问候语
    public String doSalutation();
}


2.两个类

package com.gc.action;

import com.gc.impl.Hello;

public class ChHello implements Hello{

	//该变量用来存储字符串
	public String msg=null;
	
	//设定变量msg的set方法
	public void setMsg(String msg) {
		this.msg=msg;
	}
	
	//获取变量msg的get方法
	public String getMsg() {
		return this.msg;
	}
	
	@Override
	public String doSalutation() {
		// TODO Auto-generated method stub
		return "你好"+this.msg;
	}
    
}


package com.gc.action;

import com.gc.impl.Hello;

public class EnHello implements Hello{

	//该变量用来存储字符串
	public String msg=null;
	
	//设定变量msg的set方法
	public void setMsg(String msg) {
		this.msg=msg;
	}
	
	//获取变量msg的get方法
	public String getMsg() {
		return this.msg;
	}
	
	@Override
	public String doSalutation() {
		// TODO Auto-generated method stub
		return "Hello"+this.msg;
	}
    
}

3.config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--定义一个Bean-->
    <bean id="HelloWorld" class="com.gc.action.EnHello">
    <!--将其变量msg通过依赖注入-->
    <property name="msg">
    <value>english zhuzhengke</value>
    </property>
    </bean>
</beans>


4.测试程序


package com.gc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.gc.action.HelloWorld;
import com.gc.impl.Hello;

public class TestHelloWorld {
    public static void main(String[] args)
    {
    	//通过ApplicationContext来获取Spring文件的配置
    	ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
    
        //通过Bean的id来获取Bean
    	Hello hello=(Hello)actx.getBean("HelloWorld");
    	
    	//打印输出
    	System.out.println(hello.doSalutation());
    }
}


输出:

Helloenglish zhuzhengke


修改CONFIG.XML的CLASS

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--定义一个Bean-->
    <bean id="HelloWorld" class="com.gc.action.ChHello">
    <!--将其变量msg通过依赖注入-->
    <property name="msg">
    <value>english zhuzhengke</value>
    </property>
    </bean>
</beans>

运行TEST程序,输出:

你好english zhuzhengke


仅仅改变了CONFIG的CLASS来源,测试程序的输出结果就改变了。

















   

你可能感兴趣的:(spring,bean,String,测试,Class,encoding)