spring支持三种依赖注入方式:
1.属性注入
2.构造器注入
3.工厂方式注入(很少用)
一. 首先介绍一下spring所需的几个jar文件:
或者使用maven进行管理:
pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.wj.spring01</groupId>
<artifactId>spring01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</project>
目录结构:
二. 基于set方法的注入方式:
建立User实体:
注意要有 属性对应的set方法,以及一个无参的构造方法
编写xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置一个 bean -->
<bean id="user" class="com.wj.spring.entity.User">
<property name="userNam" value="bxklk"></property>
<property name="age" value="26"></property>
</bean>
</beans>
<bean> 标签对应的是需要进行注入的类,id是bean的名字,class 是该类的全类名,通过反射机制 注入
property对应的是类的属性,以及需要赋的值。
编写main方法:
package com.wj.spring.entity;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by 小美女 on 2018/11/16.
*/
public class Main {
public static void main(String[] args){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) ctx.getBean("user");
System.out.println(user);
}
}
ClassPathXmlApplicationContext:从 类路径下加载配置文件。
getBean();通过对应的bean ID 查找要注入的类
输出结果:
注:
1.也可以不通过Id而使用类名进行注入,如上图
2.如果 User 里面包含另一个对象属性 Car
修改User类,加一个对象
在bean.xml中添加<bean id="car">,并且在<bean id="user">中加入相应的属性 如下:
<!-- 配置一个 bean -->
<bean id="user" class="com.wj.spring.entity.User">
<property name="userNam" value="bxklk"></property>
<property name="age" value="26"></property>
<!-- 通过 ref 属性值指定当前属性指向哪一个 bean! -->
<property name="car" ref="car"></property>
</bean>
<bean id="car" class="com.wj.spring.entity.Car">
<property name="carNam" value="baoma"></property>
<property name="carSize" value="25"></property>
</bean>
这里也可以不单独配置car的bean ,而使用声明内部bean的方式进行注入
<!-- 配置一个 bean -->
<bean id="user" class="com.wj.spring.entity.User">
<property name="userNam" value="bxklk"></property>
<property name="age" value="26"></property>
<!-- 内部 bean, 类似于匿名内部类对象. 不能被外部的 bean 来引用, 也没有必要设置 id 属性 -->
<property name="car">
<bean class="com.wj.spring.entity.Car">
<property name="carNam" value="benchi"></property>
<property name="carSize" value="20"></property>
</bean>
</property>
<!--<!– 通过 ref 属性值指定当前属性指向哪一个 bean! –>-->
<!--<property name="car" ref="car"></property>-->
</bean>
4.集合属性装配
<bean id="user2" class="com.wj.spring.entity.User">
<property name="userNam" value="bxklk"></property>
<property name="age" value="26"></property>
<!-- 通过 ref 属性值指定当前属性指向哪一个 bean! -->
<property name="car" ref="car"></property>
<property name="cars">
<list>
<ref bean="car"></ref>
<ref bean="car2"></ref>
</list>
</property>
</bean>
此处还可以单独声明list集合bean
<bean id="user2" class="com.wj.spring.entity.User">
<property name="userNam" value="bxklk"></property>
<property name="age" value="26"></property>
<!-- 通过 ref 属性值指定当前属性指向哪一个 bean! -->
<property name="car" ref="car"></property>
<!-- 声明集合类型的 bean -->
<property name="cars" ref="cars">
</property>
</bean>
<!-- 声明集合类型的 bean -->
<util:list id="cars">
<ref bean="car"></ref>
<ref bean="car2"></ref>
</util:list>
5.使用p进行快速配置
<bean id="user3" class="com.wj.spring.entity.User"
p:cars-ref="cars" p:userNam="Titannic"></bean>
以上简单介绍了属性配置的几个方面
三 . 构造器注入
进行构造器注入一定要有带参数的构造函数
若一个 bean 有多个构造器, 如何通过构造器来为 bean 的属性赋值
可以根据 index 和 type进行更加精确的定位.
若字面值中包含特殊字符, 则可以使用 DCDATA 来进行赋值.
注:构造器注入只是简单介绍了一下,关于spring的bean配置方式还涉及级联操作以及有关map类型数据的注入,感兴趣的朋友可以查阅一下