IoC之HelloWorld
来源:互联网 发布:拼豆豆图纸设计软件 编辑:程序博客网 时间:2024/06/08 19:45
IoC是控制反转,也被称为依赖注入。org.springframework.beans和org.springframework.context包是Spring框架IoC容器的基础。 BeanFactory接口管理任何类型对象。 ApplicationContext是BeanFactory子接口,web应用层中使用的WebApplicationContext
ClassPathXmlApplicationContext 或FileSystemXmlApplicationContext是ApplicationContext接口的实现类
配置
有三种配置方式,这里文章是基于XML配置方式
- 基于XML文件配置
- 基于注解的配置,从Spring2.5版本后引入的
- 基于java的配置,从Spring3.0版本后引入的
xml配置:创建一个XML文件,根元素是beans。使用bean子元素配置对象。
<?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="..." class="..."> <!-- 在这里写 bean 的配置和相关引用 --> </bean></beans>
实体类
public class Greeting { public void greeting(){ System.out.println("hello world!"); }}
在XML文件中配置
创建配置文件spring-ioc.xml。id指定唯一的名称,class指定类的全限定名称
<?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="greeting" class="com.devin.java.helloworld.Greeting"/></beans>
获取XML中配置对象
获得接口管理类对象,可以通过ClassPathXmlApplicationContext 或FileSystemXmlApplicationContext实现类来创建
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/spring-ioc.xml"});或者
ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{"src/main/resources/spring/spring-ioc.xml"});
然后根据方法T getBean(String name, Class<T> requiredType) 方法,可以获得配置对象的实例
Greeting greeting = context.getBean("greeting", Greeting.class); greeting.greeting();
引用文件路径问题
在beans中引用其他配置文件<import resource="spring-other.xml"/>在引用其他文件的路径是相对于本身文件所在目录,前面有/也是表示相对于本身文件所在目录。所以下面配置是一个效果
<import resource="spring-other.xml"/> <import resource="/spring-other.xml"/>
也可以使用 ./ 表示当前目录
<import resource="./spring-other.xml"/>
也可以使用classpath:和classpath*: 这个两个区别http://blog.csdn.net/kkdelta/article/details/5507799
<import resource="classpath:spring/spring-other.xml"/> <import resource="classpath*:spring/spring-other.xml"/>
创建容器接口对象时,也可以使用classpath
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:spring/spring-ioc.xml"}); Greeting greeting = context.getBean("greeting", Greeting.class); greeting.greeting();
0 0
- IoC之HelloWorld
- Helloworld之Spring依赖注入/控制反转(DI/IoC)版
- Spirng IoC/DI容器 的helloworld工程
- Spring 框架入门helloworld实例-------深入理解IOC、AOP
- SWT 入门之 -> HelloWorld
- drools之helloworld
- RMI 之helloworld(转)
- J2ME入门之HelloWorld
- Struts2之HelloWorld程序
- dwr入门之HelloWorld
- java之HELLOWORLD (applet)
- C#之HelloWorld
- java之HelloWorld
- SpringAOP之HelloWorld
- iText入门之HelloWorld
- DWR之HelloWorld
- aop 之 helloworld
- JMX之helloworld
- codeforces-755【C思维、并查集】
- wampserver2.5 apache2.4.9配置https 图文
- php简单的tocken验证
- 受到黑客攻击
- 关系型数据库与非关系型数据库....
- IoC之HelloWorld
- 指定长度进行剪切字符串
- android中Otto事件总线框架的使用
- pip安装selenium
- javaweb+tomcat实现通过ip访问项目
- AJAX - Jquery.ajax()
- 百度地图开发--解决地图无法显示的问题
- Android-View的事件体系
- 连接Access数据库