目录
系列文章:
Spring实例参考01-一个简单实例
前言
本文供以下文章参考使用:
Spring基础回顾__evenif的博客-CSDN博客
1 自定义日志类:
package com.evenif.log;
public class CustomLog {
public void before(){
System.out.println("custom aspect log before...");
}
public void after(){
System.out.println("custom aspect log after...");
}
}
2 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 头文件中引入了aop命名空间 -->
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自动创建单例 -->
<bean id="indexDao" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>
<bean id="customLog" class="com.evenif.log.CustomLog"/>
<!-- 自动创建单例服务依赖注入已存在dao -->
<bean id="indexService" class="com.evenif.service.impl.IndexServiceImpl">
<property name="indexDao" ref="indexDao"></property>
</bean>
<aop:config>
<aop:aspect ref="customLog">
<aop:pointcut id="pointcut" expression="execution(* com.evenif.service.impl.*.*(..))"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
3 bean:
package com.evenif.bean;
public class Index {
private int id;
private String name;
public Index() {
// System.out.println("create Index class");
}
public Index(int id, String name) {
this.id = id;
this.name = name;
// System.out.println("create Index class with params");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Index{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
4 dao:
package com.evenif.dao;
import com.evenif.bean.Index;
import java.util.List;
public interface IndexDao {
Index getById(int id);
void insert(Index index);
void update(Index index);
void delete(int id);
List<Index> loadAll();
}
5 daoImpl
package com.evenif.dao.impl;
import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
import java.util.ArrayList;
import java.util.List;
public class IndexMysqlDaoImpl implements IndexDao {
@Override
public Index getById(int id) {
// 假装是从数据库获取的Index对象
return new Index(id, "mysqlIndex");
}
@Override
public void insert(Index index) {
System.out.println("mysql insert : " + index);
}
@Override
public void update(Index index) {
System.out.println("mysql update : " + index);
}
@Override
public void delete(int id) {
System.out.println("mysql delete index : " + id);
}
/**
* @return
*/
@Override
public List<Index> loadAll() {
List<Index> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add(new Index(i + 1, "mysql-" + i));
}
return list;
}
}
6 service
package com.evenif.service;
import com.evenif.bean.Index;
import java.util.List;
public interface IndexService {
Index getIndex(int id);
void addIndex(Index index);
void modifyIndex(Index index);
void deleteIndex(int id);
List<Index> loadIndexList();
}
7 serviceImpl
package com.evenif.service.impl;
import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
import com.evenif.service.IndexService;
import java.util.List;
public class IndexServiceImpl implements IndexService {
private IndexDao indexDao;
public void setIndexDao(IndexDao indexDao) {
this.indexDao = indexDao;
}
public IndexServiceImpl() {
}
public IndexServiceImpl(IndexDao indexDao) {
this.indexDao = indexDao;
}
@Override
public Index getIndex(int id) {
return indexDao.getById(id);
}
@Override
public void addIndex(Index index) {
indexDao.insert(index);
}
@Override
public void modifyIndex(Index index) {
indexDao.update(index);
}
@Override
public void deleteIndex(int id) {
indexDao.delete(id);
}
@Override
public List<Index> loadIndexList() {
return indexDao.loadAll();
}
}
8 Main:
package com.evenif;
import com.evenif.bean.Index;
import com.evenif.bean.Person;
import com.evenif.bean.User;
import com.evenif.service.IndexService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
IndexService service = (IndexService) context.getBean("indexService");
System.out.println(service.getIndex(1));
Index index = new Index(2,"text02");
service.addIndex(index);
service.deleteIndex(index.getId());
service.modifyIndex(index);
service.loadIndexList();
}
}
//执行结果:
// custom aspect log before...
//custom aspect log after...
//Index{id=1, name='mysqlIndex'}
//custom aspect log before...
//mysql insert : Index{id=2, name='text02'}
//custom aspect log after...
//custom aspect log before...
//mysql delete index : 2
//custom aspect log after...
//custom aspect log before...
//mysql update : Index{id=2, name='text02'}
//custom aspect log after...
//custom aspect log before...
//custom aspect log after...