

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02
Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

Hello.java |
package javamxj.spring.beginning2; }public interface Hello { void sayHello(); |
HelloWorld.java |
package javamxj.spring.beginning2; public class HelloWorld implements Hello { public void sayHello() { System.out.println("Hello World!"); } } |
HelloJavamxj.java |
package javamxj.spring.beginning2; public class HelloJavamxj implements Hello { public void sayHello() { System.out.println("Hello, javamxj!"); } } |
HelloFactory.java |
package javamxj.spring.beginning2; public class HelloFactory { public Hello getHello(String hello) { if (hello.equals("world")) return new HelloWorld(); else if (hello.equals("javamxj")) return new HelloJavamxj(); else throw new IllegalArgumentException("输入参数错误!"); } } |
Test.java |
package javamxj.spring.beginning2; public class Test { public static void main(String[] args) { Hello hello = null; HelloFactory factory = new HelloFactory(); hello = factory.getHello("world"); hello.sayHello(); hello = factory.getHello("javamxj"); hello.sayHello(); } } |
HelloBean.java |
package javamxj.spring.beginning3; public class HelloBean { private String helloWorld = "Hello!World!"; public void setHelloWorld(String helloWorld) { this.helloWorld = helloWorld; } public String getHelloWorld() { return helloWorld; } } |
bean.xml |
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloBean" class="javamxj.spring.beginning3.HelloBean"> <property name="helloWorld"> <value>Hello! Javamxj!</value> </property> </bean> </beans> |
Main.java |
package javamxj.spring.beginning3; 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 Main { public static void main(String[] args) { // 直接调用HelloBean HelloBean helloBean = new HelloBean(); System.out.println(helloBean.getHelloWorld()); // 利用Spring调用HelloBean Resource res = new ClassPathResource("javamxj/spring/beginning3/bean.xml"); BeanFactory factory = new XmlBeanFactory(res); helloBean = (HelloBean) factory.getBean("helloBean"); System.out.println(helloBean.getHelloWorld()); } } |
src/log4j.properties |
log4j.rootLogger=warn, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n |
HelloBean.java |
package javamxj.spring.beginning4; public class HelloBean { private String helloWorld; public HelloBean(String helloWorld) { this.helloWorld = helloWorld; } public void sayHello() { System.out.println(helloWorld); } } |
bean.xml |
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloBean" class="javamxj.spring.beginning4.HelloBean"> <constructor-arg> <value>Hello! Javamxj!</value> </constructor-arg> </bean> </beans> |
Main.java |
package javamxj.spring.beginning4; 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 Main { public static void main(String[] args) { Resource res = new ClassPathResource("javamxj/spring/beginning4/bean.xml"); BeanFactory factory = new XmlBeanFactory(res); HelloBean helloBean = (HelloBean) factory.getBean("helloBean"); helloBean.sayHello(); } } |