Spring框架

目录

 

jar包

最简单版本

spring+aop+ioc

前置后置增强

环绕增强

最终增强

异常增强

注解IOC的配置文件


jar包

最纯净jar包

最简单版本

配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
	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-3.2.xsd" xmlns:p="http://www.springframework.org/schema/p">

	<bean id="jie" class="com.bdqn.entity.Home" 
		p:address="hubie" p:family="JIE">
	</bean>
	
	<bean id="fm" class="com.bdqn.entity.Home" 
		p:address="guangdong" p:family="fumu">
	</bean>
	
	<bean id="my" class="com.bdqn.entity.Person" 
		p:name="jin">
		<property name="homes">
			<list>
				<ref bean="jie" />
				<ref bean="fm" />
			</list>
		</property>
		<property name="age">
			<value type="int">23</value>
		</property>
	</bean></beans>

日志文件log4j.properties

log4j.rootCategory=info,con  
  
log4j.appender.con=org.apache.log4j.ConsoleAppender  
log4j.appender.con.layout=org.apache.log4j.PatternLayout  
log4j.appender.con.layout.ConversionPattern=%d{MM-dd HH:mm:ss} [%p]%c%n-%m%n  
  

jar包链接

 

spring+aop+ioc

jar包:spring+aop+ioc

使用注解的aop和手动bean注入的aop只需加上几段配置文件

用来扫包的配置文件,严格来说是IOC注解@autowired@service...的

<context:component-scan base-package="com.aop,com.impl,com.interfaces" />

还要有一段是用来开启aop注解的,让spring识别注解aop

<!-- 支持注解Aspectj -->
	<aop:aspectj-autoproxy />

使用aop注解必须要使用@AspectJ方法实现以注解的方式定义切面,同时jdk版本必须在5.0以上

书上也说:如果项目采用jdk5.0以上版本,可以考虑使用@AspectJ注解方式,减少配置的工作量;

如果不愿意使用注解或项目采用的jdk版本较低而无法使用注解,则可以选择使用<aop:aspect>配合普通JavaBean的形式

还有一段是支持注解bean的

<!-- 支持注解配置bean -->
	<context:annotation-config />

配置文件applicationContext.xml

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		   http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
	<!-- 支持注解Aspectj -->
	<aop:aspectj-autoproxy />
	<!-- 支持注解配置bean -->
	<context:annotation-config />
 	<!--使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入base-package为需要扫描的包(含所有子包) -->
	<context:component-scan base-package="com.bdqn.dao,com.bdqn.service"/>
	
</beans>

 

 

前置后置增强

java代码不加注解

package com.bdqn.aop;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

public class AopUser {
	
	
	public void before(JoinPoint jp){
		Date date = new Date();
		String dd = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date);
		System.out.println("进入时间:"+dd);
	}
	
	public void after(JoinPoint jp,Object result){
		Date date = new Date();
		String dd = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date);
		System.out.println("结束时间:"+dd);
	}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
	
	<context:component-scan 
	base-package="com.bdqn.aop,com.bdqn.dao,com.bdqn.entity,com.bdqn.impl" />
	<bean class="com.bdqn.aop.AopUser"></bean>
	<aop:aspectj-autoproxy />
	<bean id="song" class="com.bdqn.aop.AopUser" >
	</bean>
	
	<bean id="user" class="com.bdqn.impl.UserDao" >
	</bean>
	<aop:config>
		<aop:pointcut expression="execution(public void add())" id="pointcut"/>
		<aop:aspect id="song" ref="song">
			<aop:before method="before" pointcut-ref="pointcut" />
			<aop:after-returning method="after" pointcut-ref="pointcut" returning="result"/>
		</aop:aspect>
	</aop:config>
	
	
	</beans>
	

java的代码+注解

@Pointcut("execution(public void add(..))")
	public void pointcut(){}
	@Before("pointcut")
	public void before(JoinPoint jp){
		Date date = new Date();
		String dd = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date);
		System.out.println("进入时间:"+dd);
	}
	@AfterReturning(pointcut = "pointcut()",returning = "returnValue")
	public void after(JoinPoint jp,Object result){
		Date date = new Date();
		String dd = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(date);
		System.out.println("结束时间:"+dd);
	}	

配套xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
	
	<context:component-scan 
	base-package="com.bdqn.aop,com.bdqn.dao,com.bdqn.entity,com.bdqn.impl" />
	<bean class="com.bdqn.aop.AopUser"></bean>
	<aop:aspectj-autoproxy />
	<bean id="song" class="com.bdqn.aop.AopUser" >
	</bean>
	
	<bean id="user" class="com.bdqn.impl.UserDao" >
	</bean>
	
	</beans>
	
	
	

环绕增强

