目录
2.依赖注入(Dependency Injection,简称DI)
1.Spring
1.1 介绍
Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweight container)。
1.2 传统代码弊端
总结:使用一个new一个对象,耦合度高
准确:1.如果类中的属性通过new的方式直接绑定,则类与属性的耦合性高
2.如果需要修改属性类型,则需要修改代码,后期扩展不方便
1.3面向接口编程
总结:好处:将两个对象的共同方法抽取到接口中,实现代码的解耦
弊端:根源问题仍然没有解决,属性与类仍然绑定
准确:使用接口开发,可以在一定程度上降低类与对象的耦合性,但是属性与类绑定的事实并没 有解决
修改后:
1.4.IOC
1.4.1 IOC 介绍
IOC全称Inversion of Control,即“控制反转”,这是一种设计思想。对象创建的权利由Spring框架完成,由容器管理对象的生命周期。
IOC理论提出的观点大体是:借助于“第三方”实现具有依赖关系的对象之间的解耦
详细内容在:http://IOC的理解
测试
(1)定义Student类
public class student {
public void grade(){
System.out.println("成绩合格!");
}
}
(2)在resource的目录下创建spring.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">
<!--将Dog对象交给Spring容器管理
1.属性id是bean的唯一标识符. 一般类名首字母小写
2.属性class 表示被管理的类
-->
<bean id="student" class="cn.tedu.test4.student"></bean>
<!--<bean id="cat" class="com.jt.demo2.Cat"></bean>-->
</beans>
(3)编辑测试类
public class test {
public static void main(String[] args) {
String resource="spring.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(resource);
student student=applicationContext.getBean(student.class);
student.grade();
}
(4)结果
成绩合格
1.4.2 spring容器说明
解释: Spring容器的数据结构是Map集合,Map<key,value>,
key是bean中id值(id的首字母都是小写), value是通过反射机制实例化的对象.
数据结构:Map<key,value> 即:Map<student,student对象>
public class test {
public static void main(String[] args) {
// String resource="spring.xml";
// ApplicationContext applicationContext=new ClassPathXmlApplicationContext(resource);
// student student=applicationContext.getBean(student.class);
// student.grade();
getStudent();
}
public static void getStudent(){
try {
student student=(student) Class.forName("cn.tedu.test4.student").newInstance();
student.grade();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
结果:与上面结果一样,即spring容器是通过反射机制创建对象
1.5 spring注解开发
1.5.1 配置类
注解1:@Configuration 标识这个一个配置类
注解2:@ComponentScan("路径") 注解包扫描,使得注解有效
注解3:@Bean 标识该方法的返回值交给spring容器管理
@Configuration
@ComponentScan("cn.tedu.test4")
public class Config {
/*@Bean注解:将User对象交给Spring容器管理
key: id就是方法名 user
value: 就是返回值类型*/
@Bean
public student student(){
return new student();
}
}
1.6 单例/多例
1.6.1 说明
单例模式:Spring容器中管理对象,在内存中只有一份
多例模式:Spring容器中管理对象,在内存中有多份
1.6.2 测试
属性值:
注解4:@Scope 控制单例和多例
@Scope("singleton") 默认值 单例模式
@Scope("prototype") 多例模式
@Bean
// @Scope("singleton") //单例
@Scope("prototype") //多例
public student student(){
return new student();
}
public static void main(String[] args) {
ApplicationContext context=new AnnotationConfigApplicationContext(Config.class);
student student=context.getBean(student.class);
student student1=context.getBean(student.class);
System.out.println(student==student1); //单例模式 true;多例模式 false
}
1.7 懒加载机制
注解5:@Lazy
关于多例模式和懒加载说明:
@Lazy 只能控制单例模式,多例模式都是懒加载
1.8 Spring生命周期
阶段:对象创建 初始化 业务调用 对象销毁
注解6:@PostConstruct
注解7:@PreDestroy
@Bean
public Person person(){return new Person;}
在配置类中通过@Bean将Person交给容器管理
或者直接在Person类中加注解:@Component 交给Spring容器管理
@Component
public class Person {
public Person() {
System.out.println("我出生了");
}
@PostConstruct
public void init(){
System.out.println("我长大了");
}
public void hello(){
System.out.println("赚钱");
}
@PreDestroy
public void destroy(){
System.out.println("老了");
}
}
测试类:
接口中没有提供close的方法,需要使用实现类进行操作<