Spring是javaee体系中比较重要的框架,作为一个java程序员,应该很好的掌握它。那么Spring到底是一个什么东西呢?(春天到了,夏天是不是也快来了呢?当然不是!)
- spring是一个轻量级java框架,使用它你可以不实现它提供的API;
- spring能和其他的第三方框架很好的嵌套使用;
- spring的DI/IOC(依赖注入),反转资源的获取方向。IOC是一个创建Bean并管理Bean的一个容器,使用Spring,能更好的管理JaveBean。以前使用一个对象时,我们需要new一个,而使用spring,我们只需要告诉它资源获取方式,它就会主动把对象推送给我们;
- spring的AOP(面向切面编程),主要是用来写日志,事务及权限管理。
- -
我使用的IDE是Eclipse,使用Spring需下载相关插件,Eclipse内就可下载,相关教程参阅:http://www.cnblogs.com/wenruo/p/6283024.html
导入jar包
使用Spring需要导入相应的jar包,我是从maven官网上下载下来的相关依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
配置xml文件
生成Spring Bean Configuration File
加入相应命名空间:打开xml文件后,点击下方的Namespaces,增加相应命名空间(每个命名空间的含义,以后再详述)
创建JavaBean
Car:`package test.newtest;
/**
* @Tittle:Car
* @author Administrator
* @date 2017年9月26日下午2:50:33
*/
public class Car {
private int no;
private String name;
public Car() {
super();
// TODO Auto-generated constructor stub
}
public Car(int no, String name) {
super();
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Car [no=" + no + ", name=" + name + "]";
}
}`
XML文件中配置Bean
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!--通过依赖注入的方式配置Bean,依赖注入的方式有:
- 构造函数注入;
- setter注入;
- 静态工厂方法注入;
- 实例工厂方法注入。-->
<!-- 通过配置car 采用p标签是为了节省命名空间-->
<bean name="car" class="test.newtest.Car" p:no="1" p:name="BWM">
</bean>
<!-- 通过构造函数配置Car(用index来指定构造函数的顺序) -->
<bean name="car2" class="test.newtest.Car">
<constructor-arg index="1" value="fsss"></constructor-arg>
<constructor-arg index="0" value="1"></constructor-arg>
</bean>
<!-- 通过构造函数配置Car(用name来指定构造函数) -->
**<!-- 当通过构造函数配置Bean时,既没有指定Index也没有指定Name时,spring会按照类的构造函数顺序进行注入 -->**
<bean name="car3" class="test.newtest.Car">
<constructor-arg name="name" value="fsss"></constructor-arg>
<constructor-arg name="no" value="1"></constructor-arg>
</bean>
<!-- 通过setter配置Car -->
<bean name="car4" class="test.newtest.Car">
<property name="name" value="sjfs"></property>
<property name="no" value="2"></property>
</bean>
</beans>
获取Bean
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
/*使用Spring的ApplicationContext来解析配置文件,其实现类有:ClassPathXmlApplicationContext(加载ClassPath下的XML文件)和FileSystemXmlApplicationContext(加载系统文件)*/
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-rel.xml");
//通过Bean的name获取:
Car car =(Car) ctx.getBean("car4");
/*通过Bean的类型获取:
Car car =ctx.getBean(Car.class);
*/
System.out.println(car.toString());
}
}