Java Spring开发环境搭建及简单入门示例教程
作者:目尽地平线 时间:2021-08-17 07:36:28
本文实例讲述了Java Spring开发环境搭建及简单入门示例。分享给大家供大家参考,具体如下:
前言
虽然之前用过Spring,但是今天试着去搭建依然遇到了困难,而且上网找教程,很多写的是在web里使用Spring MVC的示例,官方文档里的getting start一开始就讲原理去了(可能打开的方法不对)。没办法,好不容易实验成功了,记下来免得自己以后麻烦。
添加依赖包
进入spring官网,切换到projects下点击 spring framework.官网上写的是以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最核心的功能,如依赖注入,切面等。
创建spring配置文件
新建一个名为bean.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"
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的语法,配置当前xml文件中的标签格式,这里配置了context和p两个命名空间。例如,配了context空间,就可以使用</context:XXX>这样的标签。
2、自动扫描组件
<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类标注为一个组件,这样,就可以使用@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的创建,为了达到这个目的,只需要将person变量用@Resource注解标注即可。
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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java中的线程池
![](https://img.aspxhome.com/file/2023/9/59369_0s.png)
java isInterrupted()判断线程的实例讲解
SpringCloud Eureka搭建的方法步骤
![](https://img.aspxhome.com/file/2023/1/61241_0s.jpg)
SpringBoot整合Mybatis实现多数据源配置与跨数据源事务实例
如何使用两个栈实现队列Java
java控制台输出图书馆管理系统
![](https://img.aspxhome.com/file/2023/4/61284_0s.jpg)
一文带你真正理解Java中的内部类
![](https://img.aspxhome.com/file/2023/3/60073_0s.png)
关于activemq安装配置以及启动错误的解决
![](https://img.aspxhome.com/file/2023/5/59485_0s.png)
SpringCloud搭建netflix-eureka微服务集群的过程详解
![](https://img.aspxhome.com/file/2023/3/58293_0s.jpg)
Spring JDBCTemplate原理及使用实例
![](https://img.aspxhome.com/file/2023/4/62134_0s.png)
Java超全面梳理内部类的使用
![](https://img.aspxhome.com/file/2023/3/62433_0s.png)
java模拟实现双向链表
![](https://img.aspxhome.com/file/2023/2/62072_0s.jpg)
idea mybatis配置log4j打印sql语句的示例
Java8新特性之类型注解_动力节点Java学院整理
java之路径分隔符介绍
spring中的FactoryBean代码示例
Java利用MultipartFile实现上传多份文件的代码
SpringMVC框架post提交数据库出现乱码解决方案
![](https://img.aspxhome.com/file/2023/1/61421_0s.png)
Spring Cache框架应用介绍
![](https://img.aspxhome.com/file/2023/2/57492_0s.png)
详解Java目录操作与文件操作教程
![](https://img.aspxhome.com/file/2023/7/58887_0s.png)