eclipse+Spring最新----------持续更新----------- ##========day 01======== ####1.简介
- 1.
applicationContext容器特点 : 容器创建时容器中所有注册的对象都会创建. - 2. Spring 的作用 : 是对象的容器,帮助我们管理项目中的所有对象.
- 3.导包 :
4+2 : 4个核心包,2个日志包 ####2.约束引入(beans) - 1.引入对应xsd : window -> Preference -> XML Catalog -> add -> fileSystem -> schema -> beans -> 最新.xsd; 勾选 Key type : Schema location; copy Location路径最后一段到Key最后
- 2.无脑引入xsi : applicationContext.xml -> 书写beans -> design -> 右键 -> Edit Namespaces -> xsi**
- 3.引入对应的Edit Namespaces : applicationContext.xml -> 书写beans -> design -> 右键 -> Edit Namespaces**
<beans>
<bean name="user" class="cn.xjw.test.User"></bean>
<!--
id : 给bean 起个名字,不可重复(早起属性)
name : 给bean 起个名字,可重复,可使用特殊字符(后来属性)
class : 类的完整类名
生命周期属性
init-method : 初始化方法
destroy-method : 销毁方法
作用范围
scope :
singleton(默认值) : 单例,创建容器时会立即创建单例对象
prototype : 多例,每次获取对象时才会创建对象,并且每次都会创建新的对象
-->
</beans>
public void fun1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u = (User) ac.getBean("user");
u.setName("tony");
u.setAge(17);
System.out.println(u);
}
####4.IOC 和 DI
- 1.IOC(控制反转) : 从我们自己创建对象反转到对象的创建权交给 Spring
- 2.DI(依赖注入) : 将必须的属性注入到对象中,是实现IOC思想的必须条件
####5.BeanFactory(过时)
- 1.最底层(原始)的接口,功能比较单一.特点 : 每次在获取对象的时候才会创建对象
- 2.ApplicationContext,特点 : 容器创建时,容器内注册的所有对象都会被创建 //ClassPathXmlApplicationContext : 类路径 //FileSystemXmlApplicationContext : 硬盘绝对路径
- 3.在WEB环境开发时使用ApplicationContext,在资源匮乏的时候可以使用BeanFactory ####6.Bean 元素
<beans>
<bean name="" class=""></bean>
<!--
id : 给bean 起个名字,不可重复(早起属性)
name : 给bean 起个名字,可重复,可使用特殊字符(后来属性)
class : 类的完整类名
生命周期属性
init-method : 初始化方法
destroy-method : 销毁方法
作用范围
scope :
singleton(默认值) : 单例,创建容器时会立即创建单例对象
prototype : 多例,每次获取对象时才会创建对象,并且每次都会创建新的对象
-->
</beans>
####7.对象创建三种方式
<!-- 1.空参构造 -->
<bean name="user" class="cn.xjw.beans.User" scope="singleton"></bean>
<!-- 2.静态工厂 -->
<bean name="user1" class="cn.xjw.create.UserFactory" factory-method="createUser"></bean>
<!-- 3.实例工厂 -->
<bean name="user2" factory-bean="userFactory" factory-method="createUser1" init-method="init" destroy-method="destroy"></bean>
<bean name="userFactory" class="cn.xjw.create.UserFactory"></bean>
public class UserFactory {
public static User createUser() {
return new User();
}
public User createUser1() {
return new User();
}
}
- scope属性
1.singleton : 大部分使用默认(singleton)
2.prototype : status2 与 spring 整合时,action 声明为多例(prototype). status2 每次请求都会创建一个新的 action.
3.request :
4.session :
- 初始化销毁方法
1.初始化 : init-method
2.销毁 : destroy-method
3.如果想要对象走销毁方法,保证:a.对象是单例.b.调用ClassPathXmlApplicationContext的close方法.
- 模块化配置
<!-- applicationContext 引入其他位置的配置文件 -->
<import resource="applicationContext.xml"/>
- 属性注入(set方式)
<bean name="user" class="cn.xjw.a_beans.User" init-method="init" destroy-method="destroy">
<!-- 值类型注入 -->
<property name="name" value="tony"></property>
<property name="age" value="17"></property>
<!-- 引用类型注入 -->
<property name="car" ref="car"></property>
</bean>
<bean name="car" class="cn.xjw.a_beans.Car">
<property name="name" value="北汽威旺"></property>
<property name="color" value="水银色"></property>
</bean>
class User{
private String name;
private Integer age;
}
class Car{
private String name;
private String color;
}
- 属性注入(构造函数)
<bean name="user1" class="cn.xjw.a_beans.User">
<constructor-arg name="name" value="999" index="0" type="java.lang.Integer"></constructor-arg>
<constructor-arg name="car" ref="car" index="1"></constructor-arg>
</bean>
- 属性注入(p名称空间 和 SPEL表达式)
<bean name="user2" class="cn.xjw.a_beans.User" p:name="tony" p:age="18" p:car-ref="car"></bean>
- 复杂类型注入
<bean name="user" class="cn.xjw.test.User"></bean>
<!-- Array -->
<property name="arr">
<array>
<value>tony</value>
<value>sid</value>
<ref bean="user" />
</array>
</property>
<!-- List -->
<property name="list">
<list>
<value>tony</value>
<value>sid</value>
<ref bean="user" />
</list>
</property>
<!-- Map -->
<property name="map">
<map>
<entry key="url" value="https://www.baidu.com"></entry>
<entry key="u" value-ref="user"></entry>
<entry key-ref="user1" value-ref="user2"></entry>
</map>
</property>
<!-- Properties -->
<property>
<props>
<prop key="driverClass">com.jdbc.mysql.Driver</prop>
<prop key="userName">root</prop>
<prop key="password">1234</prop>
</props>
</property>
- WEB项目中应用spring容器
1.导包 4+2+1
web.xml中配置listener ->ContextLoadListener
3.配置参数,指定Spring配置路径 -> ConfigLocation
4.Action中使用 WebApplicationContextUtils.getWebApplicationContext(ServletContext sc) 获取Spring容器
========day 02 : Spring 中使用注解替换 xml 中的配置========
####1.步骤
- 导包 : 4 +2 +aop
- 约束 : 引入
context约束 - 配置扫描注解 :
<context:component-scan
base-package="cn.fo.spring_day02.bean">
</context:component-scan>
####2.类上添加相关注解
- @Component : 组件
- @Service : 业务层
- @Controller : WEB层; MVC C层
- @Repository : 持久层; 数据库
以上注解没有实质性区别,只是取名不同,用于在代码中区分不同模块
- @Scope : 指定类的作用范围
singleton:单例(默认值)prototype:多例(与Struts2整合时要使用 : Struts2特性 每次请求都新创建一个 Action)####3.属性注入(在属性上和set方法上都可以) - @Value : 用于注入普通类型
- @Autowired : 根据类型自动装配
- @Qualifier : 当使用
Autowired有多个Component时使用该属性指定名称 来确认最终使用的那个 - @Resource : 相当于
Autowired和Qualifier同时使用 ####4.生命周期 - @PostConstruct : ==
init-method - @PreDestroy : ==
destroy-method####5.Spring Aop的两种代理机制 - 扩展 : 之前使用aop写法
Proxy.newProxyInstance(ClassLoader loader, Interface[] faces, InvocationHandler handler)
- 1.JDK的动态代理 : 被代理对象必须实现接口,才能产生代理对象.如果没有接口将不能使用动态代理技术
- 2.Cglib的动态代理 : 第三方代理技术 Cglib代理,可以对任何类产生代理.原理 : 对目标对象继承代理.如果被代理的对象被
final修饰,则不能被 Cglib代理. ####6.编码实现JDK 动态代理 - 1.接口
public interface UserService{
void save();
void delect();
void update();
void select();
}
- 2.实现类 :
UserServiceImpl - 3.工厂类
public class UserServiceProxyFactory implements InvocationHandler {
public UserService getUserServiceProxy() {
UserService us = (UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(),
UserServiceImpl.class.getInterfaces(), this);
return us;
}
public UserServiceProxyFactory(UserService us) {
super();
this.us = us;
}
private UserService us;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开启事务.");
Object invoke = method.invoke(us, args);
System.out.println("关闭事务.");
return invoke;
}
}
- 4.测试
UserService usProxy = new UserServiceProxyFactory(new UserServiceImpl()).getUserServiceProxy();
System.out.println("==================");
usProxy.save();
System.out.println("==================");
usProxy.delete();
System.out.println("==================");
usProxy.update();
System.out.println("==================");
usProxy.select();
System.out.println("==================");
####7.编码实现Cglib代理 ####8.Spring中的动态代理 ####9.使用注解完成Spring中的动态代理