怎么在Java中搭建一个春天开发环境?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
<强>添加依赖包强>
进入春官网,切换到项目下点击Spring框架。官网上写的是以maven依赖的形式写的,所以可以新建一个maven项目,然后将下面的依赖加入到砰的一声。xml里
& lt; dependencies> & lt;才能dependency> ,,,& lt; groupId> org.springframework ,,,& lt; artifactId> spring-context ,,,& lt; version> 4.2.0.RELEASE & lt;才能/dependency> & lt;/dependencies>
或者,也可以点击这个页面右上角的叉我> & lt; ? xml version=?.0“,编码=癠TF-8" ?比; http://www.springframework.org/schema/beans" & lt; beans  xmlns=?; ,,,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance" ,xmlns: p=癶ttp://www.springframework.org/schema/p" ,,,xmlns:上下文=癶ttp://www.springframework.org/schema/context" ,,,xsi: schemaLocation=? http://www.springframework.org/schema/beans ,,,,,http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ,,,,,http://www.springframework.org/schema/context ,,,,,http://www.springframework.org/schema/context/spring-context-3.0.xsd"比; ,,& lt;上下文:component-scan 基础包=癱om.lcl"祝辞& lt;/背景:component-scan> & lt;/beans>
<强>这个配置文件有几个地方需要说明一下:强>
1,命名空间
& lt; beans xmlns=癶ttp://www.springframework.org/schema/beans" ,,,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance" ,xmlns: p=癶ttp://www.springframework.org/schema/p" ,,,xmlns:上下文=癶ttp://www.springframework.org/schema/context" ,,,xsi: schemaLocation=? http://www.springframework.org/schema/beans ,,,,,http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ,,,,,http://www.springframework.org/schema/context ,,,,,http://www.springframework.org/schema/context/spring-context-3.0.xsd"在
这个是xml的语法,配置当前xml文件中的标签格式,这里配置了上下文和p两个命名空间,例如,配了背景空间,就可以使用& lt;/背景:XXX>这样的标签。
2,自动扫描组件
& lt;上下文:component-scan 基础包=癱om.lcl"祝辞& lt;/背景:component-scan>
这个配置可以让春天框架自动扫描代码中用@ component注解了的类,自动创建这些类的对象。
最后注意一下bean.xml要放在代码目录下,其目的是为了将bean.xml添加到类路径中。
<强>编写代码强>
首先,写一个人类作为bean类。所谓bean类,简单来说就是所有成员变量都有getter和setter方法,并且有无参构造方法的类。然后用了<代码> @ component(“人”)代码>注解将人类标注为一个组件,这样,就可以使用@将人的一个实例注入给其他对象的成员里,或者使用应用程序类的<代码> getBean(类)代码>方法得到一个实例。
package com.lcl; import org.springframework.stereotype.Service; @ component (“person") public class  Person { private 才能;String 名称; private 才能;int 年龄; public 才能;String  getName (), { ,,,return 名称; ,,} public 才能;void  setName (String 名称),{ ,,,this.name =,名称; ,,} public 才能;int  getAge (), { ,,,return 年龄; ,,} public 才能;void  setAge (int 年龄),{ ,,,this.age =,年龄; ,,} public 才能;void  info () { ,,,System.out.println(“一起来吃麻辣烫!“); ,,,System.out.println(“名字:“+ getName() +“,年龄:“+ getAge ()); ,,} }
然后是一个类,一个类有人成员变量引用了人的实例,我们是用弹簧的依赖注入来管理成员变量人的创建,为了达到这个目的,只需要将人变量用@注解标注即可。
package com.lcl; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service;/* * ,* @author luchunlong ,* *大敌;2015年8月27日,上午10:20:41 ,*/@ component public class  A { ,@ private 才能;Person 人; public 才能;void  info () { ,,,person.setName (“abc"); ,,,person.setAge (123); ,,,person.info (); ,,} public 才能;static  void main (String [], args) { ,,,ApplicationContext ctx=new ClassPathXmlApplicationContext (“bean.xml"); null null null null怎么在Java中搭建一个春天开发环境