如何分析网站关键词/广东网站营销seo费用
1.导入jar包
- 四个核心(core,beans,context,expression)+一个依赖(Commons-logging)
我使用的Spring版本是3.2.0 导入的jar包如下图
2.目标类
- 提供UserService接口和实现类
- 获得UserService实现类的实例
在之前开发中,我们直接new对象即可
学习Spring之后将由Spring创建对象实例即IOC(控制反转) ,实例对象时,从Spring工厂中获得,需要将实现的类的全限定(包名+类名)名配置到xml文件中
UsersService接口
package com.scx.test01;public interface UsersService {public void show();
}
UsersService实现类
package com.scx.test01;public class UsersServiceImpl implements UsersService {@Overridepublic void show() {System.out.println("UsersServiceImpl被new了");}
}
3.配置文件
- 位置:任意 ,开发中一般在classpath下(src)
- 名称:任意,开发中常用applicationContext.xml
- 内容:添加scheme约束
约束文件位置:spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\ xsd-config.html
<?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 definitions here --><!-- 配置service <bean>配置需要创建的对象id:用于之后从spring容器中获得实例时使用的class:需要创建实例的全限定类名--><bean id="UsersServiceId" class="com.scx.test01.UsersServiceImpl"></bean>
</beans>
4.Junit测试
package com.scx.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.scx.test01.UsersService;
import com.scx.test01.UsersServiceImpl;public class Test {@org.junit.Testpublic void test01() {/** 之前开发 自己new对象*/UsersService usersService1=new UsersServiceImpl();usersService1.show();/** Spring开发 不需要自己new 都从Spring工厂中获得*/String xmlPath = "com/scx/test01/applicationContext.xml";//获得容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);//获得内容UsersService usersService2 = (UsersService) applicationContext.getBean("UsersServiceId");usersService2.show();}
}
运行结果:
从运行结果可以看出,第一次的结果由自己new对象实例化,而第二次的结果输出Spring相关信息后也输出了运行结果。利用Spring的IOC(控制反转)成功创建了对象。