目录
系列文章:
6. Spring实例参考06-setter注入的10种方式
11. Spring实例参考11-API实现AOP前置/后置通知
前言
本文供以下文章参考使用:
1 java 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 + '\'' +
'}';
}
}
2 dao
package com.evenif.dao;
import com.evenif.bean.Index;
public interface IndexDao {
public Index getIndex();
}
3 daoImpl
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 service
package com.evenif.service;
import com.evenif.bean.Index;
public interface IndexService {
public Index getIndex();
}
5 serviceImpl
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;
}
//当定义了使用构造器自动装配时,就必须添加一个显式的无参构造,因为byType和byName实际上都会调用无参构造来实例化本类
public IndexServiceImpl() {
}
//用于使用构造器自动装配!
public IndexServiceImpl(IndexDao indexDao) {
this.indexDao = indexDao;
}
@Override
public Index getIndex() {
return indexDao.getIndex();
}
}
6 配置
<?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" default-autowire="byName">
<bean id="indexDao" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>
<!-- 一般装配方式 -->
<bean id="indexService" class="com.evenif.service.impl.IndexServiceImpl">
<property name="indexDao" ref="indexDao"></property>
</bean>
<!--
根据名称自动装配
注意:IndexServiceImpl.setIndexDao(IndexDao indexDao) 和
<bean id="indexDao" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>中
“setIndexDao”必须与id="indexDao"是对应关系,与IndexMysqlDaoImpl 定义的变量名称 IndexDao xxx;没有关系!
-->
<bean id="indexService02" class="com.evenif.service.impl.IndexServiceImpl" />
<!--
根据类型自动装配,与bean的id没有关系,但是同一种类型的bean只能有一个,否则报错!
例如再有一个:
<bean id="indexDao02" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>
使用byType一定报错!
-->
<bean id="indexService03" class="com.evenif.service.impl.IndexServiceImpl" autowire="byType"/>
<!--
根据构造方法自动装配,
public IndexServiceImpl(IndexDao indexDao) {
this.indexDao = indexDao;
}
-->
<bean id="indexService04" class="com.evenif.service.impl.IndexServiceImpl" autowire="constructor"/>
</beans>
<!-- 也可以在头文件中使用default-autowire定义,则bean的 autowire="xxx" 可以省略 ,如下所示:-->
<!--
<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" default-autowire="byName">
-->
<!--
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" default-autowire="byType">
-->
<!--
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" default-autowire="constructor">
-->
7 测试主类
package com.evenif;
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");
for(int i=2;i<5;i++){
IndexService service = (IndexService) context.getBean("indexService0"+i);
System.out.println(service.getIndex());
}
}
}
//执行结果:
// create Index class with params
// Index{id=2, name='mysql02'}
// create Index class with params
// Index{id=2, name='mysql02'}
// create Index class with params
// Index{id=2, name='mysql02'}