spring的基本使用
最近在学习javaweb中的ssm框架,今天就整理一下spring的基本用法,一方面是做个笔记整理,以后可以用来复习用,一方面也希望能分享给更多的小伙伴。以后会陆续做其他几个框架的整理,并渐渐的由浅入深的去研究这些框架。
一、配置方式使用
1、导包
2、在src目录下添加applicationContext.xml配置文件
3、添加约束
添加约束后的applicationContext.xml的头如下:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
4、在配置文件中注入对象
public class User {
private String name;
private Integer age;
private Car car;
}
public class Car {
private String carName;
private String color;
}
<!-- 将User对象交给spring容器管理 -->
<bean name="user" class="cn.znh.domain.User">
<property name="name" value="小明" />
<property name="age" value="18" />
<property name="car" ref="car" />
</bean>
<!-- 将Car对象注入到spring容器管理 -->
<bean name="car" class="cn.znh.domain.Car">
<property name="carName" value="宝马" />
<property name="color" value="red" />
</bean>
5、在代码中获取对象
方式一(基本用法):
@Test
public void fun1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ac.getBean("user");
System.out.print(user);
}
方式二(在项目中的用法,一个项目只需要一个spring容器就行了,在web.xml中配置);
<!-- 在这个listener里创建spring,使得spring随着项目的启动而创建,随项目的关闭而销毁,从而保证了一个项目中只有一个spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定applicationContext.xml的文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
//使用
ServletContext sc = getServletContext();
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
User user = (User) ac.getBean("user");
System.out.print(user);
}
二、注解方式使用
1、需要导入一个aop包,引入一个context约束,如果已经导入过就不用了
2、在applicationContext.xml配置文件中添加扫描注解路径,扫描时包含指定扫描路径下的子包
<!-- 扫描cn.znh.domain包下的注解 -->
<context:component-scan base-package="cn.znh.domain" />
3、在代码中使用注解
//四个注解功能完全一样,下面三个具有分层意义
@Component("user")
// @Service("user")//业务层
// @Controller("user")//web层
// @Repository("user")//持久层
@Scope(scopeName = "prototype")// 多例 (默认是单例的)
public class User {
@Value(value="萧红")
private String name;
@Value(23 + "")
private Integer age;
@Resource(name="car")
private Car car;
}
@Component("car")
public class Car {
private String carName;
private String color;
}
注解后,user和car不需要在配置文件中配置,就可以直接从spring中获取对象了。
三、Spring与Junit整合测试
1、目的:在测试类中不需要在每个测试方法中获取spring容器,可以通过注解直接获取spring容器中的对象
2、需要导入一个test包,如果已经导入了就不需要了
3、在测试类中使用
@RunWith(SpringJUnit4ClassRunner.class)//junit创建spring容器
@ContextConfiguration("classpath:applicationContext.xml")//指定spring配置文件的位置
public class TestUt {
@Resource(name = "user1")
private User user1;
@Test
public void fun2() {
// 可以不用写了,直接打印user1就行
// ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// User user = (User) ac.getBean("user");
System.out.print(user1);
}
}
四、spring中的aop
1、一些名词解释
Joinpoint(连接点):目标对象中,所有可以增强的方法
Pointcut(切入点):目标对象,已经增强的方法
Advice(通知/增强):增强的代码
Target(目标对象):被代理对象
Weaving(织入):将通知应用到切入点的过程
Proxy(代理):将通知织入到目标对象之后,生产的代理对象
Aspect(切面):切入点+通知
2、编写目标对象(被代理对象)
/**
* 目标对象(被代理对象)
* @author Administrator
*/
public class UserServiceImp implements IUserService {
@Override
public void save() {
System.out.println("保存用户");
int i=1/0;
}
@Override
public void add() {
System.out.println("添加用户");
}
@Override
public void delete() {
System.out.println("删除用户");
}
@Override
public void find() {
System.out.println("查找用户");
}
}
3、编写通知类(增强代码)
/**
* @author Administrator
* 通知(增强)书写增强的代码,一共5种
*/
public class Advice {
public void before() {
System.out.println("前置通知");
}
public void afterReturning() {
System.out.println("后置通知,出现异常就不会调用了");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕通知前半部分");
Object pObject = pjp.proceed();
System.out.println("环绕通知后半部分");
return pObject;
}
public void afterException() {
System.out.println("出现异常了(异常拦截通知)");
}
public void after() {
System.out.println("后置通知,出现异常也会调用");
}
}
4、导入aop约束并在applicationContext.xml中配置织入
<!-- 准备目标对象 -->
<bean name="userService" class="cn.znh.service.UserServiceImp" />
<!-- 准备通知(增强)对象 -->
<bean name="advice" class="cn.znh.aspect.Advice" />
<!-- aop配置织入 -->
<aop:config>
<!-- 配置切点 public void cn.znh.service.UserServiceImp.save() *cn.znh.service.UserServiceImp.*(..)//返回值任意、方法名任意、参数任意 -->
<aop:pointcut expression="execution(* cn.znh.service.*ServiceImp.*(..))" id="pc" />
<!--织入通知 -->
<aop:aspect ref="advice">
<aop:before method="before" pointcut-ref="pc" />
<aop:after-returning method="afterReturning" pointcut-ref="pc" />
<aop:around method="around" pointcut-ref="pc" />
<aop:after-throwing method="afterException" pointcut-ref="pc" />
<aop:after method="after" pointcut-ref="pc" />
</aop:aspect>
</aop:config>