First Spring
環(huán)境:MyEclipse6.0步驟:建立一個(gè)Java工程,選擇工程點(diǎn)擊右鍵->MyEclipse->Add Spring Capabilities
寫一個(gè)bean:
package spring.main.bean;
public class HelloBean {
private String helloWord;
public String getHelloWord() {
return helloWord;
}
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
}
}
寫Spring配置文件application.xml:public class HelloBean {
private String helloWord;
public String getHelloWord() {
return helloWord;
}
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
}
}
<?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-2.0.xsd">
<bean id="helloBean"
class="spring.main.bean.HelloBean">
<property name="helloWord">
<value>Hello!Epan Chen!</value>
</property>
</bean>
</beans>
寫一個(gè)log4j.properties放在src目錄下:<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-2.0.xsd">
<bean id="helloBean"
class="spring.main.bean.HelloBean">
<property name="helloWord">
<value>Hello!Epan Chen!</value>
</property>
</bean>
</beans>
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
最后寫一個(gè)測(cè)試類:log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
package spring.main.bean;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SpringDemo {
public static void main(String[] args) {
Resource rs = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean hello = (HelloBean)factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
運(yùn)行SpringDemo,出現(xiàn)如下內(nèi)容:import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SpringDemo {
public static void main(String[] args) {
Resource rs = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean hello = (HelloBean)factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
Hello!Epan Chen!
posted on 2008-10-07 14:12 育平 閱讀(242) 評(píng)論(0) 編輯 收藏