不能用注解的
applicationContext.xml
1. druid连接池dataSource
( jdbc.properties通过context:property-placeholder: )
2. sqlSessionFactory 工厂bean
( dataSource和mapperLocations[.xml文件] 和 typeAliasesPackage[定义实体类所在的package 每个实体类会自动注册一个别名=类名])
3. mapperScannerConfigurer 将dao实现存入工厂管理
( basePackage放dao接口所在的包 sqlSessionFactoryBeanName把上面的sqlSessionFactory 工厂放进来 )
4. 事务管理器 tx
( 把连接池dataSource放进来 )
<!--druid连接池-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--生产sqlSessionFactory 工厂bean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations"> <!-- 把所有的mapper(.xml)也放进来 -->
<list>
<value>classpath:com/zzt/dao/*.xml</value>
</list>
</property>
<property name="typeAliasesPackage" value="com.zzt.entity"></property>
</bean>
<!--将dao实现存入工厂管理-->
<bean id="mapperScannerConfigurer9" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--dao接口所在的包-->
<property name="basePackage" value="com.zzt.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--事务管理器-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
用注解的
@Service 声明此类是service类
@Transactional 进行事务控制 可以加级别 传播行为等等
@Resource 根据名称自动注入
@Autowired 根据类型自动注入
@Qualifier(“xxx”) 与Autowired搭配使用 有多个同类型是可以挑选bean id=xxx的
@value(“xxx”) 对普通类型(String/Integer等)直接注入xxx值(hello/10)
@Aspect
声明此类是一个切面类 包含切入点pointcut 和 通知advice
@Component
声明组件 进入工厂(用法跟@Service类似)
@Pointcut (“execution(* 类.方法(参数))”)
public void pc(){}
切入点(某个方法如pc())
@Before(“pc()”)
public void myBefore(JoinPoint a){xxxxxxx}
前置方法 (对切入点pc()前置执行这个方法)
@AfterReturning(value = “pc()” , returning=“ret”)
public void myAfter(JoinPoint a,Object ret){xxxxxx}
后置方法 (对切入点pc()后置执行这个方法)
@Around(“pc()”) 环绕方法
public Object MyInterceptor(ProceedingJoinPoint p) throws Throwable {
System.out.println("interceptor1~~~~");
Object ret = p.proceed();
System.out.println("interceptor2~~~~");
return ret;
}
@AfterThrowing(value = “pc()”,throwing = “ex”)
public void myThrows(JoinPoint jp,Exception ex){}
异常方法
最后 一定要注意的是这几段要记得写
<!--注解告知 注解在哪些地方种-->
<context:component-scan base-package="com.zzt"></context:component-scan>
<!--事务管理器 @Transaction-->
<tx:annotation-driven transaction-manager="tx"/>
<!--启用aop切面注解-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>