Spring Demo
1. 导入jar
1.1 四个核心的Spring的jar:
spring-beans.jar
spring-core.jar
spring-context.jar
spring-expression.jar
位于Spring核心文件的夹/libs目录下
1.2 日志jar,是spring四个核心jar需要使用的:
位于Spring依赖库中
1.3 其他jar:
2. 定义Java类和接口
3. 定义Spring的容器配置文件,在文件中注册Java对象
由spring容器管理的java对象叫做:javabean,或者bean
4. 定义测试类
创建spring的容器对象,
容器接口:ApplicationContext
实现类:
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
1 2 3
| public interface SomeService { public void doSome(); }
|
1 2 3 4 5 6 7 8 9 10 11
| public class SomeServiceImpl implements SomeService { public SomeServiceImpl() { super(); System.out.println("SomeServiceImpl类的无参构造方法"); }
@Override public void doSome() { System.out.println("执行业务方法doSome()"); } }
|
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?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 id="someService" class="com.node.service.SomeServiceImpl" /> <bean name="someA,someB" class="com.node.service.SomeServiceImpl" /> </beans>
|
配置文件的路径:
- ClassPathXmlApplicationContext
类路径下: src目录下,String resource=”applicationContext.xml”;
- FileSystemXmlApplicationContext
磁盘目录: String resource=”E:/Eclipse/project/17_spring4_01_primary/applicationContext.xml”;
项目的根目录下,与src和libs目录是同级的: String resource = “applicationContext.xml”;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource;
public class MyTest {
public static void main(String[] args) { String resource="applicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(resource); SomeService service =(SomeService) ac.getBean("someService"); service.doSome(); }
@Test public void test01() { String resource="E:/Eclipse/project/17_spring4_01_primary/applicationContext.xml"; ApplicationContext ac=new FileSystemXmlApplicationContext(resource); SomeService service=(SomeService) ac.getBean("someService"); service.doSome(); }
@Test public void test02() { String resource = "applicationContext.xml"; ApplicationContext ac=new FileSystemXmlApplicationContext(resource); SomeService service=(SomeService) ac.getBean("someService"); service.doSome(); }
@Test public void test03() { String resource="applicationContext.xml"; BeanFactory factory= new XmlBeanFactory(new ClassPathResource(resource)); SomeService service=(SomeService) factory.getBean("someService"); service.doSome(); }
@Test public void test04() { String resource="applicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(resource); SomeService service =(SomeService) ac.getBean("someService"); service.doSome(); }
@Test public void test05() { String resource="applicationContext.xml"; BeanFactory factory= new XmlBeanFactory(new ClassPathResource(resource)); SomeService service=(SomeService) factory.getBean("someService"); service.doSome(); }
@Test public void test06() { String resource="applicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(resource); SomeService service =(SomeService) ac.getBean("someB"); service.doSome(); } }
|