Spring基础(2)

来源:互联网 发布:sql promp8破解 编辑:程序博客网 时间:2024/06/03 14:12
Spring进阶
1. Spring的Bean管理(注解)
1. 代码中的特殊标记,使用注解可以完成功能
2. 注解的写法:@注解名称(属性名称=属性值)
3. 注解可以使用在类、属性、方法上
4. Spring注解开发
1.准备
(1)导入Jar包
(a)导入Spring的基础Jar包
(b)导入AOP的Jar包
Spring-aop-4.2.4.RELEASE.jar
(2)创建类、创建方法
(3)创建Spring配置文件,引入约束
需要引入Beans和Spring-context约束
(4)在配置文件中开启注解扫描
// 到包中扫描类、方法、属性上是否有注解
<context:component-scan base-package="">
// 只会扫描属性上的注解
<context:annotation-config></context:annotation-config>
2. 使用注解创建对象
(1)在创建对象的类上面使用注解实现
@Component(value="user") //类似<Bean id class> value的值相当于ID的值
public class User{}
(2)创建对象有四个注解
Spring中提供了@Component的三个衍生注解
(a)@Controller :WEB层
(b)@Service:业务层
(c)@Repository:持久层
(3)创建对象是单实例还是多实例
@Component(value="user") //类似<Bean id class> value的值相当于ID的值
@Scope(value="prototype")
public class User{}

3. 使用注解注入属性
代码:
@Component(value="userDao")
public class UserDao{
public void add(){}
}

@Service(value="userService")
public class UserService{
// 不需要Set方法
@AutoWired
private UserDao userDao;
public void add(){}
}
注解属性的两种方式:
(1)@AutoWired
(2)@Resource(name="userDao")// name属性值写注解创建Dao对象Value值

3. XML和注解方式混合使用
1. 创建对象操作使用配置文件方式实现
2. 注入属性的操作使用注解的方式实现

2. AOP
1. AOP概述
(1)面向切面编程
(2)采用横向抽取机制,取代了传统纵向继承体系重复性代码
2. AOP底层原理


3. AOP操作相关术语
(1)Pointcut(切入点):指我们要对哪些Joinpoint进行拦截的定义
(2)Advice(通知/增强):指拦截到Joinpoint之后所要做的事情就是通知,通知分为前置通知、后置通知、异常通知、最终通知、环绕通知(切面要完成的功能)
(3)Aspect(切面):指切入点和通知的结合

3. Spring的AOP操作(基于AspectJ的XML方式)
1. 在Spring中进行AOP操作,使用AspectJ实现
(1)AspectJ不是Spring的一部分,和Spring一起使用进行AOP操作
(2)Spring2.0以后新增了对AspectJ的支持
2. 使用AspectJ实现AOP的两种方式
(1)基于AspectJ的XML配置
(2)基于AspectJ的注解方式
3. 使用表达式配置切入点
切入点:实际增强的方法
常用的表达式:
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(a)execution(* cn.spring.aop.Book.add(..))
(b)execution(* cn.spring.aop.Book.*(..))
(c)execution(* *.*(..))
(d)匹配所有save开头的的方法execution(*save*(..))
4. 代码
(1)导入相关的Jar
aopalliance-1.0.jar
aspectjweaver-1.8.7.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
(2)创建Spring的核心配置文件,导入AOP约束
配置对象
配置aop操作
<aop:config>
<!-- 配置切入点(用表达式的方式指定对哪个方法进行增强) -->
<aop:pointcut expression="execution(* cn.spring.aop.Book.*(..))" id="pointcut1"/>
<!-- 配置切面(把增强用到方法上)-->
<aop:aspect ref="myBook">
<!-- 增强类型 
method:增强类里使用哪个方法作为前置增强
-->
<aop:before method="before1" pointcut-ref="pointcut1"/>
<aop:after-returning method="after1" pointcut-ref="pointcut1"/>
<aop:around method="around1" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
4. Log4J介绍
1. 通过log4j可以看到程序运行过程中更详细的信息
经常使用log4j查看日志
2. 使用
(1)导入log4j的Jar包
(2)复制log4j的配置文件,复制到src下
(3)设置日志级别:
(a) info :看到基本信息
(b)debug:看到详细信息

5. Spring整合Web项目
1. 问题
Action调用Service,Service调用Dao,会有性能问题
2. 解决方案:
(1)在服务器启动时,创建对象加载配置文件
(2)底层使用监听器、ServletContext对象
3. Spring中已经进行封装
(1)封装了监听器,只需要配置即可,需要导入Jar包:
spring-web-4.2.4.RELEASE.jar
(2)配置监听器
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
(3)指定加载Spring配置文件的位置(web.xml)
<!-- 指定Spring配置文件的位置 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-web.xml</param-value>
    </context-param>
原创粉丝点击