国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Java Spring開發環境搭建及簡單入門示例教程

Java Spring開發環境搭建及簡單入門示例教程

2021-02-03 11:24目盡地平線 JAVA教程

這篇文章主要介紹了Java Spring開發環境搭建及簡單入門示例,結合實例形式分析了spring環境搭建、配置、使用方法及相關注意事項,需要的朋友可以參考下

本文實例講述了Java Spring開發環境搭建及簡單入門示例。分享給大家供大家參考,具體如下:

前言

雖然之前用過Spring,但是今天試著去搭建依然遇到了困難,而且上網找教程,很多寫的是在web里使用Spring MVC的示例,官方文檔里的getting start一開始就講原理去了(可能打開的方法不對)。沒辦法,好不容易實驗成功了,記下來免得自己以后麻煩。

添加依賴包

進入spring官網,切換到projects下點擊 spring framework.官網上寫的是以maven依賴的形式寫的,所以可以新建一個maven項目,然后將下面的依賴加入到pom.xml里

?
1
2
3
4
5
6
7
<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最核心的功能,如依賴注入,切面等。

創建spring配置文件

新建一個名為bean.xml的配置文件,放到代碼目錄里,文件的內容如下:

?
1
2
3
4
5
6
7
8
9
10
11
<?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、命名空間

?
1
2
3
4
5
6
7
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">

這個是xml的語法,配置當前xml文件中的標簽格式,這里配置了context和p兩個命名空間。例如,配了context空間,就可以使用</context:XXX>這樣的標簽。

2、自動掃描組件

?
1
<context:component-scan base-package="com.lcl"></context:component-scan>

這個配置可以讓spring框架自動掃描代碼中用@component注解了的類,自動創建這些類的對象。

最后注意一下bean.xml要放在代碼目錄下,其目的是為了將bean.xml添加到classpath中。

編寫代碼

首先,寫一個Person類作為bean類。所謂bean類,簡單來說就是所有成員變量都有getter和setter方法,并且有無參構造方法的類。然后用了@Component(“person”)注解將Person類標注為一個組件,這樣,就可以使用@ResourcePerson的一個實例注入給其他對象的成員里,或者使用Application類的getBean(Class)方法得到一個實例。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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的創建,為了達到這個目的,只需要將person變量用@Resource注解標注即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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();
  }
}

總結

創建一個spring項目只要三步:
① 加入依賴
② 編寫bean類
③ 編寫bean.xml

其中編寫bean類時用到了@Component@Resource這兩個注解

希望本文所述對大家java程序設計有所幫助。

原文鏈接:http://blog.csdn.net/ruangong1203/article/details/48030623

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91av官网| 国产成人自拍视频在线 | 日韩成人在线网站 | 狠狠综合久久 | 欧美天天| 欧美精品一区二区三区蜜桃视频 | 成人免费在线视频播放 | 伊人草 | 成人综合视频在线 | 日韩欧美一区二区在线观看视频 | 男女中文字幕 | 97精品国产一区二区三区 | 国产精品99久久久久久动医院 | 亚洲一区在线观看视频 | 亚洲视频欧美视频 | 国产欧美一区二区三区在线看 | 91精品国产综合久久香蕉 | 久久成人精品 | 精品美女在线观看视频在线观看 | 国产一区二区免费 | 午夜小视频在线观看 | 青青草91在线视频 | 成人网av | av免费在线观看网站 | 一本大道av日日躁夜夜躁 | 天天精品 | 欧美一区二区三区在线视频 | 久久久精品国产亚洲 | 亚洲欧美精品 | 一卡二卡久久 | 中文字幕av网| 国产高清精品在线 | 国产资源在线观看 | 91电影在线| a国产精品| 久久中文字幕一区二区三区 | 欧美精品一区二区在线观看 | 97色在线视频 | 综合五月 | 国产中文一区 | 成人亚洲一区 |