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

spring初级入门一(Spring介绍+启动Spring容器+Spring容器创建对象)

2017-07-10 15:56 555 查看
一、Spring介绍

1、概念

开源

分层

轻量级

一站式(full-stack)

简化企业级开发框架

2、特点

(1)简化开发

spring对一些常见API(比如JDBC)做封装,使用这些封装后的代码会大大简化

(如:使用springjdbc来访问数据库,就不用考虑如何获取连接和关闭连接)

(2)管理对象

spring管理对象之间的依赖关系,维护起来方便

(3)集成其他框架

spring将一些框架进行集成,方便使用框架

(如:spring集成mybatis,这样mybatis操作起来简单方便)

3、特征

(1)控制反转IoC:依赖注入

(2)面向切面AOP:分离业务逻辑与系统级服务

(3)容器:管理对象的配置和生命周期

二、Spring容器

1、概念

Spring容器---是Spring框架中的一个核心模块

用来管理对象

2、Spring容器类型有2种

(1)BeanFactory

(2)ApplicationContext:ApplicationContext继承BeanFactory接口,拥有更多企业级方法,推荐使用(本例子采用这种类型)

3、启动Spring容器

步骤:

(1)导包:spring-webmvc 3.2.8.RELEASE(这里随便选的版本)



(2)添加配置文件:applicationContext.xml(应该放置在java Resources项目资源文件中)

<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

</beans>


位置:



(3)启动Spring容器:java代码中编写

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FirstSpring {

public static void main(String[] args) {
/*
* 此行代码负责启动spring容器
* a. ApplicationContext:是继承BeanFactory一个接口
* b. ClassPathXmlApplicationContext是 ApplicationContext接口的一个实现类(该类依据类路径去查找配置文件)
*/
//加载工程classpath下的配置文件(即applicationContext.xml)实例化
 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}

3、Spring容器创建对象(bean的实例化)

(1)无参构造器

创建一个无参构造器的java类(如:Student.java)

package first;

public class Student {

//默认构造器
public Student() {
System.out.println("打桩Student()");
}

}
配置applicatinContext.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!--
1、无参构造器创建对象
id属性:要求唯一。
class属性:需要一个默认的空构造器,要求写类的全限定名。
-->
<bean id="stu1" class="first.Student"></bean>

</beans>
创建一个java实例化验证(使用getBean方法获取)

package first;

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

public class FirstSpring {

public static void main(String[] args) {
//启动容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
/*
* 此行代码的作用:通过容器获得要创建的对象(注意,getBean方法的第一个参数是bean的id)。
* Student.class:是Student在方法区中对应的class对象。
*/
Student stu1 =ac.getBean("stu1",Student.class);
System.out.println("无参构造器创建对象:"+stu1);
}

}


(2)静态工厂方法

配置applicatinContext.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframew 4000
ork.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!--
2、静态工厂方法创建对象
factory-method属性:指定创建bean实例的静态工厂方法。
spring容器会调用该方法来创建对象。
-->
<bean id="cal1" class="java.util.Calendar" factory-method="getInstance"></bean>
</beans>
创建一个java实例化验证(使用getBean方法获取)
package first;

import java.util.Calendar;

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

public class FirstSpring {

public static void main(String[] args) {
//启动容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//getBean方法获取
Calendar cal1 =ac.getBean("cal1",Calendar.class);
System.out.println("静态工厂方法创建对象:"+cal1);
}

}


(3)实例工厂方法

配置applicatinContext.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<!-- 静态工厂方法配合下面实例工厂方法创建对象 -->
<bean id="cal1" class="java.util.Calendar" factory-method="getInstance"></bean>
<!--
3、实例工厂方法创建对象
factory-bean属性:指定要调用的bean的id。
spring容器会调用该bean的方法来创建对象。
-->
<bean id="date1" factory-bean="cal1" factory-method="getTime"></bean>
<bean id="date2" class="java.util.Date"></bean>
</beans>
创建一个java实例化验证(使用getBean方法获取)

package first;

import java.util.Date;
import java.util.Calendar;

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

public class FirstSpring {

public static void main(String[] args) {
//启动容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//getBean方法

//第一种
// Date date1 =ac.getBean("date1",Date.class);
// System.out.println(date1);

//第二种
Date date2 = ac.getBean("date2", Date.class);
System.out.println(date2);
}
}
Maven管理结构

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring spring容器