Spring入门学习day01
一.spring 介绍,以及spring 框架的优势和缺点
spring是J2EE应用程序框架,是轻量级的IoC和AOP的容器框架,主要是针对javaBean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts框架,ibatis框架等组合使用。(后期项目维护修改代码量比较少)
优势:
1:使用IOC容器更加容易组合对象之间关系,面向接口编程,降低耦合。其 实spring 就是一个大工厂模式(生产管理实体bean,不用实例化对象, 所有的都是通过spring容器来进行管理)
2:支持分布式事务(原子性,一致性,隔离性,持久性)
3:支持测试,与Junit测试有良好的结合;
4:与其他框架的结合度比较好(ssh,ssm)
5:支持我们企业级的api的开发(邮箱,任务调度)【超级重要】
缺点:业务功能依赖spring特有的功能,依赖与spring环境。
二. spring 各个模块的介绍
Spring core:最基础,提供IOC和依赖注入。管理bean与bean之间的依赖Spring Context:上下文容器,beanFactory功能加强的一个自接口
Spring WEB:WEB应用开发的支持
Spring MVC:针对web应用MVC思想实现
Spring DAO:提供了JDBC的抽象层,简化了编码,同时使之更健壮
Spring ORM:与流行的ORM框架的整合Spring AOP:面向切面,提供与AOP联盟兼容的编成实现。
搭建spring框架的步骤:
1:导入核心的jar包
2:导入核心配置文件(applicationContext.xml)
3:测试
Spring核心技术
IOC(控制反转)、AOP(面向切面)。
控制反转可以将传统的实例化方式交给ioc进行实例化,从而提高应用程序的响应效率。
(就是不需要自己来实例化这个对象,而依赖我们容器,也就是spring框架)
IOC按实现方法不同,可以分为依赖注入DI和依赖查找两种
spring容器的使用
从本质上讲,BeanFactory和ApplicationContext仅仅只是一个维护Bean定义以及相互依赖关系的高级工厂接口而已,通过BeanFactory和
ApplicationContext我们可以访问bean定义。
首先在容器配置文件applicationcontext.xml中添加Bean定义
然后在创建BeanFactory和ApplicationContext容器对象后,调用getBean()方法获取Bean的实例即可
方法使用示例:getBean(“唯一的标识符”)
1.IOC容器创建的2种方式
//首先在applicationContext.xml配置文件中写
<bean id="boy" class="com.offcn.entity.Boy"/>
//创建方式一:
//Spring把所有的配置文件都当做资源
ClassPathResource resource = new ClassPathResource("applicationContext.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
beanFactory.getBean("user");
//创建方式二:
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
ac.getBean("boy");//这里AppicationContext是BeanFactory下的接口
三. IOC容器创建对象(Bean实例化)
2.1 实现接口实例化对象
2.2通过工厂实例化对象(静态,非静态)
<!-- .通过工厂类创建对象 -->
public class Factory {//工厂类
public static String getStaticInstance(){//静态方法
return new String("factory_static");
}
public String getInstance(){ //非静态方法
return new String("factory_no_static");
}
}
<!-- a.工厂类静态方法 创建对象 -->
<bean id="user3" class="d_create_object.Factory" factory-method="getStaticInstance"></bean>
<!-- b.工厂类非静态方法创建对象 -->
<bean id="factory" class="d_create_object.Factory"></bean>
<bean id="user4" factory-bean="factory" factory-method="getInstance"></bean>
四.依赖注入
依赖注入方式:手动装配 和 自动装配
手动装配:一般进行配置信息都采用手动
基于xml装配:构造方法、setter方法
构造方法:
public class User {
private Integer uid;
private String username;
private Integer age;
public User(Integer uid, String username) {
super();
this.uid = uid;
this.username = username;
}
public User(String username, Integer age) {
super();
this.username = username;
this.age = age;
}
---------------------
spring配置
<!-- 构造方法注入
* <constructor-arg> 用于配置构造方法一个参数argument
name :参数的名称
value:设置普通数据
ref:引用数据,一般是另一个bean id值
index :参数的索引号,从0开始 。如果只有索引,匹配到了多个构造方法时,默认使用第一个。
type :确定参数类型
例如:使用名称name
<constructor-arg name="username" value="jack"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
例如2:【类型type 和 索引 index】
<constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
<constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>
-->
<bean id="userId" class="com.itheima.f_xml.a_constructor.User" >
<constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
<constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>
</bean>
---------------------
setter方法
<!-- setter方法注入
* 普通数据
<property name="" value="值">
等效
<property name="">
<value>值
* 引用数据
<property name="" ref="另一个bean">
等效
<property name="">
<ref bean="另一个bean"/>
-->
<bean id="personId" class="com.itheima.f_xml.b_setter.Person">
<property name="pname" value="阳志"></property>
<property name="age">
<value>1234</value>
</property>
<property name="homeAddr" ref="homeAddrId"></property>
<property name="companyAddr">
<ref bean="companyAddrId"/>
</property>
</bean>
<bean id="homeAddrId" class="com.itheima.f_xml.b_setter.Address">
<property name="addr" value="阜南"></property>
<property name="tel" value="911"></property>
</bean>
<bean id="companyAddrId" class="com.itheima.f_xml.b_setter.Address">
<property name="addr" value="北京八宝山"></property>
<property name="tel" value="120"></property>
</bean>
---------------------
基于注解装配:
注解:就是一个类,使用@注解名称
开发中:使用注解 取代 xml配置文件。
[email protected]取代
@Component(“id”) 取代
2.web开发,提供3个@Component注解衍生注解(功能一样)取代
@Repository :dao层
@Service:service层
@Controller:web层
3.依赖注入,给私有字段设值,也可以给setter方法设值
普通值:@Value(" ")
引用值:
方式1:按照【类型】注入
@Autowired
方式2:按照【名称】注入1
@Autowired
@Qualifier(“名称”)
方式3:按照【名称】注入2
@Resource(“名称”)
4.生命周期
初始化:@PostConstruct
销毁:@PreDestroy
5.作用域
@Scope(“prototype”) 多例
注解使用前提,添加命名空间,让spring扫描含有注解类
<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"
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.xsd">
<!-- 组件扫描,扫描含有注解的类 -->
<context:component-scan base-package="com.itheima.g_annotation.a_ioc"></context:component-scan>
</beans>
---------------------