springIoc容器自动装配通过autowire实现bean之间的关联关系。简单地说
不用自己去写set注入或构造注入。
byType按被注入对象的类型相同的进行注入(为属性赋值调用set方法)
byName按与被注入对象id与set方法名相同的进行注入
constructor(与byType类似)按构造方法中被注入对象的类型相同的注入
![](https://img.haomeiwen.com/i5128967/a8b12df9b3a05786.png)
![](https://img.haomeiwen.com/i5128967/329bc41a344323d8.png)
![](https://img.haomeiwen.com/i5128967/da2c9a5b6f42a5ca.png)
项目结构
![](https://img.haomeiwen.com/i5128967/9551ec651c9dcb26.png)
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>spring-ioc-autowire</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
</dependencies>
</project>
spring-configs.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="dataSource" class="bean.DataSource">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///test"></property>
<property name="userName" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="bean.JdbcTemplate" autowire="byName">
</bean>
</beans>
DataSource.java
package bean;
public class DataSource {
private String driver;
private String url;
private String userName;
private String password;
public DataSource() {
System.out.println("DataSource.DataSource()");
}
public DataSource(String driver, String url, String userName, String password) {
this.driver = driver;
this.url = url;
this.userName = userName;
this.password = password;
System.out.println("DataSource.DataSource(driver,url,userName,password)");
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "DataSource [driver=" + driver + ", url=" + url + ", userName=" + userName + ", password=" + password
+ "]";
}
}
JdbcTemplate.java
package bean;
public class JdbcTemplate {
private DataSource dataSource;
public JdbcTemplate() {
System.out.println("JdbcTemplate.JdbcTemplate()");
}
public JdbcTemplate(DataSource dataSource) {
this.dataSource = dataSource;
System.out.println("JdbcTemplate.JdbcTemplate(dataSource)");
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
System.out.println("JdbcTemplate.setDataSource()");
this.dataSource = dataSource;
}
@Override
public String toString() {
return "JdbcTemplate [dataSource=" + dataSource + "]";
}
}
TestIOCAutoWire.java
package test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.JdbcTemplate;
public class TestIOCAutoWire {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("spring-configs.xml");
JdbcTemplate jdbcTemplate=ctx.getBean("jdbcTemplate",JdbcTemplate.class);
System.out.println(jdbcTemplate);
ctx.close();
}
}
运行结果
DataSource.DataSource()
JdbcTemplate.JdbcTemplate()
JdbcTemplate.setDataSource()
JdbcTemplate [dataSource=DataSource [driver=com.mysql.jdbc.Driver, url=jdbc:mysql:///test, userName=root, password=root]]
如果spring-configs.xml这么配置,id改为dataSource1,autowire="constructor"
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="dataSource1" class="bean.DataSource">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///test"></property>
<property name="userName" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="bean.JdbcTemplate" autowire="constructor">
</bean>
</beans>
重新运行TestIOCAutoWire.java,运行结果如下
DataSource.DataSource()
JdbcTemplate.JdbcTemplate(dataSource)
JdbcTemplate [dataSource=DataSource [driver=com.mysql.jdbc.Driver, url=jdbc:mysql:///test, userName=root, password=root]]
如果spring-configs.xml这么配置,id改为dataSource1,autowire="byType"
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="dataSource1" class="bean.DataSource">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///test"></property>
<property name="userName" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="bean.JdbcTemplate" autowire="byType">
</bean>
</beans>
重新运行TestIOCAutoWire.java,运行结果如下
DataSource.DataSource()
JdbcTemplate.JdbcTemplate()
JdbcTemplate.setDataSource()
JdbcTemplate [dataSource=DataSource [driver=com.mysql.jdbc.Driver, url=jdbc:mysql:///test, userName=root, password=root]]
如果spring-configs.xml这么配置,id改为dataSource1,autowire="byName"
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="dataSource1" class="bean.DataSource">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///test"></property>
<property name="userName" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="bean.JdbcTemplate" autowire="byName">
</bean>
</beans>
运行结果如下
DataSource.DataSource()
JdbcTemplate.JdbcTemplate()
JdbcTemplate [dataSource=null]
因为JdbcTemplate没有名为setDataSource1的setter方法,改造
JdbcTemplate.java为如下
package bean;
public class JdbcTemplate {
private DataSource dataSource;
public JdbcTemplate() {
System.out.println("JdbcTemplate.JdbcTemplate()");
}
public JdbcTemplate(DataSource dataSource) {
this.dataSource = dataSource;
System.out.println("JdbcTemplate.JdbcTemplate(dataSource)");
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource1(DataSource dataSource) {
System.out.println("JdbcTemplate.setDataSource()");
this.dataSource = dataSource;
}
@Override
public String toString() {
return "JdbcTemplate [dataSource=" + dataSource + "]";
}
}
测试成功,结果如下
DataSource.DataSource()
JdbcTemplate.JdbcTemplate()
JdbcTemplate.setDataSource()
JdbcTemplate [dataSource=DataSource [driver=com.mysql.jdbc.Driver, url=jdbc:mysql:///test, userName=root, password=root]]
网友评论