hello world spring ioc——第一个spring程序

认真对待学习

最近又重新阅读了spring官方文档 对里面的有用的高频的配进行记录和分享。

简介

控制反转(IoC)又被称作依赖注入(DI)。它是一个对象定义其依赖的过程,它的依赖也就是与它一起合作的其它对象,这个过程只能通过构造方法参数、工厂方法参数、或者被构造或从工厂方法返回后通过settter方法设置其属性来实现。然后容器在创建bean时注入这些依赖关系。这个过程本质上是反过来的,由bean本身控制实例化,或者直接通过类的结构或Service定位器模式定位它自己的依赖,因此得其名曰控制反转。

容器

spring 中的org.springframework.context.ApplicationContext容器接口,在独立的应用中通常创建ClassPathXmlApplicationContext或者FileSystemXmlApplicationContext的实例,它们实现了ApplicationContext接口。一般的应用场景中不会现行的创建spring容器,如web环境。。。。但是再练习中最简单的就是使用简单的Java应用来学习,也可以帮助以后对配置的理解。

元数据

  元数据一般是通过xml的方式来配置,spring2.5以后的版本支持使用注解的方式,spring3.0之后的版本支持java接口的配置,但是这两种配置方式都违反代码与配置分离的原则,同时xml的方式提供的配置更加多样化,所以我不太建议使用(不过使用注解的方式可以提高开发效率)。下文中的的例子中我们使用的都是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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

这是spring bean的最简单配置。id是该bean的唯一标识,class是这个bean的类,让我们搭建我们第一个helloworld。

jar

想使用spring ioc,首=首相要引入spring ioc的jar包

pom.xml

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>

 

建立元数据

public class Event {
    private Long id;

    private String title;
    private Date date;

    public Event() {
        // this form used by Hibernate
    }

    public Event(String title, Date date) {
        // for application use, to create new events
        this.title = title;
        this.date = date;
    }

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

 

配置xml 名称event.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="event" class="全写包名.Event">
        <!-- collaborators and configuration for this bean go here -->
    </bean>
    
  

    <!-- more bean definitions go here -->

</beans>

 

测试类

public class SpringTest {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring_bean_cfg.xml");
        Event bean = context.getBean(Event.class);
    
        System.out.println(bean);
    
        
    }

}

 

运行结果。

com.wanda.entity.Event@5c3cee0d

于是spring帮我们建立了一个Event对象。

这就是一个spring入门下一节 我们对里面的关键点分析一下。

posted @ 2017-07-27 21:30  啥都不会,也不着急  阅读(348)  评论(0编辑  收藏  举报