分类: Java
2014-04-01 11:32:59
spring-core.jar需commons-collections.jar,spring-core.jar是以下其它各个的基本。 spring-beans.jar需spring-core.jar/cglib-nodep-2.1_3.jar spring-aop.jar需spring-core.jar/spring-beans.jar/cglib-nodep-2.1_3.jar/aopalliance.jar spring-context.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/commons-collections.jar/aopalliance.jar spring-dao.jar需spring-core.jar/spring-beans.jar/spring-aop.jar/spring-context.jar spring-jdbc.jar需spring-core.jar/spring-beans.jar/spring-dao.jar spring-web.jar需spring-core.jar/spring-beans.jar/spring-context.jar spring-webmvc.jar需spring-core.jar/spring-beans.jar/spring-context.jar/spring-web.jar spring-hibernate.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/spring-dao.jar/spring-jdbc.jar/spring-orm.jar/spring-web.jar/spring-webmvc.jar spring-orm.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/spring-dao.jar/spring-jdbc.jar/spring-web.jar/spring-webmvc.jar spring-remoting.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/spring-dao.jar/spring-context.jar/spring-web.jar/spring-webmvc.jar spring-support.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/spring-dao.jar/spring-context.jar/spring-jdbc.jar spring-mock.jar需spring-core.jar/spring-beans.jar/spring-dao.jar/spring-context.jar/spring-jdbc.jar插件介绍
public interface IHelloWorld
{
String sayHelloWorld();
}
public class HelloWorld1 implements IHelloWorld
{
public HelloWorld1()
{
super();
}
public String sayHelloWorld()
{
return "Hello World HelloWorld1";
}
}
public class HelloWorld2 implements IHelloWorld
{
public HelloWorld2()
{
super();
}
public String sayHelloWorld()
{
return "Hello World HelloWorld2";
}
}
<xml version="1.0" encoding="UTF-8"> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/dtd/spring-beans.dtd"> <beans> <bean id="beanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constrUCtor-arg> <list> <value>beanRefDataAccess.xml</value> <value>beanRefService.xml</value> <value>beanRefMVC.xml</value> </list> </constructor-arg> </bean> </beans>
<xml version="1.0" encoding="UTF-8"> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/dtd/spring-beans.dtd"> <beans> <bean id="helloWorldDAO1" class="HelloWorld1"/> <bean id="helloWorldDAO2" class="HelloWorld2"/> </beans>
<xml version="1.0" encoding="UTF-8"> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/dtd/spring-beans.dtd"> <beans> <bean id="helloWorld1" class="HelloWorld1"/> <bean id="helloWorld2" class="HelloWorld2"/> <bean id="springDemoConstructor" class="SpringDemoConstructor"> <constructor-arg> <value>Spring IDE Constructor</value> </constructor-arg> <property name="helloWorld"> <ref bean="helloWorld1"></ref> </property> </bean> <bean id="springDemoSetter" class="SpringDemoSetter"> <property name="hello" value="Spring IDE Setter"/> <property name="helloWorld"> <ref bean="helloWorld2"></ref> </property> </bean> </beans>
<xml version="1.0" encoding="UTF-8"> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/dtd/spring-beans.dtd"> <beans> <bean id="helloWorldMVC1" class="HelloWorld1"/> <bean id="helloWorldMVC2" class="HelloWorld2"/> </beans>
public interface ISpringDemo
{
IHelloWorld getHelloWorld();
String getHello();
}
public class SpringDemoConstructor implements ISpringDemo
{
private String hello;
private IHelloWorld helloWorld;
public SpringDemoConstructor(String hello)
{
this.hello = hello;
}
public String getHello()
{
return hello;
}
public IHelloWorld getHelloWorld()
{
return helloWorld;
}
public void setHelloWorld(IHelloWorld helloWorld)
{
this.helloWorld = helloWorld;
}
}
public class SpringDemoSetter implements ISpringDemo
{
private String hello;
private IHelloWorld helloWorld;
public String getHello()
{
return hello;
}
public void setHello(String hello)
{
this.hello = hello;
}
public IHelloWorld getHelloWorld()
{
return helloWorld;
}
public void setHelloWorld(IHelloWorld helloWorld)
{
this.helloWorld = helloWorld;
}
}
<xml version="1.0" encoding="UTF-8"> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/dtd/spring-beans.dtd"> <beans> <bean id="helloWorld1" class="HelloWorld1"/> <bean id="helloWorld2" class="HelloWorld2"/> <bean id="springDemoConstructor" class="SpringDemoConstructor"> <constructor-arg> <value>Spring IDE Constructor</value> </constructor-arg> <property name="helloWorld"> <ref bean="helloWorld1"></ref> </property> </bean> <bean id="springDemoSetter" class="SpringDemoSetter"> <property name="hello" value="Spring IDE Setter"/> <property name="helloWorld"> <ref bean="helloWorld2"></ref> </property> </bean> </beans>
<ref local="helloWorld2"></ref>
<ref bean="helloWorld2"></ref>
<file> <pub>-//SPRING//DTD BEAN//EN</pub> <abs>/dtd/spring-beans.dtd</abs> <rel>spring-beans.dtd</rel> </file>
# Set root logger level to ERROR and its only appender to R.
log4j.rootLogger = INFO,R
# R is set to be a DailyRollingFileAppender.
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = Application.log
log4j.appender.R.DatePattern = yyyy-MM-dd'.log'
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.beans.factory.access.SingletonBeanFactoryLocator;
public final class ServiceFactory
{
private static BeanFactoryLocator bfLocator = null;
private static BeanFactoryReference bfReference = null;
private static BeanFactory factory = null;
static
{
bfLocator = SingletonBeanFactoryLocator.getInstance();
bfReference = bfLocator.useBeanFactory("beanFactory");
factory = bfReference.getFactory();
}
private ServiceFactory()
{
super();
}
public static Object getBeanByName(final String beanName)
{
return factory.getBean(beanName);
}
}
做成静态的这样我们可以ServiceFactory .getBeanByName(“beanname”)就可以得到相应的bean了。import junit.framework.TestCase;
public class SpringIDETest extends TestCase
{
private IHelloWorld helloWorld = null;
private ISpringDemo springDemo = null;
private final static String hello1 = "Hello World HelloWorld1";
private final static String hello2 = "Hello World HelloWorld2";
private final static String helloset = "Spring IDE Setter";
private final static String hellocon = "Spring IDE Constructor";
public void testSpringBeans()
{
helloWorld = (IHelloWorld)ServiceFactory.getBeanByName("helloWorld1");
assertEquals(hello1,helloWorld.sayHelloWorld());
helloWorld = (IHelloWorld)ServiceFactory.getBeanByName("helloWorld2");
assertEquals(hello2,helloWorld.sayHelloWorld());
}
public void testIoCConstructor()
{
//Constructor
springDemo = (ISpringDemo)ServiceFactory.getBeanByName("springDemoConstructor");
assertEquals(hellocon,springDemo.getHello());
assertEquals(hello1,springDemo.getHelloWorld().sayHelloWorld());
}
public void testIoCSetter()
{
//Setter
springDemo = (ISpringDemo)ServiceFactory.getBeanByName("springDemoSetter");
assertEquals(helloset,springDemo.getHello());
assertEquals(hello2,springDemo.getHelloWorld().sayHelloWorld());
}
}