<bean id="acountService" class="com.sise.service.impl.AcountServiceImpl"></bean>
<bean id="loggerAdvice" class="com.sise.utils.LoggerAdvice"></bean>
<aop:config>
<!--
aop:pointcut:放在外面-所有切面都可以引用,
放里面只能当前切面能用
由于约束问题,不能放再aspect结束标签后面-->
<aop:pointcut id="pt1" expression="execution( com.sise.service..*(..))"/>
<aop:aspect id="LogAdvice" ref="loggerAdvice">
<!-- <aop:before method="BeforePrintLog" pointcut-ref="pt1"></aop:before>-->
<!-- <aop:after-returning method="AfterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>-->
<!-- <aop:after-throwing method="AfterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>-->
<!-- <aop:after method="AfterPrintLog" pointcut-ref="pt1"></aop:after>-->
<aop:around method="AroundPrintLog" pointcut-ref="pt1"></aop:around>
</aop:aspect>
</aop:config>
/*
* 环绕通知----等价动态代理
* */
public Object AroundPrintLog(ProceedingJoinPoint pjp){
Object[] args = pjp.getArgs();//得到的是执行办法中的参数数组
Object rtValue=null;
try {
System.out.println("前置通知--");
rtValue = pjp.proceed(args);
System.out.println("后置通知--");
return rtValue;
} catch (Throwable throwable) {
System.out.println("异常通知--");
throw new RuntimeException(throwable);
}finally {
System.out.println("最终通知--");
}
}
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "bean.xml")这样就会出现,必须加classpath:说明再根路径下
//@ContextConfiguration(locations = "classpath:bean.xml")
@ContextConfiguration(classes = SrpingConfiguration.class)
public class acountTest {
@Autowired
private IAcountService ac;
@Test
public void findAll() {
// ClassPathXmlApplicationContext acs = new ClassPathXmlApplicationContext("bean.xml");
// IAcountService ac = (IAcountService) acs.getBean("acountService");
List<AcountBean> allAcount = ac.findAllAcount();
for (AcountBean acountBean : allAcount) {
System.out.println(acountBean);
}
}
<!-- 注解所需要的包<bean:component-scan base-package="com.sise"></bean:component-scan>-->
<!-- <bean:component-scan base-package="com.sise"></bean:component-scan>-->
<!-- 配置持节层dao-->
<bean id="acountDao" class="com.sise.dao.impl.AcountDaoImpl">
<property name="runner" ref="runner"></property>
<property name="connectionUtils" ref="connectionUtils"></property>
</bean>
<!-- 配置业务层service-->
<bean id="acountService" class="com.sise.service.impl.AcountServiceImpl">
<!-- property-> 配置的这个AcountServiceImpl实现类的属性-->
<property name="iAcountDao" ref="acountDao"></property>
</bean>
<bean id="connectionUtils" class="com.sise.utils.ConnectionUtils">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionmanager" class="com.sise.utils.TransactionManager">
<property name="connectionUtils" ref="connectionUtils"></property>
</bean>
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入数据源-->
<!-- 这个1语句:<constructor-arg name="ds" ref="dataSource"></constructor-arg>-->
<!-- 如果想线程和连接绑定,则需要再daoImpl里面的sql语句前面添加connectionUtils.getThreadConnection(),
return runner.query(connectionUtils.getThreadConnection(),"select * from mybatisacount"
, new BeanListHandler<AcountBean>(AcountBean.class));
而且要注掉这个1语句:<constructor-arg name="ds" ref="dataSource"></constructor-arg>
这个语句不注掉,就会创建多个连接,使事务不能使用
-->
<!-- <property name="runner" ref="runner"></property> 用这个就会出现错误-->
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/item"></property>
<property name="user" value="root"></property>
<property name="password" value="8438031100"></property>
</bean>
<!-- 配置AOP-->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.sise.service.*.*(..))"/>
<aop:aspect id="logAdvice" ref="transactionmanager">
<!-- <aop:before method="beginTransaction" pointcut-ref="pt1"></aop:before>-->
<!-- <aop:after-returning method="commitTransaction" pointcut-ref="pt1"></aop:after-returning>-->
<!-- <aop:after-throwing method="rollbackTransaction" pointcut-ref="pt1"></aop:after-throwing>-->
<!-- <aop:after method="release" pointcut-ref="pt1"></aop:after>-->
<aop:around method="aroundTransaction" pointcut-ref="pt1"></aop:around>
</aop:aspect>
</aop:config>
<!-- 配置spring创建容器时要扫描的包
注解开发:@ComponentScan(value = "com.sise") (只需要再主配置文件标志这个)
-->
<context:component-scan base-package="com.sise"></context:component-scan>
<!-- <!– 配置一个数据库的操作模板:JdbcTemplate –>-->
<!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
<!-- <property name="dataSource" ref="dataSource"></property>-->
<!-- </bean>-->
<!-- <!– 配置数据源 –>-->
<!-- <bean id="dataSource"-->
<!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>-->
<!-- <property name="url" value="jdbc:mysql:///spring_day02"></property>-->
<!-- <property name="username" value="root"></property>-->
<!-- <property name="password" value="1234"></property>-->
<!-- </bean>-->
<!-- 配置spring开启注解AOP的支持
注解开发:@EnableAspectJAutoProxy(exposeProxy = true)(只需要再主配置文件标志这个)
-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
/*
* Configuration:说这个类是配置类
*
* 用于指定当前类是一个 spring 配置类,当创建容器时会从该类上加载注解
* 。获取容器时需要使用 AnnotationApplicationContext(有@Configuration 注解的类.class)。 属
* ComponentScan:告知spring扫描这个包下的所有文件注解
*
* <!-- 告知spting往那些包及子包扫描注解信息 -->
<bean:component-scan base-package="com.sise"></bean:component-scan>
*
* PropertySource:
* 让这个文件加载再系统中,
* @Value("${properties文件的 key}")
*
@Value("${jdbc.driver}")
private String driver;
*
* Import加载非主配置文件,
* */
/*
* bean.xml:必须配置
* <!-- 告知spting往那些包及子包扫描注解信息 -->
<bean:component-scan base-package="com.sise"></bean:component-scan>
*
* */
@Configuration
@ComponentScan(value = "com.sise")
@PropertySource("bean.properties")
@Import({jdbcTemplateConfig.class,jdbcConfig.class})//多个辅助类配置文件用{}中间逗号隔开
@EnableAspectJAutoProxy(exposeProxy = true)
//@Import(jdbcTemplateConfig.class)
public class SrpingConfiguration {
}
public class jdbcTemplateConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url;
@Bean("template")
public JdbcTemplate createQueryRunner(DataSource dataSource) {
JdbcTemplate template = new JdbcTemplate(dataSource);
return template;
}
@Bean("dataSource")
public DataSource createDataSource() {
DriverManagerDataSource cds = new DriverManagerDataSource();
cds.setDriverClassName(driver);
cds.setUrl(url);
cds.setUsername(username);
cds.setPassword(password);
return cds;
}
}
public class jdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url;
@Bean("runner")
public QueryRunner createQueryRunner(DataSource dataSource) {
QueryRunner runner = new QueryRunner(dataSource);
return runner;
}
@Bean("dataSource")
public DataSource createDataSource() {
ComboPooledDataSource cds = new ComboPooledDataSource();
try {
cds.setDriverClass(driver);
cds.setJdbcUrl(url);
cds.setUser(username);
cds.setPassword(password);
return cds;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
}
写一个类把dao持久层多有重复属性代码写再一个类中然后extends就行了
public class JdbcDaoSupport {
@Autowired
private QueryRunner runner;
@Autowired
private ConnectionUtils connectionUtils;
@Autowired
private JdbcTemplate template;
public JdbcTemplate getTemplate() {
return template;
}
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public void setConnectionUtils(ConnectionUtils connectionUtils) {
this.connectionUtils = connectionUtils;
}
public QueryRunner getRunner() {
return runner;
}
public ConnectionUtils getConnectionUtils() {
return connectionUtils;
}
public void setDataSource(DataSource dataSource) {
if (template == null) {
template = createJdbcTemplate(dataSource);
}
}
private JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Component("transactionManager")
@Aspect//表示当前类是一个切面类
public class TransactionManager {
/*
*
*如果想用注解开发并且想使用aop,就只能用环绕通知,不能用before...,这些
* 因为他们执行顺序--->开启事务,释放和解绑事务(会导致线程和链接解绑,使链接为null,因此出现
* java.sql.SQLException: Can't call commit when autocommit=true),也就提交不了事务,从而使风险增加
* 因此注解只能使用环绕通知
*/
@Autowired
private ConnectionUtils connectionUtils;
@Pointcut("execution(* com.sise.service.impl.*.*(..))")
public void pt1() {
}
public void setConnectionUtils(ConnectionUtils connectionUtils) {
this.connectionUtils = connectionUtils;
}
// @Before("pt1()")
public void beginTransaction() {
try {
System.out.println("开启事务");
connectionUtils.getThreadConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
// @AfterReturning("pt1()")
public void commitTransaction() {
try {
connectionUtils.getThreadConnection().commit();
System.out.println("提交事务");
} catch (SQLException e) {
e.printStackTrace();
}
}
// @AfterThrowing("pt1()")
public void rollbackTransaction() {
try {
connectionUtils.getThreadConnection().rollback();
System.out.println("回滚事务");
} catch (SQLException e) {
e.printStackTrace();
}
}
// @After("pt1()")
public void release() {
try {
connectionUtils.getThreadConnection().close();
System.out.println("释放资源,将链接和事务解绑");
connectionUtils.CloseTranaction();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Around("pt1()")
public Object aroundTransaction(ProceedingJoinPoint pjp) {
Object[] args = pjp.getArgs();
Object proceed = null;
try {
beginTransaction();
proceed = pjp.proceed(args);
commitTransaction();
} catch (Throwable throwable) {
rollbackTransaction();
throw new RuntimeException(throwable);
} finally {
release();
}
return proceed;
}
}
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!
常见资源合集和破解 fmvvvteih...