搭建环境(参考:http://huangminwen.iteye.com/admin/blogs/1873922)
有如下接口:
package org.spring.service; public interface PersonService { public void showMessage(); }
实现类:
package org.spring.service.impl; import org.spring.service.PersonService; public class PersonServiceBean implements PersonService { public void showMessage() { System.out.println("Hello Spring World!"); } }
现在我们不想在代码中直接实例化PersonServiceBean类,想把它交给Spring容器管理,这时我们就需要在spring.xml中配置如下信息:
<?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-2.5.xsd"> <bean id="personServiceBean" class="org.spring.service.impl.PersonServiceBean" /> </beans>
在编写spring配置文件时,如果没有联网,则不能出现帮助信息,解决方法:
Window->Preferences->MyEclipse->Files and Editors->XML-> XML Catalog
点"add",在出现的窗口中的Key Type中选择URI,在location中选"File system",然后在spring解压目录, 的 dist/resources目录中选择spring-beans-2.5.xsd,回到设置窗口的时候,一定要把窗口中的Key Type改为Schema location,Key改为 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
beans.xml中的<bean>元素是用于配置我们要交给Spring管理的bean类,通过Bean中的id属性可以获得该Bean,该值不能包含特殊字符,Bean也可由name属性获得,与id属性所不同的是name属性可以包含特殊字符,如:(/spring/spring)
class属性:指定要交给Spring管理的Bean类,当spring容器启动后,因为spring容器可以管理bean对象的创建,销毁等生命周期,所以我们只需从容器直接获取Bean对象就行,而不用编写一句代码来创建bean对象。
测试类:
package org.spring.junit; import org.junit.Test; import org.spring.service.PersonService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring.xml"); PersonService personService = (PersonService) ctx .getBean("personServiceBean"); personService.showMessage(); } }
控制台打印信息:
工程目录结构图:
相关推荐
Spring4 HelloWorld初学Spring,java bean通过applicationContext.xml配置SpringIoC容器生成。
spring cloud 微服务helloworld项目,适合新手,项目基于spring cloud 微服务技术,使用了eureka注册公司,configserver配置中心,项目需要在本地新建配置中心配置文件
博文链接:https://hintcnuie.iteye.com/blog/198673
第一次共享自己的代码,Spring Hello World。代码比较简单,适合初学者,入门使用。高手就不要下载。呵呵。
这是spring MVC的HelloWorld完整实例,包含jar包
Spring AMQP hello world
我自己写的Spring MVC的一个HelloWold,从建HelloWorld工程,搭建环境Install spring,配置web.xml,配置SpringMVC的配置文件springDispatcherServlet-servlet.xml,添加控制器类,建jsp页面,部署 Tomcat,最后运行...
编译通过了,是在Eclipse平台开发Spring程序的小例子。入门可以看看~~
spring mvc helloworld myeclipse2013中的实例,加说明。
Spring MVC3.05,helloworld实例。含完整jar包。
spring笔记一helloworld,只是我学习spring框架,建立的第一个工程文件,导入的jar包,我的第一个spring- helloworld
NULL 博文链接:https://jiaozhiguang-126-com.iteye.com/blog/1843178
实现spring mvc 之helloworld
spring5.0的mvc项目实例,HelloWorld实例,基于eclipse oxygen jee 的maven project
spring security helloworld 简单例子,导入即可运行。
基于xml配置的spring mvc Helloworld实例。
博客示例代码,Spring boot 自学笔记(一) hello world 代码
Spring入门,基本配置信息,JavaBean的书写,配置文件的格式,配置文件的要求,Spring 所需的jar包,applcationContext的相关介绍
1. 先配置 nacos server 2. 启动 ProviderApplication.java类 3. 启动 ConsumerApplication.java类 4. 浏览器中输入 http://localhost:8082/say/hello