您的位置:首页 > 编程语言 > Java开发

从java bean配置初识spring 实现最简单的Helloworld

2016-06-03 16:11 555 查看
刚接触spring的朋友总觉得spring很高大上,是的,spring是很高大上,但是这个高大上值得是spring集成了很多的高大上功能,并不是指的能使用spring很高大上,相反,spring正是为了简化开发而出现的框架,那么,我们就从最简单的bean配置来初识spring。

新建java工程,新建congfig源文件夹,注意,一定是源文件夹,否则后续代码运行可能会出错。

新建包:com.cslc.com,先把完整的代码运行起来再解释。

包下面新建实体类:HelloWorld.java

package www.cslc.com;

public class HelloWorld {

private  String  name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "HelloWorld [name=" + name + "]";
}
public  void sayHello(){
System.out.println("hello "+name);
}
}


早config下新建配置文件 applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 
<bean id="helloWorld" class="www.cslc.com.HelloWorld">
<property name="name" value="World"></property>
</beans>
</beans>


新建Main.java测试一下:

package www.cslc.com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext ctx =new  ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld  helloWorld =ctx.getBean(HelloWorld.class);
helloWorld.sayHello();
}
}


需要的jar:

spring-aop-4.0.0.RELEASE.jar

spring-beans-4.0.0.RELEASE.jar

spring-context-4.0.0.RELEASE.jar

spring-core-4.0.0.RELEASE.jar

spring-expression-4.0.0.RELEASE.jar

commons-logging-1.1.1.jar

以上应该是最简单的spring程序,只用spring管理一个HelloWorld的bean

其中主要的是配置文件的

<bean id="helloWorld" class="www.cslc.com.HelloWorld">
<property name="name" value="World"></property>
</beans>


代表配置了一个HelloWorld的bean, java中有两种方法可以获取这个对象

ctx.getBean(HelloWorld.class); 这种方式的好处是不用强制类型转化,但是当配置文件中配置了两个不同HelloWorld的时候,就会出现异常

ctx.getBean(“helloWorld”); 这种方式是最常用的方式,需要进行强制类型转化

在配置文件中配置bean有多种形式,但是当我们不用标签的时候,需要保证bean中有默认的无参的构造器,否则也会出现异常。

一般情况下,spring中的bean配置分为两种,其中最常用的是上面的配置形式:

<bean id="helloWorld" class="www.cslc.com.HelloWorld">
<property name="name" value="World"></property>
</beans>


用property 指定属性的值,一般指定的都是对象,并不是某一个属性的值,类似于:

<bean id ="person"  class="www.cslc.com.Person">
<property name="helloWorld"  ref="helloWorld"></property>
</bean>


第二种配置的方式就是构造器配置,但是很少用:

<bean id="car" class="www.cslc.com.Car">
<constructor-arg   value="Audi"></constructor-arg>
<constructor-arg   value="china"></constructor-arg>
<constructor-arg   value="300000"></constructor-arg>
</bean>


如果构造器中不指定index的话就会按个匹配,每一个参数对应构造器中的参数可以根据index 或者 type来区分。

简单的说,spring是一个容器,一般都说spring是一个轻量级的容器,这个轻或重不是指的是本省的代码量级,而是指的对程序的“侵入”多少,spring可以说做的基本完美,采用模块化的设计,使得程序可以按需加载,当我们需要个对象的时候,直接从容器中取就可以,而不用关心对象的创建销毁。

spring同样可以以轻的方式来管理实务、日志、数据源、融合的第三方框架等等,实现程序的解耦,简化开发工作。

spring整体采用IOC的方式,IOC值的是控制反转,概念很生涩,但是通俗的解释就是把对象实例化的控制权,由程序控制转为spring容器控制。 IOC是一种思想,在spring中通过DI技术实现。DI中文解释是依赖注入,原理和意思和IOC基本相同。

ICO/DI是spring的核心思想之一,另一个就是AOP,AOP的中文意思是面向切面编程,是spring能简化程序开发的一个重要原因。 通俗的讲,在程序开发中有很多与业务无关的代码,但是这些代码还是必要的,会出现在很多的地方,有人会有抽象出一个方法来引用,但是这种引用出现的次数有时候也是很大数量级别的,像日志,假设有一天你的老板不想用log4j的时候,传统方式需要改的代码将让人崩溃,而spring提供了面向切面编程这种优雅的方式解决这个问题。这些配置在配置文件中,在程序运行的过程中,spring会很优雅的将代码织入到连接点上,实现完整的业务。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring helloworld