目录
系列文章
6. Spring实例参考06-setter注入的10种方式
11. Spring实例参考11-API实现AOP前置/后置通知
12. Spring实例参考12-自定义类实现AOP
1 前言
该文供以下文章的参考内容:
Spring笔记https://blog.csdn.net/qq_34378496/article/details/128095043
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mysqlDao" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>
<bean id="oracleDao" class="com.evenif.dao.impl.IndexOralceDaoImpl"/>
<!-- 该配置是indexService中通过setIndexDao(..)方法自动注入,indexDao自动映射
indexService.setIndexDao(..)方法,如果名称不一致一定报错!这是其中的一种
注入方式:通过setter方法注入-->
<bean id="indexService" class="com.evenif.service.impl.IndexServiceImpl">
<property name="indexDao" ref="mysqlDao"></property>
</bean>
</beans>
2 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;
}
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 + '\'' +
'}';
}
}
3 dao层
package com.evenif.dao;
import com.evenif.bean.Index;
public interface IndexDao {
public Index getIndex();
}
4 dao层实现
4.1 实现1
package com.evenif.dao.impl;
import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
public class IndexMysqlDaoImpl implements IndexDao {
@Override
public Index getIndex() {
// 假装是从数据库获取的Index对象
return new Index(2,"mysql02");
}
}
4.2 实现2
package com.evenif.dao.impl;
import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
public class IndexOralceDaoImpl implements IndexDao {
@Override
public Index getIndex() {
// 假装是从数据库获取的Index对象
return new Index(3,"oralce03");
}
}
5 service层
package com.evenif.service;
import com.evenif.bean.Index;
public interface IndexService {
public Index getIndex();
}
6 service层实现
package com.evenif.service.impl;
import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
import com.evenif.service.IndexService;
public class IndexServiceImpl implements IndexService {
private IndexDao indexDao;
public void setIndexDao(IndexDao indexDao) {
this.indexDao = indexDao;
}
@Override
public Index getIndex() {
return indexDao.getIndex();
}
}
7 测试
package com.evenif;
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) {
//注意这个xml文件位置要放对
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
IndexService indexService = (IndexService) context.getBean("indexService");
System.out.println(indexService.getIndex());
}
}
//执行结果:
//Index{id=2, name='mysql02'}
版权声明:本文为qq_34378496原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。