Spring基础知识:
控制反转 (IOC):
Spring通过一种称作控制反转的技术促进了松耦合。即 一个对象依赖的其他对象会通过被动的方式传递进来,而不是自己主动创建或者查找依赖对象。
面向切面编程(AOP):
在不改变软件原有功能的情况下,为软件扩展横向功能。AOP机制可以让开发者把业务流程中的通用功能(如登录,注册等)抽取出来,单独编写功能代码,在业务流程过程中,Spring框架会根据业务流程要求,自动把独立编写的功能模块代码切入到流程的合适位置。
Spring框架创建Bean对象的三种方式:
- 调用构造器创建Bean
- 调用静态工厂方法创建Bean
- 调用实例工厂方法创建Bean
构造器创建Bean示例如下:
package constructor;
/**
实体类
*/
public class Person {
private String username;
{
System.out.println("使用构造器创建Bean对象");
}
public Person(String username){
this.username=username;
}
public void hello(){
System.out.println("调用普通方法对"+username+"说你好");
}
}
配置文件:beanConstructor.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:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"
default-autowire="byName"
>
<!-- 指定class属性,通过构造器创建Bean实例 class:需要创建的Bean实例-->
<bean id="person" class="constructor.Person">
<constructor-arg name="username" value="博弈"></constructor-arg>
</bean>
</beans>
package constructor;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
测试类
*/
public class TestBean {
public static void main(String[] args) {
//1初始化容器对象
AbstractApplicationContext aac = new ClassPathXmlApplicationContext("beanConstructor.xml");
//2创建bean实例
Person ps = aac.getBean("person",Person.class);
//3调用bean实例方法
ps.hello();
//4关闭容器对象
aac.close();
}
}
静态工厂创建Bean示例如下:
//定义接口 统一接收类型
public interface People {
public void hello();
}
//具体实现类 Chinese
public class Chinese implements People{
private String nation;
public void setNation(String nation) {
this.nation = nation;
}
public void hello(){
System.out.println("我是"+nation+"人");
}
}
//具体实现类 American
public class American implements People{
private String nation;
public void setNation(String nation){
this.nation = nation;
}
public void hello() {
System.out.println("我是"+nation+"人");
}
}
//定义静态工厂,编写实例方法
public class PeopleFactory {
public static People getPeople(String arg){
if(arg.equalsIgnoreCase("chinese")){
return new Chinese();
}
else if(arg.equalsIgnoreCase("american")){
return new American();
}
else{
return new People(){
@Override
public void hello() {
System.out.println("世界向你问好");
}
};
}
}
}
配置文件:beanStaticFactory.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:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"
default-autowire="byName"
>
<!-- 创建chinese Bean 由PeopleFactory工厂的getPeople()方法进行创建 class:静态工厂实现类 factory-method:静态工厂实现类中具体的创建方法-->
<bean id="chinese" class="constructor.PeopleFactory" factory-method="getPeople">
<!-- 为静态工厂的创建方法传入参数 -->
<constructor-arg value="chinese"></constructor-arg>
<property name="nation" value="中国"></property>
</bean>
<bean id="american" class="constructor.PeopleFactory" factory-method="getPeople">
<constructor-arg value="american"></constructor-arg>
<property name="nation" value="美国"></property>
</bean>
</beans>
测试类:
//1初始化容器对象 (容器对象可以只初始化一个,此处编写为方便分开书写)
AbstractApplicationContext aacC = new ClassPathXmlApplicationContext("beanStaticFactory.xml");
//2创建bean实例
People ppC = aacC.getBean("chinese",Chinese.class);
//3调用bean实例方法
ppC.hello();
//4关闭容器对象
aacC.close();
//1初始化容器对象
AbstractApplicationContext aacA = new ClassPathXmlApplicationContext("beanStaticFactory.xml");
//2创建bean实例
People ppA = aacA.getBean("american",American.class);
//3调用bean实例方法
ppA.hello();
//4关闭容器对象
aacA.close();
实例工厂创建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"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"
default-autowire="byName"
>
<!-- 配置工厂Bean class:指定工厂的实现类 通过该Bean实例进行其他Bean对象的创建 -->
<bean id="peopleInstanceFactory" class="constructor.PeopleInstanceFactory" />
<bean id="chinese" factory-bean="peopleInstanceFactory" factory-method="getPeople">
<constructor-arg value="chinese"></constructor-arg>
<property name="nation" value="中国ren"></property>
</bean>
<bean id="american" factory-bean="peopleInstanceFactory" factory-method="getPeople">
<constructor-arg value="american"></constructor-arg>
<property name="nation" value="美国ren"></property>
</bean>
</beans>