Java Spring開發(fā)環(huán)境搭建及簡單入門示例教程
本文實(shí)例講述了Java Spring開發(fā)環(huán)境搭建及簡單入門示例。分享給大家供大家參考,具體如下:
前言
雖然之前用過Spring,但是今天試著去搭建依然遇到了困難,而且上網(wǎng)找教程,很多寫的是在web里使用Spring MVC的示例,官方文檔里的getting start一開始就講原理去了(可能打開的方法不對(duì))。沒辦法,好不容易實(shí)驗(yàn)成功了,記下來免得自己以后麻煩。
添加依賴包
進(jìn)入spring官網(wǎng),切換到projects下點(diǎn)擊 spring framework.官網(wǎng)上寫的是以maven依賴的形式寫的,所以可以新建一個(gè)maven項(xiàng)目,然后將下面的依賴加入到pom.xml里
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
</dependencies>
或者,也可以點(diǎn)擊這個(gè)頁面右上角的fork me on github,在github里下載依賴包,然后加入到項(xiàng)目的build path中去。
注意, spring-context只是包含了spring最核心的功能,如依賴注入,切面等。
創(chuàng)建spring配置文件
新建一個(gè)名為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>
這個(gè)配置文件有幾個(gè)地方需要說明一下:
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">
這個(gè)是xml的語法,配置當(dāng)前xml文件中的標(biāo)簽格式,這里配置了context和p兩個(gè)命名空間。例如,配了context空間,就可以使用</context:XXX>這樣的標(biāo)簽。
2、自動(dòng)掃描組件
<context:component-scan base-package="com.lcl"></context:component-scan>
這個(gè)配置可以讓spring框架自動(dòng)掃描代碼中用@component注解了的類,自動(dòng)創(chuàng)建這些類的對(duì)象。
最后注意一下bean.xml要放在代碼目錄下,其目的是為了將bean.xml添加到classpath中。
編寫代碼
首先,寫一個(gè)Person類作為bean類。所謂bean類,簡單來說就是所有成員變量都有g(shù)etter和setter方法,并且有無參構(gòu)造方法的類。然后用了@Component(“person”)注解將Person類標(biāo)注為一個(gè)組件,這樣,就可以使用@Resource將Person的一個(gè)實(shí)例注入給其他對(duì)象的成員里,或者使用Application類的getBean(Class)方法得到一個(gè)實(shí)例。
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的實(shí)例,我們是用spring的依賴注入來管理成員變量person的創(chuàng)建,為了達(dá)到這個(gè)目的,只需要將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();
}
}
總結(jié)
創(chuàng)建一個(gè)spring項(xiàng)目只要三步:
① 加入依賴
② 編寫bean類
③ 編寫bean.xml
其中編寫bean類時(shí)用到了@Component、@Resource這兩個(gè)注解
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
解決IDEA報(bào)錯(cuò)Failed?to?start?bean‘documentationPluginsBootstra
這篇文章主要介紹了解決IDEA報(bào)錯(cuò)Failed?to?start?bean‘documentationPluginsBootstrapper‘問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Can''t use Subversion command line client:svn 報(bào)錯(cuò)處理
這篇文章主要介紹了Can't use Subversion command line client:svn 報(bào)錯(cuò)處理的相關(guān)資料,需要的朋友可以參考下2016-09-09
java?數(shù)組實(shí)現(xiàn)學(xué)生成績統(tǒng)計(jì)教程
這篇文章主要介紹了java?數(shù)組實(shí)現(xiàn)學(xué)生成績統(tǒng)計(jì)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java解析調(diào)用webservice服務(wù)的返回XML串詳解
這篇文章主要介紹了Java解析調(diào)用webservice服務(wù)的返回XML串詳解的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Spring Boot項(xiàng)目@RestController使用重定向redirect方式
這篇文章主要介紹了Spring Boot項(xiàng)目@RestController使用重定向redirect方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
淺談spring的重試機(jī)制無效@Retryable@EnableRetry
這篇文章主要介紹了淺談spring的重試機(jī)制無效@Retryable@EnableRetry,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
java中進(jìn)制的轉(zhuǎn)換,Byte與16進(jìn)制的轉(zhuǎn)換方法
下面小編就為大家?guī)硪黄猨ava中進(jìn)制的轉(zhuǎn)換,Byte與16進(jìn)制的轉(zhuǎn)換方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11

