Spring 简介
Spring 是一个开源框架,为了解决企业级应用开发的复杂性而创建的。
为了降低java开发的复杂性,Spring采取以下4种关键策略:
1,基于POJO的轻量级和最小侵入式编程;
2,通过控制反转和依赖注入以及面向接口实现松耦合;
3,基于切面和惯例进行声明式编程;
4,通过切面和模板减少样板式代码。
环境准备
jar包
官网jar下载地址:https://repo.spring.io/libs-release-local/org/springframework/spring/
jar包说明
常用jar包说明
IOC概念
1,Ioc 控制反转:
是对对象控制权的转移,从程序代码本身转移到外部容器,把对象的创建,初始化,销毁工作都交给spring容器去做,由spring容器控制控制生命周期。
2,DI依赖注入:
指程序运行过程中,需调用另一个对象协助时,不用在代码里直接创建被调用者,而是依赖外部容器,由外部容器创建后传递给程序。它是目前最优秀的解耦方式。
3,Ioc和DI的关系:
IoC是一个概念,是一种思想,其实现方式多种多样。当前比较流行的实现方式之一 是DI。
XML配置方式
一,普通构造注入
jar包引入
1,创建Spring工程。
2,创建Spring的xml配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
3,在Spring的配置文件声明User 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 在容器中注册UserBean -->
<bean class="com.dpb.javabean.UserBean" id="userBean"></bean>
</beans>
所谓的声明,就是将自己的信息·告诉给Spring容器,例如id,class,Spring容器跟据class通过反射(默认使用无参构造方法)创建一个名为userBean的User对象。
4,初始化容器
public static void main(String[] args) {
//初始化Spring容器,当Spring容器初始化时,会自动加载配置文件,然后根据配置文件中的内容初始化Bean
ApplicationContext ac =
new ClassPathXmlApplicationContext("application.xml");
}
5,初始化Spring容器之后,可以看到User已经创建出来。
注意:上面这种初始化操作User需要一个无参构造。
6,通过调用容器中的getBean方法获取Spring容器中的对象
public static void main(String[] args) {
//初始化Spring容器,当Spring容器初始化时,会自动加载配置文件,然后根据配置文件中的内容初始化Bean
ApplicationContext ac =
new ClassPathXmlApplicationContext("application.xml");
// 去Spring容器中获取一个UserBean对象
UserBean user = ac.getBean("userBean", UserBean.class);
System.out.println(user);
}
7,也可以通过类型获取一个Bean的实例。
public static void main(String[] args) {
//初始化Spring容器,当Spring容器初始化时,会自动加载配置文件,然后根据配置文件中的内容初始化Bean
ApplicationContext ac =
new ClassPathXmlApplicationContext("application.xml");
// 去Spring容器中获取一个UserBean对象 通过类型直接获取
UserBean user = ac.getBean( UserBean.class);
System.out.println(user);
}
这种方式有潜在的隐患:如果Spring容器中有多个User的实例,此时就会报错
二,静态工厂注入
创建静态工厂类
package com.sxt.userfactory;
import com.sxt.bean.User;
/**
* 静态工厂注入
* @author Administrator
*
*/
public class UserFactory {
public static User getInstance(){
return new User();
}
}
在application.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 class="com.sxt.userfactory.UserFactory" factory-method="getInstance" id="user"></bean>
</beans>
获取相应的实列
三,动态工厂注入
创建动态工厂类
package com.sxt.factory;
import com.sxt.bean.User;
/**
* 动态工厂注入
* @author Administrator
*
*/
public class UserFactory {
public User getInstance(){
return new User();
}
}
在application.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 class="com.sxt.factory.UserFactory" id="UserFactory"/>
<bean factory-bean="UserFactory" factory-method="getInstance" id="user"/>
</beans>
获取实例
package com.sxt.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sxt.bean.User;
public class UserTest {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
User bean = (User) ac.getBean("user");
bean.run();
}
}