今日用jar包
笔记
***面试 Spring的两大特性(对spring的理解):
* IOC 和 AOP :
* IOC 控制反转 最主要作用用来解除类和类之间的关系耦合:解除的耦合方式用了4个,1.配置,2. 容器.3.反射4.依赖注入DI(采用get/set方法给属性赋值或者构造器给属性赋值)
* AOP 面向切面编程,为了解除业务逻辑耦合的,例如日志业务逻辑和数据业务逻辑,AOP的实现原理是基于代理的。代理分为静态代理和动态代理(jdk动态代理,cglib代理)
* spring在内部实现方式:在被代理类里看接口,有的话选择jdk,没有就cglib
* spring类似一个管家,后面所有内容全部spring管理,MVC的V他不管,其余和Java相关的几乎全管,和页面展示的基本不管。
* Spring 一个无侵入式的轻量级开源框架,主要思想是解除程序中的耦合:IOC解除类和类之间的关系耦合,AOP解除了业务逻辑耦合。
* 为什么用spring?
* 使现有技术更加易用,推进编码最佳实践
* 轻量级框架(轻量级:1.性能开销问题 2.框架的大小就是jar包的多少 3.使用的难易程度 * spring中所有的行为都是由于spring管理类,core解除的是类之间的耦合,AOP解除的方法级别的耦合
* spring也有自己的持久层框架,只不过不太好,都不用
* ORM 给hibernate和mybatis来提供支持
* spring还提供了DAO模板可自动生成
* spring还是MVC框架
* IOC 控制反转 Inversion Of Control
* 高层模块不应该依赖于低层模块 eg:Action必须使用DAO,DAO必须有DBManager。高层模块里不能new出来对象
* 实现必须依赖抽象,而不是抽象依赖于实现(能用接口接收对象的地方,尽量使用接口,能用接口声明的地方,尽量用接口,灵活性高)
* 依赖注入 Dependency Injection DI : 是IOC的核心实现原理 用set方法给累的属性赋值
* 保留抽象接口,让组件依赖于抽象接口,当组件要与其他实际的对象发生依赖关系时,由抽象接口来注入依赖
* spring环境
* 导jar包(spring4.2.9)
* 配置文件推荐写 applicationContext.xml 直接放src下,此文件名字可改 spring.xml
* 一个spring可以对应多个xml,配置文件往往是根据业务逻辑来划分的
* 跟xml和根标签之间有<!>这个的是dtd,根标签里写内容的统一称为schema
* 以后想要什么东西就在spring中配置什么东西
* 注入种类 :
* Type2 就是set/get方法
* Type1 有侵入式不用
* Type3 就是构造器进行属性的注入
* Type2 Type3无侵入式
* spring中的接口是为了提高抽象层次的,是一个变量接收的中介,都是可以直接实例化拿来直接使用的,所谓的组件可以进行实例化的不用new直接用
* 构造器constructor进行注入时,是从下标为0开始index="0"
* 构造器可以赋值,但最后以属性的赋值作为标准<property>
* 构造注入只有一个优点:时效性好,创建对象时把自己的成员变量给实化了
* 命名空间的引入和标签的引入差不多,不引就不能用,从今天开始注入全用Annotation
* 如果说要注入的组件只有一个,@Resource(name="")后面name可以不写,若有多个必须手动指定
* 使用Annotation注入使用到的就是零配置,使用到的属性零配置
* 想用注解管理类必须把当前包告诉spring
* 零配置:但凡采用Annotation配置方式取代xml配置方式的统一称为零配置
package com.anbo.vo;
public interface IEnginee {
public void start();
}
package com.anbo.vo;
import org.springframework.stereotype.Component;
@Component(value="enginee")
public class Enginee implements IEnginee {
public void start(){
//
System.out.println("0.8发动机启动");
}
}
package com.anbo.vo;
import org.springframework.stereotype.Component;
@Component(value="engineet")
public class EngineeT implements IEnginee {
public void start(){
System.out.println("W28发动机启动...");
}
}
package com.anbo.vo;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
/*
* 下面注释的都是get/set方式和构造器方式的,都不用,以后都是注解方式
*/
@Component(value="car")
public class Car{
public Car(){}
/*
* 重载构造器
* 使用构造函数来进行注入
*
*/
/*public Car(IEnginee en){
this.en=en;
}*/
// @Resource(name="engineet")//把配置文件的对象enginee赋给en
@Resource(name="enginee")//这里如果组件只有一个可以不写(name="")如果有多个必须写
private IEnginee en;//null
/*public IEnginee getEn() {
return en;
}
public void setEn(IEnginee en) {
this.en = en;
}*/
public void run(){
//null.start();//空指针异常...
en.start();
System.out.println("小轿车开车....");
}
}
package com.anbo.vo;
import java.util.ArrayList;
import java.util.List;
public class Student {
private String stuname;
private List<String> cources = new ArrayList<>();
public List<String> getCources() {
return cources;
}
public void setCources(List<String> cources) {
this.cources = cources;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
}
package com.anbo.vo;
public class User {
private String username;
private String userpass;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<!-- 注解的话:1.先引入命名空间context aop的, -->
<context:annotation-config/>
<!-- 要想让注解管理类必须把当前类所在的包跟spring说一声 -->
<!-- 让spring启用注解管理类 -->
<context:component-scan base-package="com.anbo.vo"></context:component-scan>
<!-- 在spring的容器中声明了一个java.util.Date类型的对象,名字叫做date,spring会自动对这个对象进行初始化 -->
<!-- Annotation注入第一步就使用到了零配置,这地方用到的属性零配置 -->
<!-- Date date = new Date(); -->
<bean id="date" class="java.util.Date"></bean>
<!-- 让spring管理所有组件
<bean id="car" class="com.anbo.vo.Car"> -->
<!-- 注入 -->
<!-- ref == references 引用 -->
<!-- <constructor-arg index="0" ref="enginee"></constructor-arg> -->
<!-- 构造器可以赋值,构造器和属性都存在时,属性作为标准 -->
<!-- <property name="en" ref="enginee"></property>
</bean>-->
<!-- <bean id="enginee" class="com.anbo.vo.Enginee"></bean>
<bean id="engineet" class="com.anbo.vo.EngineeT"></bean> -->
<!-- 下面这种配置方式很罕见 -->
<bean id="student" class="com.anbo.vo.Student">
<property name="stuname" value="zhangsan"></property>
<property name="cources">
<list>
<value>数学</value>
<value>语文</value>
<value>英语</value>
</list>
</property>
</bean>
<bean id="student1" class="com.anbo.vo.Student" p:stuname="王五">
</bean>
<!-- 让spring管理.properties配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:a.properties</value>
</list>
</property>
</bean>
<bean id="user" class="com.anbo.vo.User">
<property name="username" value="${username}"></property>
<property name="userpass" value="${userpass}"></property>
</bean>
</beans>
package com.anbo.test;
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.anbo.vo.Car;
import com.anbo.vo.Student;
import com.anbo.vo.User;
public class Test {
public static void main(String[] args) {
//spring的核心就是一个context容器
//要想用spring就必须先用容器
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
System.out.println(context);
Date date = (Date) context.getBean("date");
System.out.println(date);
Car car = (Car) context.getBean("car");
car.run();
Student stu = (Student) context.getBean("student");
System.out.println(stu.getStuname());
for(String cource : stu.getCources()){
System.out.println(cource);
}
Student student1 = (Student) context.getBean("student1");
System.out.println(student1.getStuname());
User user = (User) context.getBean("user");
System.out.println(user.getUsername());
System.out.println(user.getUserpass());
}
}