您好,登錄后才能下訂單哦!
怎么在Java中搭建一個Spring開發(fā)環(huán)境?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
添加依賴包
進入spring官網(wǎng),切換到projects下點擊 spring framework.官網(wǎng)上寫的是以maven依賴的形式寫的,所以可以新建一個maven項目,然后將下面的依賴加入到pom.xml里
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.0.RELEASE</version> </dependency> </dependencies>
或者,也可以點擊這個頁面右上角的fork me on github,在github里下載依賴包,然后加入到項目的build path中去。
注意, spring-context只是包含了spring最核心的功能,如依賴注入,切面等。
創(chuàng)建spring配置文件
新建一個名為bean.xml的配置文件,放到代碼目錄里,文件的內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://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"> <context:component-scan base-package="com.lcl"></context:component-scan> </beans>
這個配置文件有幾個地方需要說明一下:
1、命名空間
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://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的語法,配置當(dāng)前xml文件中的標(biāo)簽格式,這里配置了context和p兩個命名空間。例如,配了context空間,就可以使用</context:XXX>這樣的標(biāo)簽。
2、自動掃描組件
<context:component-scan base-package="com.lcl"></context:component-scan>
這個配置可以讓spring框架自動掃描代碼中用@component注解了的類,自動創(chuàng)建這些類的對象。
最后注意一下bean.xml要放在代碼目錄下,其目的是為了將bean.xml添加到classpath中。
編寫代碼
首先,寫一個Person類作為bean類。所謂bean類,簡單來說就是所有成員變量都有g(shù)etter和setter方法,并且有無參構(gòu)造方法的類。然后用了@Component(“person”)
注解將Person類標(biāo)注為一個組件,這樣,就可以使用@Resource將Person的一個實例注入給其他對象的成員里,或者使用Application類的getBean(Class)
方法得到一個實例。
package com.lcl; import org.springframework.stereotype.Service; @Component("person") public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void info(){ System.out.println("一起來吃麻辣燙!"); System.out.println("name:"+getName()+" age:"+getAge()); } }
然后是A類,A類有person成員變量引用了Person的實例,我們是用spring的依賴注入來管理成員變量person的創(chuàng)建,為了達到這個目的,只需要將person變量用@Resource注解標(biāo)注即可。
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 { @Resource private Person person; public void info(){ person.setName("abc"); person.setAge(123); person.info(); } public static void main(String[] args){ ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml"); A a=ctx.getBean(A.class); a.info(); } }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。