配置文件不加注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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 ">


	<bean id="ib" class="com.bdqn.entity.IBuyComputerImpl" abstract="false"
		lazy-init="default" autowire="default">
	</bean>
	<bean id="yh" class="com.bdqn.aop.YouHui" abstract="false"
		lazy-init="default" autowire="default">
	</bean>

	<aop:config>
		<aop:pointcut expression="execution(public void buyComputer())"
			id="pointcut" />
		<aop:aspect ref="yh">
			<aop:around method="youHui" pointcut-ref="pointcut" />
		</aop:aspect>
	</aop:config>
</beans>

java代码

package com.bdqn.aop;

import org.aspectj.lang.ProceedingJoinPoint;


public class YouHui {
	public void youHui(ProceedingJoinPoint jp) throws Throwable{
		System.out.println("送一个鼠标");
		Object obj = jp.proceed();
		System.out.println("送一个键盘");
	}
}

配置文件加注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
	
	<context:component-scan 
	base-package="com.aop,com.impl,com.interfaces" />
	
	<aop:aspectj-autoproxy />

	<bean id="song1" class="com.aop.AopBuy"></bean>
	<bean id="buy" class="com.interfaces.BuyComputer"></bean>
	<!-- <aop:config>
		<aop:pointcut expression="execution(public void buy())"
			id="pointcut" />
		<aop:aspect ref="song1">
			<aop:around method="song" pointcut-ref="pointcut" />
		</aop:aspect>
	</aop:config> -->
	
	</beans>
	
	

java代码

package com.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

public class AopBuy {
	@Around("execution(public void buy())")
	public void song(ProceedingJoinPoint jp) throws Throwable{
		System.out.println("鼠标");
		Object obj = jp.proceed();
		System.out.println("键盘");
	}
}

最终增强

配置文件不加注释

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

	<aop:config>
		<aop:pointcut expression="execution(public void add(..))" id="pointcut"/>
		<aop:aspect ref="song1">
			<aop:after method="song" pointcut-ref="pointcut"/>
		</aop:aspect>
	
	
	</aop:config>
	
	
	
	</beans>
	
	
	
	

Java代码

package com.bdqn.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;

public class AopSong {
	
	public void song(JoinPoint jp){
		System.out.println("最终增强");
	}
}

配置文件+注释

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
	
	<!-- <context:component-scan base-package=""></context:component-scan> -->
	<aop:aspectj-autoproxy />
	<bean id="song1" class="com.bdqn.aop.AopSong"/>
	<!-- <bean id="user" class="com.bdqn.impl.UserDao"/> -->
	
	<!-- <aop:config>
		<aop:pointcut expression="execution(public void add(..))" id="pointcut"/>
		<aop:aspect ref="song1">
			<aop:after method="song" pointcut-ref="pointcut"/>
		</aop:aspect>
	
	
	</aop:config> -->
	
	
	
	</beans>
	

java的代码

package com.bdqn.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;

public class AopSong {
	@After("execution(public void add(..))")
	public void song(JoinPoint jp){
		System.out.println("最终增强");
	}
}

异常增强

配置文件不加注释

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
	
	<aop:config>
		<aop:pointcut expression="execution(public void add())" id="pointcut"/>
		<aop:aspect ref="exe">
			<aop:after-throwing method="e" pointcut-ref="pointcut" throwing="ex"/>
		</aop:aspect>
	</aop:config> 
	<aop:aspectj-autoproxy />
	</beans>
	
	
	

Java代码

package com.bdqn.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AopException {
	
	public void e(JoinPoint jp,RuntimeException ex){
		System.out.println("检测到异常,已经回滚");
	}
}

配置文件+注释

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
	
	<context:component-scan 
	base-package="com.bdqn.aop,com.bdqn.impl" />
	<bean id="user" class="com.bdqn.impl.UserDao"></bean>
	<bean id="exe" class="com.bdqn.aop.AopException"></bean>
	<!-- <aop:config>
		<aop:pointcut expression="execution(public void add())" id="pointcut"/>
		<aop:aspect ref="exe">
			<aop:after-throwing method="e" pointcut-ref="pointcut" throwing="ex"/>
		</aop:aspect>
	</aop:config> -->
	<aop:aspectj-autoproxy />
	</beans>
	
	
	

java代码

package com.bdqn.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AopException {
	@AfterThrowing(pointcut = "execution(public void add())",throwing="ex")
	public void e(JoinPoint jp,RuntimeException ex){
		System.out.println("检测到异常,已经回滚");
	}
}

注解IOC的配置文件

使用注解的配置信息启动spring容器

<context:component-scan base-package="com.aop,com.impl,com.interfaces" />

还有java自带的@resource这个注解使用时必须要有settergetter方法

使用IOC的注解

@Autowired

使用这个注解时,要注意后面可以设置它是否一开始就自动寻找依赖注入,如果为true,没有找到可以注入的bean会编译报错

属性required,还有一个@Qualifier(“”)这个配套@autowired使用可以直接声明注入beanID

@Repository:DAO层的注解,一般命名规范的话这个可以省略

@Service:service层的注解

@Controller:后面SpringMVC的控制层

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值