这是黑马视频的学习笔记
使用注解方式管理bean
注解:
代码里面的特殊标记,使用这些标记可以完成一些对应的功能,能简化操作
注解写法:@注解名(属性=属性值)
注解可以用在代码的类,属性,方法上面
使用注解管理spring
1. 准备工作: 首先要导入jar包支持注解,就是aop的jar包。然后创建类和方法。最后在spring核心配置文件中引入相关约束,开启扫描注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "
default-autowire="byName">
<!--开启注解扫描 常用 属性值里面是实体类对象的包名-->
<context:component-scan base-package="cn.itcast.anno"></context:component-scan>
<!--只扫描属性上面的注解 很少用 -->
<context:annotation-config></context:annotation-config>
2.。 注解方式创建对象
package cn.itcast.anno;
import org.springframework.stereotype.Component;
@Component(value="user")
public class User {
public void add() {
System.out.println("add.......");
}
}
有4个用在类上创建对象的注解,其实4个注解功能现在都一样,通用。还有一个@Scope注解,配置是单实例还是多实例。属性值有prototype等
@Component
@Controller :web层
@Service :业务层
@Repository ; 持久层
3. 注解方式注入属性
有@autoWired注解和@Resource注解两种方式,第二种用的多
第一种
package cn.itcast.anno;
import org.springframework.stereotype.Component;
@Component(value="userDao")
public class UserDao {
public void add() {
System.out.println("dao........");
}
}
package cn.itcast.anno;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component(value="userService")
public class UserService {
@Autowired
private UserDao userDao;
public void add() {
System.out.println("service......");
userDao.add();
}
第二种:
package cn.itcast.anno;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component(value="userService")
public class UserService {
@Resource(name="userDao")
private UserDao userDao;
public void add() {
System.out.println("service......");
userDao.add();
}
}
4.配置文件和注解同时使用,一般是使用配置文件创建对象,使用注解注入属性
AOP的概念和原理
1 aop:面向切面(方面)编程,扩展功能不修改源代码实现
2 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码
3 aop底层使用动态代理实现
(1)第一种情况,有接口情况,使用动态代理创建接口实现类代理对象
(2)第二种情况,没有接口情况,使用动态代理创建类的子类代理对象
AOP操作术语
Joinpoint(连接点): 类里面可以被增强的方法,这些方法称为连接点
Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义.
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
Aspect(切面): 是切入点和通知(引介)的结合
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
Target(目标对象):代理的目标对象(要增强的类)
Weaving(织入):是把增强应用到目标的过程.
把advice 应用到 target的过程
Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类
spring的aop操作
1.在spring框架里进行Aop操作,使用AspectJ实现
(1)AspectJ不是spring的一部分,只是和spring一起进行aop操作
(2)spring2.0以后增加了对AspectJ的支持
2.使用AspectJ实现aop有两种方式
(1)基于aspectj的xml配置
(2)基于aspectj的注解方式
Aop操作准备
1 除了导入基本的jar包之外,还需要导入4个aop相关的jar包
2使用配置文件进行aop
创建spring核心配置文件,导入aop的约束
3.创建增强用的类和被增强的类与方法
package cn.itcast.aop;
import org.junit.Test;
public class Book {
@Test
public void add() {
System.out.println("add............");
}
}
增强用的类
package cn.itcast.aop;
public class MyBook {
public void before1() {
System.out.println("前置增强。。。。。。。。。。");
}
}
4.使用表达式配置切入点
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
常用的表达式:
- execution(* com.spring.aop.Book.add(..)),定义了一个访问修饰符任意,在com.spring.aop包下Book类add方法的切面;
- execution(* com.spring.aop.Book.*add(..)),定义了访问修饰符任意,在com.spring.aop包下Book类的任选方法的切面;
- execution(* .(..)),定义了访问修饰符任意,在任意包下任意类任意方法的切面;
- execution(* save.*(..)),定义了所有save开头的方法的切面;
配置文件的代码
<!-- 第一步 配置对象 -->
<bean id="book" class="com.spring.aop.Book"></bean>
<bean id="bookService" class="com.spring.aop.BookService"></bean>
<!-- 第二步 配置AOP操作 -->
<aop:config>
<!-- 2.1 配置切入点 -->
<aop:pointcut expression="execution(* com.spring.aop.Book.*(..))" id="pointcut1"/>
<!-- 2.2 配置切面:把通知用到方法上 -->
<aop:aspect ref="bookService">
<!-- 配置增强类型
method:增强类里面使用哪个方法作为前置 -->
<aop:before method="before" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
2.4 AOP的其他操作
2.4.1 修改增强类BookService
需要注意的是环绕方法,需要引入一个ProceedingJoinPoint对象!
package com.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class BookService {
public void before1(){
System.out.println("bookService before....");
}
public void after1(){
System.out.println("bookService after.....");
}
public void around1(ProceedingJoinPoint jp) throws Throwable{
System.out.println("around before..");
//执行被增强的方法
jp.proceed();
System.out.println("around after..");
}
}
2.4.2 修改配置文件aopBean.xml
<!-- 第一步 配置对象 -->
<bean id="book" class="com.spring.aop.Book"></bean>
<bean id="bookService" class="com.spring.aop.BookService"></bean>
<!-- 第二步 配置AOP操作 -->
<aop:config>
<!-- 2.1 配置切入点 -->
<aop:pointcut expression="execution(* com.spring.aop.Book.*(..))" id="pointcut1"/>
<!-- 2.2 配置切面:把通知用到方法上 -->
<aop:aspect ref="bookService">
<!-- 配置增强类型
method:增强类里面使用哪个方法作为前置 -->
<aop:before method="before1" pointcut-ref="pointcut1"/>
<!-- 后置方法 -->
<aop:after method="after1" pointcut-ref="pointcut1"/>
<!-- 环绕通知方法 -->
<aop:around method="around1" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
log4j介绍
使用log4j可以得到更详细的日志信息。要导入log4j的jar包,复制配置文件到src下。
可以设置日志级别
spring整合web项目演示
action调用service,service调用dao。
要导入spring整合web项目的spring-web的jar包
1.创建3个类
action类
package cn.itcast.action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionSupport;
import cn.itcast.service.UserService;
public class UserAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("action......");
ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml");
UserService userService=(UserService) context.getBean("userService");
userService.add();
return NONE;
}
}
service类
package cn.itcast.service;
import cn.itcast.dao.UserDao;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System.out.println("service.........");
userDao.add();
}
}
dao类
package cn.itcast.dao;
public class UserDao {
public void add() {
System.out.println("dao.........");
}
}
2.3个配置文件代码
spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "
default-autowire="byName">
<bean id="userService" class="cn.itcast.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="cn.itcast.dao.UserDao"/>
</beans>
struts的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="hello" extends="struts-default" namespace="/" >
<action name="userAction" class="cn.itcast.action.UserAction"/>
</package>
</struts>
web.xml里面配置。框架里面已经封装了监听器,要在web.xml中配置就行
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--指定spring配置文件位置 如果不指定,监听器只会加载web-inf下的application.xml文件,找不到就报错 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean2.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>